karan_garg_12's blog

By karan_garg_12, history, 8 months ago, In English

Today, finally for the first time in history, I got a test case to hack the problem 2155D - Batteries.

But the contest had already ended :(

The test case is actually easy:

(n = 40, a = 4) and the active positions are (1,14,27,40). It should be solved in under (400) operations, but everyone who wrote code that checks by iterating over the gaps between the batteries will get the result in approximately (403)–(404) queries.

One day I'll also hack, today I got the motivation.

UPDATE — It's only for those who did not do cyclic test. (Including tourist)

  • MLA19 Do add this test case for later testing in practice.
  • Vote: I like it
  • +162
  • Vote: I do not like it

»
8 months ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

The tests are bad IMO. People wrote O(t * m) code in E which should TLE but passes

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I think you mean about $$$O(n^2)$$$, yeah that's the code I'm talking about. But that is actually smart, The code will always give the output in approx the same steps as desired. But yeah approx doesn't always mean less than equal to right :)

    • »
      »
      »
      8 months ago, hide # ^ |
       
      Vote: I like it +3 Vote: I do not like it

      No I mean that in Problem E

      People are doing this

      if (n > 1){
              int ans = 0;
              for (int i = 2; i <= m; i++){
                  ans |= col[i];
              }
              cout << (ans ? "Mimo\n" : "Yuyu\n");
          }
      

      This for loop over O(m) but there is no bound on sum of m so its TLE

      • »
        »
        »
        »
        8 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        Oh, The cases were really weak, man.

      • »
        »
        »
        »
        8 months ago, hide # ^ |
        Rev. 2  
        Vote: I like it +8 Vote: I do not like it

        Not necessarily. 2e9 operations is large, but every one of them is a single OR operation. Time limit is 2 seconds. If we assume one cpu cycle per OR instruction, no compiler optimisation, and 1GHz CPU 2e9 OR operations pass. But codeforces most probably uses faster CPUs, and GCC definitely vectorized the OR operation here because it's GCC it likes doing black magic optimisations. So it should easily pass. Bitwise operations are just way too fast.

»
8 months ago, hide # |
Rev. 2  
Vote: I like it +3 Vote: I do not like it

The array is cyclic, so when your step size is 1, (40+1) gives you a pair (40,1) to check, hence the code that you're talking about will ask exact 40 queries.

run this code to try out different cases, i have currently used your case with my code.


#include "bits/stdc++.h" using namespace std; int32_t main () { int n=40; int q = 0; set<int>st; st.insert(1); st.insert(14); st.insert(27); st.insert(40); auto ask = [&] (int x, int y) -> int { q++; return (st.count(x) and st.count(y)); }; for(int step = 1;step<n;step++){ for(int i=1;i<=n;i++){ int j = (i + step); if(j>n)j-=n; int v = ask(i,j); if(v) { cout<<q<<endl; return 0; } } } cout<<q<<endl; }
»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

We can get the answer here in 40 queries right, because we will get the pair (1, 40) in n iterations for checking the gap length = 1

»
8 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

TLDR: those who only asked for (i,j) such that j = i — step , for a step size = step.

and did not consider the pair(i, (i+step)%n+1) , should fail this case mentioned. (tourist is one of them btw).

»
8 months ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

It wouldn't have mattered whether the contest ended or not :(. Hacks were disabled for the problem from the start...

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Uphack when?

»
8 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

In this testcase, tourist code is finding the answer in 403 operations [1, 14, 27, 40]. It supposed to be find in under 400 operations. Please check on this MLA19

»
8 months ago, hide # |
Rev. 3  
Vote: I like it +6 Vote: I do not like it

I solved the problem in a rather different way from every other solution I have seen; mine seems to get your sample test case in 146 tries, but I am not entirely convinced that my method always works regardless (I ended up submitting right at 1:59:59 and fortunately didn't have any typos, but I didn't have the chance to fully prove to myself that my solution is sufficiently optimal).

What I did:

Spoiler

342117601

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    I didn't fully understand your approach, but mine was also guessing in approx 146 queries. My approch was to divide the array in first n/2 blocks then check each block if that block separately have the pair or not, if not now divide the array in n/4 blocks and so on. I got this approach with the idea of pigeon hole principal.

    • »
      »
      »
      8 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it +3 Vote: I do not like it

      I revised my explanation a bit so that it is hopefully a bit easier to understand. It sounds like I do the same thing as you but in reverse (instead of starting with two blocks and splitting, I start with $$$n$$$ blocks and merge).

      Edit: actually, it sounds like you go bottom-up, so we are doing the same thing (I just had a messy implementation).

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    your solution is correct runs in roughly $$$O(\frac{3n^{2}}{4a})$$$ queries

    • »
      »
      »
      8 months ago, hide # ^ |
       
      Vote: I like it +1 Vote: I do not like it

      nevermind, your case fails this case:

      n = 19, a = 3

      active positions: (1, 6, 14)

      query limit is 120

      your code takes 121 queries

      • »
        »
        »
        »
        8 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        Out of curiosity, is my issue the fact that I wait to use the leftover ones at the end instead of just merging them with the next size up immediately?

        • »
          »
          »
          »
          »
          8 months ago, hide # ^ |
           
          Vote: I like it +1 Vote: I do not like it

          yes

          • »
            »
            »
            »
            »
            »
            8 months ago, hide # ^ |
             
            Vote: I like it 0 Vote: I do not like it

            I most definitely panicked when trying to implement at the end and made a bad last-minute decision to add the separated merging in... oops.

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Exactly the same as yours!

  • »
    »
    8 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    I wrote a similar solution in the contest and it passed, but after the contest I found it was not correct by doing stress tests.

    FYI, my solution fails with the following case: $$$n=9$$$, $$$a=3$$$, positions $$$\{ 4, 8, 9 \}$$$, max allowed queries $$$27$$$, my solution makes $$$28$$$ queries.

»
8 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

I think this could be avoided is the interactor uses an approximate algorithm on MIS.