Horse Racing Duals puzzle discussion

and yet the command wont work
error: cannot find symbol

    Array.sort(horses);

edit: Arrays.sort
damn
tnx(;

Im trying to understand why I only have 90% and I canā€™t pass the Horses in disorder test but I canā€™t figure it why? Iā€™m trying to learn c++ pls help!

Thanks!

int main()
{
    int N;
    cin >> N; cin.ignore();
    
    vector<int> PI;
    vector<int> Diff;
    int D = -1;
    int previous = -1;
    
    for (int i = 0; i < N; i++) {
        int Pi;
        cin >> Pi; cin.ignore();
        
        //cerr << "Pi = " << Pi << endl;
        
        PI.push_back(Pi);
    }
    
    
    sort(PI.begin(), PI.end(), greater<int>());
    
    for(int k = 0; k < N; k++)
    {
        
        int tmp;
        
        if(previous == -1)
        {
            previous = PI[k];
        }
        else
        {
            
            tmp = previous - PI[k];
            
            if(D == 1 || tmp == 1)
            {
                D = 1;
                break;
            }
            
            else if(D == -1 || D > tmp)
            {
                D = tmp;
            }
            
            previous = PI[k];
        }
            
    }
    
    
    // Write an action using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;

    cout << D << endl;
}

I canā€™t find where the problem is. Can you help me, please?
for (i = 0; i < N; ++i) {

    cin >> P[i]; 
   // cin.ignore();
    cout <<"power " << P[i]<<endl;
      
}

 for (i = 0; i < N; ++i) {
      d = P[i + 1] - P[i];
    cout <<"d  "<< d << endl;
     min=0;
     if(P[i] < min) {
         min = P[i];
         } 
 }

You output everything but the answer
If you need to debug you should use cerr instead of cout

1 Like

Thank you for answer ! )

Iā€™m using qsort in c for this but itā€™s not working which is oddā€¦ Why though?

Edit: It worked!! That damn \n kept me thinking for an hour -_-
got 100% finally <3

Hi everyone,

FWIW, in C,
I had timeout in the many horses test
whether I did an insertion sort (embedded in the scanf loop or after) or a quicksort.
I passed 100% only by using qsort.

Have a nice time :wink:

Salut, je valide ce test Ơ 90% quand je soumets mon rƩsultat alors que les tests sont valides. Quand pensez-vous ?

public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int N = in.nextInt();
    TreeSet<Integer> tree = new TreeSet<Integer>();
    for (int i = 0; i < N; i++) {
        int pi = in.nextInt();
        tree.add(pi);
    }
    Iterator treePuisaance = tree.iterator();
    
    int difResult = 0;
    int valeurPrecedente = (int)treePuisaance.next();
    while(treePuisaance.hasNext()){
        
        int valeurCourante = (int)treePuisaance.next();
        int difCourante = valeurCourante - valeurPrecedente;
        if(difResult == 0 || difCourante < difResult){
            difResult = difCourante;
        }
        valeurPrecedente = valeurCourante;
    }

    System.out.println(difResult);
}

Something is wrong with C# here. I tested it and its returning the wrong number although the console is proving I am feeding it the correct answer.

using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
class Solution
{
    static void Main(string[] args)
    {
        int N = int.Parse(Console.ReadLine());
        List<int> list = new List<int>();

        for (int i = 0; i < N; i++)
        {
            int pi = int.Parse(Console.ReadLine());
            Console.WriteLine(pi);
            list.Add(pi);
        }

        Console.Error.WriteLine();

        list.Sort();
        int diff = 10000000;

        foreach(var v in list)
            Console.Error.WriteLine(v);
        
        Console.Error.WriteLine();
        
        for (int i = 1; i < N; i++)
        {
            int z = list[i] - list[i - 1];

            if (z < diff)
                diff = z;
        }

        // Write an action using Console.WriteLine()
        // To debug: Console.Error.WriteLine("Debug messages...");

        Console.Error.WriteLine(diff);
        Console.Error.WriteLine();

        Console.WriteLine(diff);
    }
}```

5

8

9

5

8

9

1

1


![image|690x388](upload://xSkZJbFzDyu8xp2eKdcWYk6gxmR.png)

Check which of your printing are on the output console and which are on the error console.

1 Like

Everything is on the error console, and the var passed to that to verify its the correct answer is the same that is passed into WriteLine.

I thought it was pretty obvious from the pic and the code that you can tell what is what in which order. I literally have the same exact solution in C++ (except for cout) without issue.

1 Like

So Iā€™m not sure why I get different results when the logic is identical but just a different language.

#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;
    cin >> N;
    cin.ignore();
    vector<int> vec;

    for (int i = 0; i < N; i++)
    {
        int Pi;
        cin >> Pi;
        cin.ignore();
        vec.push_back(Pi);
    }

    sort(vec.begin(), vec.end());
    int diff = 10000000;

    for (int i = 1; i < vec.size(); ++i)
    {
        int z = vec[i] - vec[i - 1];

        if (z < diff)
            diff = z;
    }
    // Write an action using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;

    cout << diff << endl;
}```

Your c++ solution and your c# solution implements the same algorithm, but they do not provide the same output log and error log.

1 Like

Why when i use .sort() in JavaScript, this doesnā€™t count disorder success ? I finish only with 90%ā€¦

When i use sort = > 90%
ā€œHorses in disorderā€ not okā€¦
I donā€™t understand.

With sort, you have less operations to do because you have no more to compare each horse with all others.

But if your sort does not take care that values are number and not string it is an epic fail ā€¦

I donā€™t know how you coded but you should check that because it is classic error

In javascript, pi is a number. I create an array with pi, and i sort() this for compare latter. I sort an array of number.
If i try another solution, my script are too long.

yes, definitely.
precise and perfect hint