voventa's blog

By voventa, history, 10 months ago, translation, In English

Thank you all for participating! 👊👊👊

2121A - Письмо домой

Tutorial
Code

2121B - Выше облаков

Tutorial
Code

2121C - Тем, кто с нами

Tutorial
Code

2121D - 1709

Tutorial
Code

2121E - Спонсор твоих проблем

Tutorial
Code

2121F - Ямакаси

Tutorial
Code

2121G - Гэнгста

Tutorial
Code

2121H - Ice Baby

Tutorial
Code
  • Vote: I like it
  • +110
  • Vote: I do not like it

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

speedyy voventa

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

    speedyy voventa

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

      Speedyy voventa

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

        Speedyy voventa

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

          speedyy voventa

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

            speedyy voventa

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

H code in the tutorial is smaller than my A's . Wth :/. nvm good questions

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

E can also be solved by randomly sampling 1000 numbers per testcase and seeing what the minimum score of these numbers is. Enjoy :D 324927463

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

For $$$E$$$, I generated $$$100$$$ random numbers $$$x$$$ in the range $$$[l, r]$$$ and took the minimum $$$f(l, x) + f(x, r)$$$ over all of them. I think that the worst-case probability that some $$$x$$$ works is $$$(\frac{4}{5})^9$$$ (or around $$$13\%$$$) because, in most cases, it seems like each digit of $$$x$$$ has a $$$\frac{4}{5}$$$ chance of not being equal to either of the corresponding digits of $$$l$$$ and $$$r$$$. So the probability that the solution fails a testcase might be $$$(1 - (\frac{4}{5})^9)^{100} \approx 5 \cdot 10^{-7}$$$.

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

    What a madlad

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

    Posting suspicious solutions in chat before the hacking round is over invites attention. :)

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

      Yeah lol I'm pretty sure my numbers are wrong. But even if they weren't, each test would still have a $$$\approx \frac{1}{200}$$$ chance of failing, so if someone were really dedicated, they could make it fail. Can I ask what your $$$l, r$$$ were?

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

        303120507 589565053

        Found through randomized search / offline simulation.

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

          basicallly any full 9 digit input

          with first digit at exact 2 gap, right?

          to maximise the randomness accidentally selecting any l_i or r_i from the remaining 8 digits?

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

          can you try hacking me?

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

          Interesting. I might be wrong, but it looks like $$$300\,000\,000 \,\, 599\,999\,999$$$ would be an optimal hacking testcase. That is because there is a $$$\frac{1}{3}$$$ chance of getting a $$$4$$$ (as opposed to a $$$3$$$ or a $$$5$$$), and then a $$$(\frac{4}{5})^8$$$ chance of satisfying all of the other digits. So the chance of any one $$$x$$$ producing the answer is $$$\frac{1}{3} \cdot (\frac{4}{5})^8 \approx 5.5\%$$$. So the probability of failing this testcase is $$$(1 - \frac{1}{3} \cdot (\frac{4}{5})^8)^{100} \approx 0.3\%$$$, which is really not good for $$$10^4$$$ testcases.

          And, unless there's a better testcase out there, I guess there's not a snowball's chance in hell you will be able to hack macaquedev, as $$$(1 - \frac{1}{3} \cdot (\frac{4}{5})^8)^{1000} \approx 10^{-25}$$$.

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

            Your analysis looks accurate to me.

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

            Can you share your intuition behind this method? Like, how did you even think this could work in the first place?

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

              yes — I just realised that given a random number, it's more likely NOT to match than to match — therefore if you generate 1000 numbers, you have a very very low probability of not getting an optimal number at least once. Also I just couldn't be bothered to solve it properly.

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

    Dk what u mean exactly but it sounds super cool!

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

      I mean exactly what I said. It's quite trivial to calculate score given l, r and some number x between l and r. You just implement what's written in the problem statement — compare each digit. Then, you generate 1000 random values of x, and with almost 100% certainty, at least one of them will have the minimal possible score. This is not hackable.

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

