Horse Racing Duals puzzle discussion

First thing: it’s poor practice to post full source code, it used to be really frowned upon in the past.

Second thing: Python’s a[-1] means the last element of a. So, if a is sorted, a[0] - a[-1] quickly becomes negative and then all other differences would be positive, so greater than this. Hope this is hint enough to fix your problem.

Bonjour,
je suis débutante sur codingGame et java.
J’ai essayé ce jeu et n’ayant pas réussi, j’ai cherché des solution et en ai trouvé une avec
int answer = Integer.MAX_VALUE;
mais je ne comprends pas le lien avec MAX_VALUE?
Pourriez - vous m’expliquez SVP.
Merci d’avance

Quand tu tentes de minimiser une valeur (la différence de puissance entre les chevaux), il faut que tu l’initialises à quelques choses de très grand, c’est pour cela qu’il utilise Integer.MAX_VALUE, qui est la plus grande valeur qu’un int peut prendre (2 147 483 647).

Ensuite, je suppose que dans son code il fait quelque chose comme

if (myNewValue < answer)
     answer=myNewValue

Ok, I dont know if someone will healp me, but there I go…

Can someone help me with this, I don’t knot why it is not working:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

void getUnitaryDif(int N, int arrayT[],int arrayDif[]);
int difOfEntries (int arrayUnitaryDif[],int startI,int endI);
int module(int input);
int minimum (int inputA, int inputB);


/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/

int main()
{
int N;
int arrayT[N];
cin >> N; cin.ignore();
for (int i = 0; i < N; i++) {
    int Pi;
    cin >> Pi; cin.ignore();
    arrayT[i] = Pi;
}

int arrayUnitaryDif[N-1];

getUnitaryDif(N, arrayT, arrayUnitaryDif);

int result(arrayUnitaryDif[0]);

for (int i = 1; i < N-1; i++)
{
 
    result = minimum(result, module(arrayUnitaryDif[i]));   
    
}

for (int i = 1; i < N; i++)
{

    for (int j = i+1; j <= N; j++)
    {

        result = minimum(result, module(difOfEntries(arrayUnitaryDif, i, j)));

    }

}

cout << result << endl;
}

void getUnitaryDif(int N, int arrayT[],int arrayDif[])
{

for (int i = 0; i < N-1; i++)
{

    arrayDif[i] = arrayT[i+1] - arrayT[i];

}

}

int difOfEntries (int arrayUnitaryDif[],int startI,int endI)
{
int dif(0);

for (int i = startI-1; i < endI; i++)
{

    dif += arrayUnitaryDif[i];

}

return dif;
}

int module(int input)
{

if (input < 0)
    return -input;
else
    return input;

}

int minimum (int inputA, int inputB)
{

if (inputA < inputB)
    return inputA;
else
    return inputB;

}

The ideia is to get an array (arrayUnitDif[]) that has the diference between horse N and horse N+1. Then any other diference of N and N+x would be as simple as summing arrayUnitDif[N] to arrayUnitDif[N+x-1].

Can someone help me, please. Ive been trying this for hours. :cry:

What’s the problem? Does it not compile? Does it not run? Does it give a wrong result? Every time? Sometimes?

Hi everyone. I only got 90% score for this puzzle. This result shows "
Shorter difference on the first 2 horses" not resolve. Anybody has any idea?

How do you calculate the shortest difference? Are you sure your algorithm will work for any valid input? Are you sure the algorithm implemented correctly? I suggest to recheck everything

Hi everyone!
I’m a bit disappointed : I just got an error after submit, on the “01 Simple Case”. All the other tests are OK. Must be something really stupid, but I can’t find what… Any idea ?
Thanks,

Edit: Ok, I re-coded this from scratch, using simple and brutal sorting this time. Way more simple than the custom sorting based on dichotomy I used before, and much more efficient… I still don’t have any idea why the simple case was wrong. But the new code scored 100% easily, so I won’t search for long…

Hi i’m having some issue resolving this puzzle
Any kind of help would be appreciated, thanks.

Here is my code (C++ )

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
int main()
{
    int N;
    int D=10000000;
    vector<int> liste (N,0); 
    cin >> N; cin.ignore();
    for (int i = 0; i < N; i++) {
        int Pi;
        cin >> Pi; cin.ignore();
        liste[i]=Pi;
        
    }

    sort(liste.begin(),liste.end());
    for (int k = 0; k<N-1;k++){
        if (abs(liste[k] - liste[k+1]) < D){
            D=abs(liste[k] - liste[k+1]);
        }
    }
    

    // Write an action using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;

    cout << D << endl;
}

I get this :
Errors
Segmentation fault.
at Answer.cpp. function main () on line 32
Failure
Found: Nothing
Expected: 1

I think there’s a problem with the way i create my vector ?

try liste.push_back(Pi);

thanks it worked

could you tell me what goes wrong with liste[i]=Pi ?

The list is initially empty (you just say that you plan to add N elements, so the vector class can already allocate that memory and avoid moving bytes while extending).
So, before you can set/overwrite the element at index x, you first have to add it with push_back.

1 Like

i dont know why my solution doesnt works in javascript for many horses, please everyone can help me?

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
var aux;
var menor=10000;
var N = parseInt(readline());
var cadena = new Array(100001);
for (var i = 0; i < N; i++){
    
    cadena[i]=parseInt(readline());
}
cadena.sort(function(a, b){return a-b});

for (var i = 0; i < N; i++) {
    if (i===0){
        aux=cadena[0];
    }
    if (i>0){
        if ((cadena[i]-aux)<menor){
        menor=(cadena[i]-aux);
        aux=cadena[i];
        }
    }
}

// Write an action using print()
// To debug: printErr('Debug messages...');

print(menor);

The problem is that you store your variable “aux” only if your condition is true, so if it’s false you keep all time the same value and your condition can’t be true anymore

Using Javascript I had trouble with Horses in Disorder test but solved it.
In case this helps anyone with a similar problem,
I originally had a variable D which held the lowest difference between currently checked values, but I checked if D was undefined before every comparison. I changed D to be a very high initial value 99999999 (which I should have done in the first place if I’d noticed the constraints section 0 < Pi ≤ 10000000)

var temp= pi[i+1]-pi[i]    //pi is my array of horse power

var D;
if(!D || temp < D){    
    D = temp;
}

//became
var D=99999999
if(temp < D){
    D = temp;
}

just changing this made it efficient enough to pass.

Good Luck!

There is a weird bug when solving this puzzle in PHP.

If there is an extra line break at the end of the source code, the last test in the validation tests will not pass (Many horses).
This happens even if all the IDE test pass and all the other validation tests pass.

Please fix this.

2 Likes

To me it’s not a bug but just how PHP works while the interpretor of PHP consider that all outside of the php tag is just text, normally to write html. So if the PHP tag is closed you can write hardcoded value and CG will consider that like the STDOUT of the file.
Except if they scan the file and remove all outside any php tag there is no issue of this, and i don’t think we really need that

I don’t think the disorderly is solvable in pure bash (I mean not calling external utilities). Anybody did it??

I am really not good in bash (never did any bash except for this puzzle) but yeah i did it

Could you share your code?? You can mail me if you want:

[No, you shouldn’t ask for this here.]