reirugan's blog

By reirugan, 16 months ago, In English

I hope you all enjoyed the contest!

Rating Predictions
Rate the contest!

A — Blackboard Game

Solution
Rate the problem!

B — Tournament

Solution
Rate the problem!

C — Prefix Min and Suffix Max

Solution
Rate the problem!

D — Binary String Battle

Solution
Rate the problem!

E — MEX Count

Solution
Rate the problem!

F — Minimize Fixed Points

Solution
Rate the problem!

G — Modular Sorting

Solution
Rate the problem!
  • Vote: I like it
  • +292
  • Vote: I do not like it

| Write comment?
»
16 months ago, hide # |
 
Vote: I like it +12 Vote: I do not like it

Pen,Paperforces round

»
16 months ago, hide # |
 
Vote: I like it +38 Vote: I do not like it

Bro dropped the editorial at the speed of light. Very refreshing contest btw!!

»
16 months ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

Fun fact: As a tester, testing for this contest was stopped once due to me.

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

    Context for those who are curious: it turned out one of my problems was, up to a cosmetic rewording, a duplicate of a past ARC problem. beaten_by_ai luckily remembered the problem and it was removed.

    https://atcoder.jp/contests/arc147/tasks/arc147_e

    Initially, MEX Count was D (with a subtask to solve for one $$$k$$$) and Minimize Fixed Points was E, with the AtCoder problem placed at F. After it was removed, DE were moved to EF and Binary String Battle was added.

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

      ahaa, thats why F is a bit easy i believe??

    • »
      »
      »
      16 months ago, hide # ^ |
      ← Rev. 2  
      Vote: I like it 0 Vote: I do not like it

      Note that the original problem is rated approx. $$$\color{orange}{2476}$$$, which roughly corresponds to $$$\color{red}{2585}$$$ in CodeForces. Therefore, we could have witnessed the hardest div3F in the history.

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

Thank you for the contest and the fast editorial!!!

»
16 months ago, hide # |
 
Vote: I like it +33 Vote: I do not like it

assert(number_theory == magic);

»
16 months ago, hide # |
 
Vote: I like it -10 Vote: I do not like it

editorial drops faster than brainrot memes hitting my youtube page

»
16 months ago, hide # |
 
Vote: I like it +28 Vote: I do not like it

Am I the only one who got stuck on D and solved E? Very nice contest by the way, kudos

»
16 months ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

problems were really interesting thank you for the great round!

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

This is the best div3 E i've ever seen

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

What could be the expected rating of the problems? ig A-B-C seems like a standard 900,1100, and 1200 rated. D was tricky but code is short so it can't be thought of more than 1400. E probably felt like 1500-1600. I don't know about F and G tho. What do you guys think?

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

    A: 800 B: 800 C: 800 D: 1200 E: 1400 F: 1600 G: 2000

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

    For A and B, I think they are ~800. C should be ~1000 (I think I got WA before AC because I had no idea what I was doing). D is really wonky and is not obvious, so I think it should be ~1300. E is not bad but requires a bit of work, so 1500~1600. F is straight-up divisor checking, so ~1600. G I didn't solve, so I just assume it's ~2000.

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

In D : Step 3 -> Take the example given after Bob gets s=00101111 Alice could convert this to s=00000010 and eventually win. I don't see how Bob wins this.

The strategy I see is Alice tries to make the array of the for 10001000/01000100 etc , and wherever Bob tries to reverse digits she flips back the ones Bob flips additionally being able to take care of extra ones. Hence, even if n>2k she can win.

What's wrong?

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

    nevermind Bob would pick the newly found full 0 string , nice contest :)

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

Contest without graphs and dp after long time. Kudos to authors.

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

Clean editorial, easy to understand.

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