in problem D why simply sorting a and b and then swaping is enough. can someone explain me clearly why swaping in last will not violate our conditions .?

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

    Let $$$a$$$ and $$$b$$$ be the arrays after sorting, but before swapping.

    Then for any $$$i$$$, we have that $$$\min(a_i, b_i) \leq a_i, b_i$$$. But at least one of $$$a_i$$$ or $$$b_i$$$ must be smaller than $$$\min(a_{i+1}, b_{i+1})$$$, because one of them was placed before $$$\min(a_{i+1}, b_{i+1})$$$ whilst sorting. So we have that the mins are increasing.

    By similar reasoning, for any $$$i$$$, we have that $$$\max(a_i, b_i)$$$ is less than at least one of $$$a_{i+1}$$$ or $$$b_{i+1}$$$ because $$$\max(a_i, b_i)$$$ was placed before one of them whilst sorting. Since we have that $$$a_{i+1}, b_{i+1} \leq \max(a_{i+1}, b_{i+1})$$$, we have that the maxes are increasing.

    So we have that $$$\min(a_1, b_1) \lt \min(a_2, b_2) \lt \dots \lt \min(a_n, b_n)$$$ and $$$\max(a_1, b_1) \lt \max(a_2, b_2) \lt \dots \lt \max(a_n, b_n)$$$, so if we place all of the mins in $$$a$$$ and all of the maxes in $$$b$$$, both arrays remain sorted.

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

    After sorting, we have:
    - a[i] < a[i+1] for all i
    - b[i] < b[i+1] for all i

    Now, when we decide to swap because a[i] > b[i], we need to verify that the swap won't violate the sorted order of either array.

    Swapping b[i] into a:

    We're putting b[i] in place of a[i]. Since we originally had b[i] < a[i] < a[i+1], it follows that b[i] < a[i+1], so inserting b[i] into a won't break the increasing order in a.

    Swapping a[i] into b:

    This is more subtle. It's possible that a[i] > b[i+1], which would break the sorted order in b. However, since b[i+1] < a[i] < a[i+1], on the next iteration (i+1), we will again encounter a[i+1] > b[i+1], and thus perform another swap. This chain reaction will make sure that the arrays remain valid.

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

    simple proof for min(a[i], b[i]) < min(a[i + 1], b[i + 1])

    d1 = a[i + 1] - a[i], d1 > 0

    d2 = b[i + 1] - b[i], d2 > 0

    min(a[i], b[i]) < min(a[i] + d1, b[i] + d2) because both arguments of min have increased

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

DEF were easier compare to normal div 3 , Instantly knew solution of D,E here (this doesnt happen usually)

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

    how did you easily get D?

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

      u just had to make sure your operation wont exceed 1709 and max array size was 40. even at worst case you wouldnt need more than 780 swaps to sort single array , to sort both arrays 780+780 and if a[i]>b[i] then lets take 40 more operation they are within range , its brute force literally

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

        Oh! you are right this problem is simply a brute force. I just mistaken n*(n-1)/2 as n^2 and thought that it is not optimal to sort both array

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

      My solution was just to have odd numbers in a and even numbers in b, and move them one by one to the correct places in order, 1, 2, 3, etc.:

      1 3 5 7 ..
      2 4 6 8 ..
      

      My submission just simulates this process.

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

Too much implementation based questions, I think 2.5 hours for this would've been better, the explanations are pretty short which is fine when someone who is experienced is reading them, but for most people it might easily not come to them. Hope next div 3 is better

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

It's actually crazy how beautiful the solution to H is...

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

    can u explain it.. can't figure out ,been thinking 'bout it for like 2 hours straight. a strong hint is also welcome ,like don't explain everything but only what u feel is the critical optimization part.

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

Alternate solution for problem G:

Let dp[i] denote the sum of function f() over all substrings in prefix of i, i.e, $$$\sum f(s_l,s_{l+1},...,s_r)$$$$$$\forall$$$ l, r s.t. $$$1 \le l \le r \le i$$$. Let ans[i] denote the sum of function f() over all substrings ending at index i, i.e, $$$\sum f(s_l,s_{l+1},...,s_i)$$$$$$\forall$$$ l s.t. $$$1 \le l \le i$$$. Let pre[i] denote the prefix sum till index i, i.e, $$$pre[i] = \sum_{j = i}^{i} (s[j] == 1)$$$.

Final answer will be dp[n] and base case is dp[1] = 1 and ans[1] = 1 (assuming 1 based indexing).

Now suppose the $$$i^{th}$$$ character is a 1 (small change in sign when $$$s_{i} = 0$$$), then:

