Horse Racing Duals puzzle discussion

there is an error it is looking for 47 but that would mean the 2 best horses for the dule would be “9999999” and “9999952” but there is no “9999952” in the list. I wrote code to test this. here it is::
li = []
w = 0
x = 0
y = 0
z = 0
run = 0

n = int(input())
for i in range(n):
pi = int(input())
li.append(pi)
w = “FALSE”
x = 0
y = 0
for i in li:
if i > x: x = i
y = y + 1

for i in li:
if i == x - 47: W = “TRUE”

z = n - y
print(“x=”,x,“y=”,y,“z=”,z,“IS THERE A 47 POINT SPREAD?”,w, file=sys.stderr)
print(x)

So either it is not explained properly as in they are not looking for “The difference D between the two closest strengths.” but rather they was them split by 2s and then to locate the closest set. This is either a mistake in the fin target witch is “111” by the way or a miss communication in the directions
AS PER PYTHON3

You seem to be assuming that the strongest horse must be one of the racers. Is that requirement mentioned in the problem statement?

just like a lot of other people i succeed in all the tests except horses in disorder.

i just now realised what the problem is. all horses probably have 0 pi (that’s why they are in disorder, all dead)

so your solution must allow for that

finalement plutôt facile avec l’appel à une fonction builtin de python

qu’est ce qui ne va pas avec mon code ?

const N = parseInt(readline());

var chevaux = [];
for (let i = 0; i < N; i++) {
    const pi = parseInt(readline());
    chevaux.push(pi);
};

var diff = chevaux.sort((a,b) => a-b)

                    .map((current, index, array) => current - array[index-1])

                    .slice(1)

                    .sort((a,b => a-b));

                    

console.log(diff[0]);
#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 dif=0,temp=0;

    cin >> N; cin.ignore();

    int *arr;

    arr=new int[N];

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

        int Pi;

        cin >> Pi; cin.ignore();

        arr[i]=Pi;

    }

    for (int i=0; i<N;i++)

    {

        cerr << arr[i] << endl;

    }

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

        for (int z=1;z<N;z++)

        {

            cerr << "compare: " << arr[i] << " vs " << arr[z] << endl;

            if(dif != 0){

                int newdif=abs(arr[z] - arr[i]);

                cerr << "newdif: " << newdif << endl;

            if (newdif < dif && newdif!=0){

                dif=newdif;

                cerr << "Correct" << endl;

            }

            }else{

                dif=abs(arr[z]-arr[i]);

            }

            cerr << "dif: " << dif << endl;

        }

    }

    // Write an answer using cout. DON'T FORGET THE "<< endl"

    // To debug: cerr << "Debug messages..." << endl;

    cout << dif << endl;

}

Any one can found the error?
I has problem at task 3. result is 111 but expect 47

Asking for help. I pass every testcases except the “Horses in disorder”.
Why isn’t my code working on that single test? I got a difference of 2 instead of 1. Any ideas?
Thank you :slight_smile:

const N = parseInt(readline());
let d=10000000;
let p=[]
for (let i = 0; i < N; i++) {
    const pi = parseInt(readline());
    p[i]=pi;
}

p.sort;

for (let j=0; j<N-1; j++) {
    if (Math.abs(p[j+1]-p[j])<d) {
        d=Math.abs(p[j+1]-p[j]);
    }
}

console.log(d);

The sorting of p is not working.

You have to write: p.sort(); then it passes all testcases.

2 Likes

I passed test case 1 with this code below in Python. However, I can not pass 2 and 3. Does anyone can tell me what is the problem with the code below?

import sys
import math

n = int(input())
pi_list = [] #empty list for pi
for i in range(n):
pi = int(input())
pi_list.append(pi) #append all pi to pi_list
a = max(pi_list) # find the maximum value of pi in the list
pi_list.remove(a) # remove the maximum one
b = max(pi_list) # find the second maximum value in the list
diff = a - b # maximum - second maximum
print(diff) # the answer should be the smallest difference

Your code finds the difference between the two maximum values. But that’s not your task. You have to find the smallest difference between two values.
Example:

4
2
3
9
20

The smallest difference is between 2 and 3. So the answer is 1 because it’s the smallest difference. But your code prints 11, because it’s the difference between 9 and 20.
Does this answer your question?

1 Like

Actually, I found out why… because the difference of the maximum and the second maximum is not necessarily the smallest -_-!

Yes, you are right. This is what I want to understand. Thanks. @ItsAFeature

1 Like

Hello,
I solved using C++ but was not able to pass Many Horses Case.
Output of the Many Horses Case was = Process has timed out. This may mean that your solution is not optimized enough to handle some cases.
What am I doing Wrong?

Code :

int main()

{

    int N, P[100000], i, d[100000],min  ;

    cin >> N;

    

    for (i = 0; i < N; i++) 

    {    

        cin >> P[i]; cin.ignore();

    }

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

    {   

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

        {

            if(P[j]>P[j+1])

            {

                int temp;

                temp=P[j+1];

                P[j+1]=P[j];

                P[j]=temp;

            }

        }

    }

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

    {

        d[i]=P[i+1]-P[i];

    }

     for(i=1;i<N-1;i++)

    {   

        if(d[0]>d[i])

        {

            min=d[i];

        }

    }

cout<<min<<endl;

}

If I understand your code right, you have implemented your own algorithm to sort P.
That’s not necessary, because C++ has already a sort-algorithm.
You can replace the for-loop for sorting P by this:

sort(P, P+N);

This sorts P and makes your code much faster. Now you don’t have the problem of timeout anymore. You still have a bug in your code, but try to find it on your own. I don’t want to do all the work for you :wink:
I hope this answers your question.

3 Likes

I am new to C++ and hence, didnt knew about sort algorithm.
Indeed there was a mistake and i solved and scored 100%.
Thanks Alot!!

Thanks just changed this part in my code and it works passes the test now!
c=expr $b - $a
for
c=$(( b - a ))

Hi, what does the validator “Horses in disorder” exactly mean? I read somewhere it is similar to “Horses in any order” but can somebody elaborate.

it means that in this test/validator the horses are not sorted by power.

in “simple cas” : sorted by ascending power.
in “many horses” : sorted by decresing power.
in “horses in disorder” : unsorted

2 Likes

How is this different from “Horses in any order validator”?

same kind of test. Here it is so you can check what’s the issue in your code:
Input

10
12
15
3
10
15
5
7
16
5
1
1 Like