Although i was not able to solve F, it was a beautiful problem and worth spending my time on. Thank you for the contest

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

    I solved F by calculating smallest prime factor of every number and then performing a greedy algorithm for prime numbers.

    For me, E was way harder lmao (any hint for E?)

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

      you can think of difference array technique to solve the problem. Also count of element only matters .

    • »
      »
      »
      16 months ago, hide # ^ |
      ← Rev. 2  
      Vote: I like it 0 Vote: I do not like it

      I was able to get E because i have done somewhat a similar question before. But here are some hints. I believe the editorial has a different solution to mine.

      Spoiler 1
      Spoiler 2
      Spoiler 3
»
16 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Great Contest, except D was horrible.

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

https://t.me/placementsoftware reirugan this is a telegram channel where answers were getting released during the contest, please check this and use it during the plagiarism check to ban the contestants who have used the exact same code in their submission.

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

As always, please report cheaters on https://cf-cheater-database.vercel.app/.

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

can anyone please tell me how alice will win if n=5 k=3 and s = 11111

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

can someone pls help me understand where I am going wrong in G? I am at a loss[submission:326909800]

https://codeforces.me/contest/2123/submission/326909800

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

Strange how all Problem A solutions match. Is this collective intelligence or something else?

»
16 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
Spoiler
»
16 months ago, hide # |
← Rev. 5  
Vote: I like it 0 Vote: I do not like it

Can someone please explain D to me, especially step 3 in the editorial. I was able to come to the point where I observed that 1. If n is odd and the middle element is already 1 and rest count of 1 is at max k then its always possible for Alice to Win. 2. If n is even and middle 2 elements are 1 and rest count of 1 is at max k then again Alice can win. Basically I understood the reverse of step 2 that if I can force Bob to only be able to make a sequence of length exactly 'k' then Alice can win. Also k == half the length of string in above 2 cases.

But this condition, n < 2*k seems like magic to me.