$$$dp[i] = dp[i - 1] + ans[i - 1] + \sum_{j = 1}^{i} (pre[i] - pre[j - 1] \gt i - j + 1 - pre[i] + pre[j - 1])$$$

$$$ans[i] = dp[i] - dp[i - 1]$$$

The summation equation basically accounts for all the +1's we do when attach $$$s_i$$$ to $$$s_{1, 2, ..., i-1}$$$.

We can rearrange the summation equation and then use (ordered) set optimization to quickly compute the dp :)

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

    can you explain the ans[i-1] + part? i dont get it.

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

      suppose s[i]='1' then ans[i] stores the answer for all substrings ending at i now you can see that this value wont change a lot from ans[i-1] it only changes when adding the last element to a subset increases the majority and this increase is only by 1 so you just have to add this number of subsets to ans[i-1] to get ans[i].

      so find all such subsets using the difference in count of 0 and 1

      let pref[i] be count(1)-count(0) till i;

      then at all previous places where pref[j] < pref[i] form the required answer; ordered set does it quickly

      similarly you can derive it for when s[i]='0'

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

    sumit sir :)

»
10 months ago, hide # |
Rev. 2  
Vote: I like it -15 Vote: I do not like it

.

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

Problem G has solutions for O(n)

I a bit overkilled this task and wrote this O(n) solution that uses 2 stacks to recalculate the answer using information about the answer on the previous prefix. If you are interested in analyzing it, here is the code: 324911499.

But I found a more interesting and simpler for understanding solution that reaches O(n), written by Kilo_5723. This solution implements the same logic as the author's, but counts the sum of modules of the prefix with asymptotic O(n), instead of O(nlogn): 324868540

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

    That's a clever approach. Thanks for sharing it.

    I wanted to point out that the tutorial's solution can also be made $$$O(n)$$$ trivially by using a counting sort instead of a comparison-based sort, since all the elements are between $$$-n$$$ and $$$n$$$.

    In fact, you can think of Kilo_5723's solution as doing an online counting sort: at the end of the loop, the array val contains a count of the elements of pref in the tutorial. The “online” part is that he updates the total for each character processed, which is why there are only additions/subtractions and no multiplications.

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

    https://mirror.codeforces.com/contest/2121/submission/325347841

    Here's mine also with O(n) time

    Seems a little simpler

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

      Could you please explain how it works?

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

        Basically, when you have the answer to some prefix that is n characters long, to find the answer to prefix that is n+1 characters long(either adding a 1 or 0), all you need to know is what ranges have what ever character added as the (n+1)th as the most common character or have both 1 and 0 as the most common character. We can have an array of size n*2 +1 and have a middle index and move it up or down depending on if a character is 0 or 1, for instance down if 0, up if 1 as the most we can move up or down is n. In this case in the array whatever index we start on we can increment it, and keep track of how many increments (or just the sum) of the array on the left and right sides of the 'middle' index as for all the increments that where previously done on the right side, if the new character is 0, that new character is increasing the value of those sequences as those subsets had 0 most common character. The inverse is true for the left side. This is like dp where we are using our previous answer and adding onto it, with each new encounter during our iteration through the list.

        https://mirror.codeforces.com/contest/2121/submission/326062731

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

Solution for problem E, using digit DP

324944536

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

When will be the ratings updated? today i gave my first contest so

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

    After the Hacking period is over, everyone's submissions will be finalized. I would expect them to be back by early morning EST.

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

Can anybody tell me why am I getting WA on test 2? 324959446 (Problem C)

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

Does anyone have another way to solve H? The editorial is too hard for me XD

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

    I simply used a Lazy Segment Tree; after compressing all values, let $$$\mathrm{dp}[k][v]$$$ be the maximum length of a non-decreasing subsequence using the first $$$k$$$ intervals and ending in $$$v$$$.

    Initially (for $$$k = 0$$$), the maximum length is always $$$0$$$. For a fixed $$$k$$$, all non-decreasing subsequences ending in $$$[0; l]$$$ can be "extended" by one by simply appending $$$l$$$ to it. At the same time, all subsequences ending in $$$v \in [l; r]$$$ can be "extended" by $$$v$$$; two operations that can be easily implemented for a Segment Tree!

    So in the end, you simply maintain a Lazy Segment Tree containing all possible ending values and update that for each step in $$$\mathcal O(\log n)$$$ time.

    Submission: 324887791

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

