Horse Racing Duals puzzle discussion


#336

Maybe your algorithm is not optimized enough?


#337

Salut à tous! Je suis nouveau sur CG. Y’a des tests supplémentaires dans horse-racing, je ne sais pas comment les passer. Qcqn a une iddée?


#338

Hi there !

Is someone using Kotlin for this puzzle ?
I passed all the tests except the ‘huge’ ones…

It seems that I can’t even store the int in array without timeout.

this code :

 for (i in 0 until N) {
        //val pi = input.nextInt()
        lesdadas+= input.nextInt()
        
 }

without anything else make a timeout.
Is someone succeed in Kotlin?
thanks


#339

I wanted to make a liste with the pi
I putted
10. liste.append(pi)

but it doesnt work, I got the message
SyntaxError: Non-ASCII character ‘\xc3’ in file Answer.py on line 10, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

at Answer.py. not in a function on line 10

someone can explain ??


#340

Did you try to use a non-ASCII character in Python 2?


#341

what 's non ASCII character (sorry Im begginer)
Something like é, @ ??

No I didnt


#342

Ok I found the problem. It was because I putted : " # We create a liste ".
I delete it and it worked.
But I dont understand why…


#343

Nothing special in “# We create a liste” but maybe you printed an no-break space.


#344

I’ve used lists instead of arrays and it worked. This is the only difference between your algo and mine.


#345

Aaaaaaarg ! I just did it and it’s fine ! :slight_smile:
Thanks a lot !


#346

Hello , can i get the validator test by private message too. I am facing the same error for “Shorter difference on the first 2 horses”. Thanks


#347

Hi guys, just solved this in java. But found an odd misbehavior of the iteration through array list via for cycle.

See here :

import java.util.*;
import java.io.*;
import java.math.*;

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

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        ArrayList<Integer> powers = new ArrayList<Integer>();
        
       
        for (int i = 0; i < N; i++) {
            int pi = in.nextInt();
            powers.add(pi);
            //DEBUG
            //System.err.println(pi);
       }
        
        //sort numerically
        Collections.sort(powers);

        // go through array, remembering the smallest differnce
        int diff = -1, prev = -1;
         int curr = -1;      
        for (int i = 0; i < N-1; i++) {
 //       for (int curr : powers) {
            //DEBUG
            //System.err.println(curr);
            
           prev = powers.get(i);
           curr = powers.get(i+1);

    //first horse
    if (prev == -1) {
        prev = curr;
    }
    else
    //second horse
    if (diff == -1) {
        diff = curr - prev;
        prev = curr;
    } else
    // third and more ....
    if (curr - prev < diff) {
        diff = curr - prev;
        prev = curr;
    }

    }
    // Write an action using System.out.println()
    // To debug: System.err.println("Debug messages...");

    System.out.println(diff);
    }
    }

When I try to use the iteration (commented out) via the for (item : array) construct, then the 3rd test fails by giving different result than it is supposed to. First 2 tests work normally.


#348

Hello, I’m using C and I get time out at many horses and numerous horses.
I used Quicksort and single for loop comparison everything in it’s own function.


#349

Hi
I’m using C# and I can’t do the last one of the many horses it says it takes to much time.
Using 2 for loops to search na array with the values.
HELP PLS!


#350

Hi, I also used Quicksort and then a loop iterating over all sorted values to find the lowest difference. I failed the last test at first then I changed the way i choose my Pivot element to be the median of three values, for those values i chose index 0, index size - 1 and index size / 2 then it worked out for me. btw i was using c++


#351

Hey,

In JS, I obtain a difference in power that is less (30) than the one the test suggests (47). Is this a bug ?

Best.


#352

Hi, I am desperately trying to understand why this code get the segmentation fault error when I test it. As a noob trying to learn, I d like to understand why, more than to find a workaround without first a comprehension of the error.

Thanks for any help!

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

int main()
{
    int N;
    vector <int> MyN(N);
    
    cin >> N; cin.ignore();
    for (int i = 0; i < N; i++) {
        int Pi;
        cin >> Pi; cin.ignore();
        MyN[i]=Pi;
    }
    
    sort(begin(MyN), end(MyN));
    
    int minSpace = 10000000;
    int currentSpace = `Preformatted text`0;
    for (int i = 0; i < N-1; i++)
    {
        currentSpace = MyN[i+1]-MyN[i];
        
        if (currentSpace > minSpace)
        {
            minSpace=currentSpace;
        }
    }
    
    cout << minSpace << endl;
    //action using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;

  
}

#353

hey, why cant i use Arrey.sort?


#354

maybe because it is “Array.sort” ?
:rofl:


#355

Possibly because you initialize myN before you have read N, so the value of N is random and may exceed the allowed memory size.