Basically how to logically go from Step 2 -> Step 3 My Submission: https://codeforces.me/contest/2123/submission/326981142

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

    you can think of it like this : in order for Alice to win , she needs at some point to make all the remaining ones included in all the substrings , so that whatever Bob does he can not get the full +k ones at his turn .And when we say included in all the substrings they must also be included in the first substring of length k . So we must ensure that when we divide the string into first (k) and last (n-k) elements , we should be sure that the ones in the first substring can be included in all other substrings , so (k) should be greater than (n-k) , reformulating : k>n-k --> 2*k>n is a necessary condition for Alice to win . hope it helps:)

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

      Thanks, this idea that "all remaining ones must be present in all substrings" was the key idea I believe. I re-interpreted it in my own way. Basically, I need to ensure that in the worst case (odd length with 1 in the middle) too I am having a 'k' such that the first substring and last substring can cover it. This is only possible if they overlap, which means: length of first substring + length of last substring > n 2*k > n (since both are equal to 'k')

      If my 'k' fulfills this condition then and count of 1`s > k then Alice always has a chance to win if she plays optimally.

      Thanks a lot!! I am new and my maths is weak. How can I perform better on such problems?

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

I absolutely love the G problem. After reading the editorial, it just seems so obvious

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

the sol $$$O(tn^2)$$$ for prob A, 326827179

(What tf was I thinking?)

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

I was looking awoo 's Solution. for Problem E . I dont have any idea how this simple logic work, some. kind soul please help me. :

vector<int> cnt(n + 1);
for (int x : a) ++cnt[x];
vector<int> ans(n + 2);
forn(i, n + 1){
	int l = cnt[i];
	int r = n - i;
	if (l <= r){
		++ans[l];
		--ans[r + 1];
	}
	if (cnt[i] == 0){
		break;
	}
}
forn(i, n + 1) ans[i + 1] += ans[i];
forn(i, n + 1) cout << ans[i] << " ";
  • »
    »
    16 months ago, hide # ^ |
    ← Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    Think of it like marking the start and end of the increment, and the prefix sum “spreads” the value across the entire range. https://codeforces.me/blog/entry/78762

    Btw, if you're asking about the full solution, try thinking about the problem in reverse: for each i, check whether it's possible to make the mex = i, and determine the minimum and maximum number of elements to remove to achieve that. (step 4 in this editorial)

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

    ohh look what he is calulating for each possible mex he is calculating 2 values one is what is min number of element we need to remove to get ans as i and what is max number of value we ca remove get ans as i now for getting some value as min value is just its frequency and max value you can calculate first remove everyhting that is >= element . now when a mex is not possible we break now for each i we know we can get value as i for some k belongs to l to r then we just to linear sweeping between l to r incraesing value at l by +1 and value at r by +1 and doing prefix array sum

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

D was quite tricky, E,F were much easier

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

Great contest and balanced questions! Had that nostalgic codeforces feel many contests these days don't

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

can anyone please explain why does my code exceed time limit in testcase 30 (326998284)

I'm using an array for count which should be better than a map as provided in the solution

»
16 months ago, hide # |
← Rev. 2  
Vote: I like it +1 Vote: I do not like it

Alternative solution to F:

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

Anybody body know what is wrong with this? https://codeforces.me/contest/2123/submission/326994630, stops on test 13?

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

Can someone help me find why my greedy approach is wrong for d . Basically i am greedily converting the outermost k 1's of the string to 0 , and then checking if i can get a substring of length k+1 . Here's my submission — 326923441

my idea was that if bob can make k+1 consecutive 1 then he always win

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

    Your approach is incorrect. Consider the following example:

    n = 5, k = 3 s = "11111"

    After Alice's first move—if she plays optimally—the string will become "01010". Now, Bob is left with a maximum substring of consecutive zeroes of length 1, so he can turn at most 3 characters into '1'. On her next move, Alice can easily turn all the characters back to '0'.

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

i had pretty simple approach on F!!....

https://codeforces.me/contest/2123/submission/326973038

I just swap x with its largest divisor!

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

I only came up with C with 1 minute left xD Don't know when am I finally touching Ds

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

Am I the only one, who solved problem E using Merge Sort Tree? Because when you realise, that 0 <= i <= min(n-k, mex) and freq(i) <= k it turns out to be a straightforward Merge Sort Tree task (and I couldn't figure out a different solution)

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

For question F , I just iterated from n to 1

and swapped it with the max divisor

int largest_divisor(int n) {
    if (n <= 1) return n;
    for (int i = sqrt(n); i >= 2; --i) {
        if (n % i == 0) {
            return n / i;
        }
    }return 1;
}
int solve(){
    ci(n)vector<int> a(n+1);
    for( int i= 1;i<=n;i++){
        a[i]=i;
    }
    for(int i=n; i>=0;i--){
        int largest = largest_divisor(i);
        if(largest == 1) continue;  
        swap(a[i],a[largest]);
    }
    for(int i=1;i<=n;i++){
        cout << a[i] << " ";
    }
    cout << endl;  
    return 0;
}

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

Writing Solutions step wise is even more confusing . BEA!

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

Editorial of G seems too much complicated to understand ? Is anyone able to make it more simpler ?

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

    I tried chat GPT.

    It helped.

    Any way, I am getting WA but can't tell whats wrong in my code

    327149170

    Can some one lend some brain power?

    Thanks

    • »
      »
      »
      16 months ago, hide # ^ |
      ← Rev. 5  
      Vote: I like it 0 Vote: I do not like it

      Found one silly mistake

      Now on TLE on test 23 327273197

      After removing all binary data structures, and using global data stores

      Now i achieved TLE on test 29 327276612

      reirugan is there any special reason why input constrain are so tight?

      My way to calculate divisors was innefficient, still normal code with STL's binary structure based data type like map, vector::push_back() gave TLE :)

      I had to re write code to optimize time with so many unconventional ways. this seem so unnecessary it also waist lot of compute resources of codeforces engines.

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

        I think five seconds is pretty lenient? Some solutions ran as fast as 312ms.

        • »
          »
          »
          »
          »
          16 months ago, hide # ^ |
          ← Rev. 2  
          Vote: I like it 0 Vote: I do not like it

          My fully optimized code ran in 1200ms

          beyond this I can only think about rewriting input reader myself. and precomputing gcd? (not sure if that is possible)

          Thanks for lovely problem though, enjoyed every bit brain fuck, and connecting dots to understand solution.

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

          saw one of those solution they optimized by scaling down possible divisors , by only focusing on divisors based on query input.

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

            True, but even the solution codes in the editorial both run in around 2.5 seconds, and they are rather unoptimized (using map, for example). I think it's fair to set the time limit to double that.

  • »
    »
    16 months ago, hide # ^ |
    ← Rev. 4  
    Vote: I like it +1 Vote: I do not like it

    Key observation is

    1. $$$(a + (k.n))\mod{m}$$$ where $$$n\in{N}$$$, can be written as $$$a\mod{gcd(k,m)}$$$

    2. possible values you can transform $$$a_i$$$ for given any $$$k$$$ is congruent to $$$a_i\mod{gcd(k,m)}$$$

    3. for any value of $$$k$$$, $$$gcd(k,m)$$$ will be a divisor of $$$m$$$ which means there are only less than $$$800$$$ values possible for divisors (given $$$m \lt 5\times{1e5}$$$)

    4. For each divisor you can keep array $$$a$$$ transformed into $$$a_i\mod{d|m}$$$

    5. you can keep precomputed sum of all arrays (arrays based on divisor of m). and determine answer.

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

Question G — Could someone please explain why ai(mod(gcd(k, m)) is the invariant across the operations? Where did gcd(k, m) come from?

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

    Let x=gcd(k,m). Since x divides k, adding k keeps ai invariant mod x because k is congruent to 0 mod x. Similarly, taking ai modulo m keeps ai invariant modulo x because x divides m.

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

    Where did gcd(k, m) come from?

    I like to imagine in term of grid, picture m=10, k-5, ai=3

    ___#______
    

    keep doing ai + k for few times and you get

    ___#____#____#____#____#____#____#____#____#____#_
    

    now see how would (ai + k) mod m would look, by wrapping this list every 10 chars

    ___#____#_
    ___#____#_
    ___#____#_
    ___#____#_
    ___#____#_
    

    You can see ai + k only ever return in 2 possible values, if you look at these values mod gcd(10,5), i.e. mod 5

    ___#_
    ___#_
    ___#_
    ___#_
    ___#_
    ___#_
    ___#_
    ___#_
    ___#_
    ___#_
    

    you can see ai mod (gcd(k,m)) is invariant. that means you can only set ai to ai mod (gcd(k,m)) being smallest possible value and take gcd(k,m) steps at a time.

    if gcd(k, m) is 1 you can make ai any value, try to picture same with k = 7.

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

why gcd(k, m) in problem G?

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

This should be the gold standard for editorial quality. Feedback for problem quality and difficulty and the solutions explained in collapsible steps. I also really liked the tester rating predictions. I don't expect all editorials to be of this quality, but I certainly appreciate it!

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

We can optimize the solution for problem E using a difference array. Let cnt[x] be the frequency of value x. For x < mex, it contributes to answer k iff cnt[x] ≤ k ≤ n − x − 1.

Each x defines a valid interval on k. We add +1 on [cnt[x], n − x − 1] using a difference array, then take prefix sums.

This avoids sets/maps and runs in O(n) time with O(n) memory instead of O(nlogn) time complexity.

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

Solution for Problem C — Prefix Min and Suffix Max:

Intuition: If an array element is either the minimum element of its prefix or the maximum element of its suffix (or both), then we can reduce the whole array down to that single element using the given operations. Otherwise, we can't. For example, let's say the array looks like this:

a = [13, 10, 12, 20]

Let's look at a couple of example elements in this array. First, let's define a[i..j] to be the contiguous subarray from index i to j inclusive.

For the third element, a[2] = 12, its prefix is a[0..2] = [13, 10, 12] and its suffix is a[2..3] = [12, 20], so it is neither the min of its prefix or the max of its suffix, so we output 0 for this element.

For the first element, a[0] = 13, its prefix is a[0..0] = [13] and its suffix is a[0..3] = [13, 10, 12, 20], and it is the min of its prefix. So we output 1 for this element.

Why does this idea work? Let's look at the case where an element is the minimum of its prefix first:

Theorem 1: If an element x is the minimum of its prefix, then we can reduce the array down to x.

Proof: Let's suppose we have any array a. Also, assume that a[i] = x and it is the min of its prefix a[0..i]. Then we can use the prefix operation to replace this prefix by x. After this there two possible cases:

  1. The array a is just x by itself, in which case we are done.
  2. The array a consists of x followed by at least one element. In this case, we can apply the suffix operation on all the elements to the right of x. Now the array becomes [x, y] where y is some other element from the original array. Since all array elements are distinct we know that either x > y or x < y. If x > y we can apply the suffix operation to the whole array to end up with x by itself. Similarly, if x < y, we can apply the prefix operation.

One can make a similar argument to the above when an element is the the maximum of its suffix.

The only remaining thing we need to prove is that if an element is neither the minimum of its prefix nor the maximum of its suffix, then we cannot reduce the array to that single element:

Theorem 2: If an element x is neither the minimum of its prefix nor the maximum of its suffix, then we cannot reduce the array down to x.

Proof: Suppose we have an array a and an element a[i] = x in the array which is neither the minimum of its prefix nor the maximum of its suffix. Then x cannot be the first or the last element in the array (why?). So we have an array that looks like this:

a = [..z..x..y..]

where z is the minimum of the prefix of x and y is the maximum of the suffix of x. Here the ".." just means any other remaining elements (possibly zero).

We can show that we cannot reduce the array a down to the element x by itself:

Suppose start with some operation. This operation cannot include the element x because then we would eliminate x. If it is a prefix operation it cannot eliminate z. If it is a suffix operation it cannot eliminate y (why?).

So after the first operation z is still to the left of x and y is still to the right of x. Now we can repeat the same argument for any operation following the first one.

But eventually we must include x in some operation, otherwise we cannot reduce the array a down to just x. But if we do this, we will eliminate x since if it's a prefix operation it will include z and x (in which case x will be eliminated since z < x), and if it's a suffix operation it will include y and x (because y > x).

So we get a contradiction because in order to reduce the array down to x by itself, we must eventually include x in some operation, but if we do this we must eliminate x.

Implementation:

Below is the implementation of the solution in Java. The printSolution function contains the main algorithm for the problem. For the implementation we do two scans: One from left to right to check whether each element is the minimum of its prefix, and one from right to left to check whether each element is a the maximum of its suffix. During the second scan we check for each element whether it is a min of its prefix or max of its suffix and output the solution value for that element accordingly.

      import java.util.*; 
 
      public class Codeforces
      {
         public static void main(String[] args)  
         {  
           Scanner scanner = new Scanner(System.in);
           int t = scanner.nextInt();
           scanner.nextLine();

           for(int testCase = 0; testCase < t; testCase++)
           {
               int n = scanner.nextInt();
               scanner.nextLine();
               int[] a = new int[n];

               for(int i = 0; i < n; i++)
               {
                   a[i] = scanner.nextInt();
               }

               printAnswer(a, n);
               scanner.nextLine();
           }
         }

         public static void printAnswer(int[] a, int n)
         {
             int min = a[0];
             boolean[] isMin = new boolean[n];

             for(int i = 0; i < n; i++)
             {
                 min = Math.min(a[i], min);

                 if(a[i] == min)
                 {
                    isMin[i] = true;
                 }
             }

             int max = a[n - 1];
             StringBuilder answer = new StringBuilder();

             for(int i = n - 1; i >= 0; i--)
             {
                 max = Math.max(a[i], max);

                 if(a[i] == max || isMin[i])
                 {
                    answer.append("1");
                 }
                 else
                 {
                    answer.append("0");
                 }
             }

             System.out.println(answer.reverse().toString());
         }
      }

 

»
4 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For Question F, can we say that the minimum number of fixed points in such a permutation is the number of prime numbers greater than n/2 + 1(This one comes from the number 1 itself)