cute contest, my first time solving more than 3 problems lol.

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

2121C — Those Who Are With Us

In editorial, it is written Let mx be the maximum value in the matrix. Note that the answer to the problem will be either mx−1 or mx. Can anyone explain why?

I think this code fails on testcase like 1 3 3 1 2 3 4 1 2 4 1 2 According to the solution provided answer will be 3 but, correct answer will be 2.

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

    The 4 can only become a 3, and will then remain the max. You can use the decrement op at most once. How will you get a 2 max for this?

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

    Yeah.

    Suppose the max element in matrix is 9 and rest of the elements are 5.

    5 5 5

    9 5 5

    Now we can choose r=2 and c=1 and ans will be 7 but according to algorithm we get 8. if maxx=largest element in matrix and secondMaxx= 2nd largest then, I tried writing two diff cases for when maxx-secondMaxx>1 and maxx-secondMaxx==1.

    Even in the first test case

    1 1

    1

    The answer should be -1 right? After choosing r=1 and c=1. Can anyone please explain where I have gone wrong?

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

      heyy, Thanku for clarifying. You are right

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

      Answer will be 8, as we will -1 the maximum value that is 9 and get the answer as 8. The concept of secondMaxx is totally wrong and that make me also confused to make me comment.

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

      Yeah i got it now.

      We can only decrement aij once.

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

    The problem asks you to output the manimum possible maximum value. Meaning, you have to find out after doing one operation, what is the maximum value in the matrix? You have two cases, then

    1) Lets say the maximum number has a count of 1 in the entire matrix, thus, you can simply reduce it by 1 and that's your answer.

    2) If there are more than 1 count of the maximum in the matrix, then you will have to check if its possible to reduce all of them with one operation or not. If its possible, then the answer is mx — 1. If its not possible, then the answer is mx

    1 3 3 1 2 3 4 1 2 4 1 2

    In this test case, its possible to choose row = 2, and column 2 to reduce all the 4s to 3s. Thus, the final maximum value will be 3

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

