Блог пользователя __declspec

Автор __declspec, история, 3 года назад, По-английски

Is it possible to hack java's quicksort? I found this generator only capable of hacking int arrays but not long ones.

Unsuccessful hacking attempt (they used Arrays.sort on long array): https://codeforces.net/contest/1671/hacks/798786

Successful hacking attempt (they used Arrays.sort on int array): https://codeforces.net/contest/1671/hacks/798795

  • Проголосовать: нравится
  • +31
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by __declspec (previous revision, new revision, compare).

»
3 года назад, # |
  Проголосовать: нравится +21 Проголосовать: не нравится

As a long time java user, yes, it's possible to hack it. You can also hack long, but I think you need a different generator. The source code for the generator is essentially a copy-paste of Java's Arrays.sort method, but built in such a way to make Arrays.sort pick the worst possible pivot each time.

So, what should you do instead?

  • Option 1: Use Collections.sort(). This uses Mergesort instead of QuickSort, which has a worst-case complexity of n*log(n) which is better than the n^2. You can put everything into an ArrayList, sort that, and then copy it back.
  • Option 2: (faster constant factor, my favorite) RuffleSort! You can copy it from any of my submissions, but the idea is to shuffle the array O(n) before sorting it. It's still O(n*log(n)) just like option 1, but it's faster.
  • Option 3: (even faster, but only works when array size is >=5*10^4): Use a radix sort to sort the array in O(2*n) for ints, or O(4*n) for longs using 2^16 buckets. You have to be careful with this one. On problems with 10^4 testcases, you can TLE if your runtime is O(2^16 + n) per testcase, which is easy to miss.
  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +21 Проголосовать: не нравится

    I didn't explain this earlier, but the reason Collections.sort uses MergeSort is to make it stable, which is desirable with objects, but not important with primitives, since they can't be distinguished anyway.

  • »
    »
    3 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    regarding option 1: You can sort in-place (without copying back and forth) using Collection.sort if you use something like this

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      This doesn't compile for me. Here's the error I get on Java 11 on Codeforces Custom Invocation:

      Error message

      Even if it did compile though, I'm almost positive that this is a massive footgun that nobody should ever use, because it makes you insanely prone to comparing Integer objects with ==, which is massively broken in java.

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Is there a generator that can hack long arrays? I seemingly could not find one suitable after some searching. Also why sorting int arrays and long arrays are different?

    • »
      »
      »
      21 месяц назад, # ^ |
      Rev. 4   Проголосовать: нравится +55 Проголосовать: не нравится

      It looks like the issue here is not that sorting int and long arrays are different, but that the first solution reads into an array of size n+1 rather than n:

              long[] num = new long[n+1];
              for (int i = 1; i <= n; i++) {
                  num[i]=in.nextInt();
              }
      

      To properly hack this, it suffices to modify the generator to print only the last $$$n$$$ numbers of the generated array for $$$n+1$$$.