Can anyone please tell why my solution is getting memory limit exceeded :(

https://mirror.codeforces.com/contest/2121/submission/324913725

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

    I dont see anything to worry about. I guess its from the vectors. I beilive they dont deallocate themselves. I dont know to help you with this one. Try long long a[] insead of vectors

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

    You're using inbuilt swap function which stores the elements it is swapping and since you're swapping around 1600 pairs worst case for every test case. for 100 test cases it will add and you'll have to store 160000 pairs which is causing the memory limit exceed. you can instead of using inbuilt swap function swap them manually and it should pass

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

      no bro, it is still not working

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

        your code is going on an infinite loop for the 1st test of test case 6. Possible issue according to me is that in the while loop you're checking if ac[i]==a[i] and then finding the jth index in ac but updating a instead which might create a cycle for a, and hence causing infinite loop.

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

170ms -> TLE ??? What the fucking original tests ???

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

Liked C

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

For problem F, I got a solution that uses sparse table + binary search. I thought that max[i -> x] for some x is monotonic. To my calculations, the time complexity should be O(n*log(n) + n*log^2(n)). But that gave tle on test 24 for some reason. Can anyone check my solution if I did calculate time complexity wrong or are there some bug in the code because I couldn't find anything https://mirror.codeforces.com/contest/2121/submission/324921228

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

hi voventa I have solved the problem F and it also accepted but after system testing it showing TLE(time limit exceed) on test case 24 why ?

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

The problem G is the same as https://www.codechef.com/problems/SUMFSUB. There's not even a single change. The problem is fairly recent as well. I tried putting the problem statement of G in http://yuantiji.ac/en/ and it did not show me SUMFSUB. I can understand that no one is at fault here, but we need more powerful tools so that this does not happen in the future.

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

I believe I have another approach for Problem D (324890440) where we construct the first row as:

$$$1, 2, 3, \ldots, N$$$

and the second row as:

$$$N+1, N+2, N+3, \ldots, 2N$$$.

Here's an analysis of the worst-case scenario for my solution:

Worst-case scenario input
  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    ItsNotMeItsYou I have also did the same thing in contest but got FST on system test-13 can you please tell me why it is failing My submission : My Submission

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

      You use operation 3 in the final step; I do it first.

      • »
        »
        »
        »
        10 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it
        #include <bits/stdc++.h>
        using namespace std;
        
        #define ll long long
        #define vi vector<int>
        #define vll vector<long long>
        #define vpii vector<pair<int,int>>
        #define vpll vector<pair<long long,long long>>
        #define pii pair<int,int>
        #define pll pair<long long,long long>
        #define mod1 1000000007
        #define mod2 998244353
        #define INF 1e18
        #define pb push_back
        #define ppb pop_back
        #define mp make_pair
        #define ff first
        #define ss second
        #define sz(x) ((ll)(x).size())
        #define all(x) (x).begin(), (x).end()
        #define rep(i,a,b) for(ll i=(a) ; i<(b) ; i++)
        #define all(x) (x).begin(), (x).end()
        #define yes cout<<"YES\n"
        #define no cout<<"NO\n"
        #define nl '\n'
        #define read(a) for(auto &it : a) cin>>it;
        #define printv(nums) for(int i=0;i<nums.size();i++) cout<<nums[i]<<" "; cout<<"\n";
        
        #ifndef ONLINE_JUDGE
        #include "template.cpp"
        #else
        #define debug(...)
        #define debugArr(...)
        #endif
        
        void solve()
        {
            int n;
            cin >> n;
            vi a(n),b(n);    
            read(a);
            read(b);
            vpii ans;
            for(int i=1;i<=2 * n;i++)
            {
                int idx = -1;
                char cur = 'a';
                for(int j=0;j<n;j++)
                {
                    if(a[j] == i)
                    {
                        idx = j;
                        cur = 'a';
                        break;
                    }
                }
                if(idx == -1)
                {
                    for(int j=0;j<n;j++)
                    {
                        if(b[j] == i)
                        {
                            idx = j;
                            cur = 'b';
                            break;
                        }
                    }   
                }
                int reqidx = (i <= n) ? i - 1 : i - n - 1;
                char reqA = (i <= n) ? 'a' : 'b';
                if(cur != reqA)
                {
                    swap(a[idx],b[idx]);
                    ans.pb({3,idx+1});
                }
                while(idx > reqidx)
                {
                    if(cur == 'a') ans.pb({1,idx});
                    else ans.pb({2,idx});
                    if(cur == 'a') swap(a[idx] , a[idx-1]);
                    else swap(b[idx] , b[idx-1]);
                    idx--;
                }
                while(idx < reqidx)
                {
                    if(cur == 'a') ans.pb({1,idx+1});
                    else ans.pb({2,idx+1});
                    if(cur == 'a') swap(a[idx] , a[idx+1]);
                    else swap(b[idx] , b[idx+1]);
                    idx++;
                }
            }
            cout << sz(ans) << nl;
            for(auto &[x,y] : ans) cout << x << " "<< y << nl;
        }
        
        
        signed main()
        {
            ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
            freopen("Error.txt", "w", stderr);
        
            int t = 1;
            cin >> t;
            for (int i = 1; i <= t; i++)
            {
                solve();
            }
        
            return 0;
        }
        

        Now it is taking more operations

        On that tc:13 previously it was taking 1810 op but not 2160

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

I like problem F

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

Great contest and enjoyable problems!

A modified approach for Problem F. Observe that the array can be split into blocks by elements greater than $$$x$$$ (non-inclusive), i.e. $$$a = [l_1...r_1, g_1 \gt x, l_2...r_2, g_2 \gt x, ..., l_s...r_s]$$$. There will always be at least one block. Each block, likewise, can be split into sub-blocks (at least one) by elements that equal to $$$x$$$ (non-inclusive). If we count subarrays with the desired sum $$$s$$$ in a block, it will have all subarrays with a maximum of $$$x$$$ in that block and some subarrays which don't contain any maxima. These additional subarrays correspond to sub-blocks. So the answer is the sum of the count of subarrays with the desired sum $$$s$$$ across all blocks minus the sum of the count of subarrays with the desired sum $$$s$$$ across all sub-blocks in every block.

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

I felt that the editorial for problem B was a bit too convoluted. I suggest the following solution:

OBSERVATION:

1) String b, if it exists, will never constitute the first or last letters.

2) The minimum length of string b will be 1, and all possible string values b can take, if present, can be extended from the 1 you obtain, if present.

ALGORITHM:

Create a list containing the counts of all alphabets, and increment accordingly for each instance. Then from i=2 to i=n-1(in 1-based indexing), iterate through the string and see if any character in there has a count more than 2. If you find a character that has a count of more than 1, you can break and report YES. Else, after the iteration, report NO.

This is because as soon as you find such an alphabet, you can directly make that alphabet string b and the remaining string will have that character.

Please feel free to point out if I have missed anything.

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

Overthinking did me a lot of harm in BCD...

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

Hi voventa, can you please add this blog to the contests materials? I didn't know this blog before I open the announcement that editorial is out

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

Can you please link the editorial to the problems also?

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

The (very neat) idea for problem H can also be used to solve this problem, and the code is not so different

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

Very nice contest!!!

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

Hi,i am new here. I solved it but in virtual contest, how to calculate the points i got? I solved problems A,B,C? Thanks

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

In the C++ solution code for Problem F (Yamakasi), why does using unordered_map instead of map cause TLE?

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

    I guess cause clearing an unordered_map is more time consuming than clearing a map.

    Map: Red-black tree

    Unordered map: Hash table

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

Why does my solution 326223855 for F is giving tle despite being nlogn? Update : Missed the auto& it.

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

G can also be easily solved with a segment tree. First, get the sum of max counts for every substring containing the number at index 0. Keep track of the number of positive and negative relative one/zero counts for every substring. Then, we can remove the numbers from the left one by one. If we remove a 1, then decrease our result by the number of positive net sums and decrease all net sums by 1. If we remove a 0, then decrease our result by the number of negative net sums and increase all net sums by 1. Then the answer is the sum of each result after removing each number from the left n times.

Code: 326714584

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

I am so stupid that I cannot solve this problem in 20min and I got 'Wrong Answer on test 13' using about 1h.
$$$\color{red}{\texttt{Bad.}}$$$

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

Here is my O(n) solution for problem G.

    int n;
    cin>>n;
    string s;
    cin>>s;
    vector<stack<pair<int,int>>>delta_cnt_pairs(2);
    vector<int>exceed_cnt(2);
    int ans=0,sum=0,eq=0;

    for(auto i:s){
        int x = i-'0';
        int y = x^1;

        delta_cnt_pairs[x].push({1, eq + 1});
        exceed_cnt[x]+=eq + 1;
        eq=0;

        if(!delta_cnt_pairs[y].empty()){
            auto t=delta_cnt_pairs[y].top();
            delta_cnt_pairs[y].pop();
            if(t.first == 1){
                eq = t.second;
                exceed_cnt[y] -= eq;
            }
            else{
                delta_cnt_pairs[y].push({t.first - 1, t.second});
            }
        }
        sum+=exceed_cnt[x];
        ans+=sum;
    }
»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

https://mirror.codeforces.com/contest/2121/submission/327441927

for which test case is this failing? can anyone help me figure it out?

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

in problem C, what about the case:

3 2 2 2 1 1 2 1 1

we can choose r=1, c=1 which will result in:

1 1 1 1 1 1 1 1 1 ?

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

proof for problem D solution -> (to ensure after sorting both a and b , if we do operation 3 on each index from 0 to n-1 => it doesn't disturb the sorting order of a and b)

we sort both a and b, now performing operation 3 on each index - let say first index where a[i] > b[i] is i = x => a[x] > b[x], then a[x-1] < b[x-1] => a[x-1] < b[x] (since, b[x-1] < b[x])

a[x + 1] > a[x] => a[x + 1] > b[x] so now if we swap a[x] and b[x] => a[x-1] < b[x] < a[x + 1] thus satisfying sorted ordering of a

in the same way b[x-1] < a[x] < b[x + 1] satisfying sorted ordering of b also

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

    Hey Nipun! Thanks, buddy. Yours is the only solution that I can understand (the math that made me understand it). Been thinking about the problem for some time, and I could not get to a mathematical proof of this. Thanks again.

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

Did anyone solve G using divide and conquer? I feel like it is possible but I am struggling with the implementation.

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

damn good editorial

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

Problem H also can just be solved with segment tree beats, submission: https://mirror.codeforces.com/contest/2121/submission/358456170

»
4 weeks ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it
Simpler implementation for Problem E
»
17 hours ago, hide # |
 
Vote: I like it 0 Vote: I do not like it