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

Автор Evirir, 21 месяц назад, По-английски

2049A - MEX Destruction

Idea & preparation: Evirir

Tutorial
Solution

2049B - pspspsps

Idea & preparation: Evirir

Tutorial
Solution

2049C - MEX Cycle

Idea and solution: Evirir and Kaey
Preparation: Evirir

Tutorial
Solution

2049D - Shift + Esc

Idea: Evirir and Kaey
Solution: CSQ31, Evirir, Kaey
Preparation: Evirir and CSQ31

Tutorial
Solution

2049E - Broken Queries

Idea & preparation: Evirir
Solution: Kaey

Tutorial
Solution

2049F - MEX OR Mania

Idea & preparation: YouKn0wWho

Tutorial
Solution
Разбор задач Codeforces Round 994 (Div. 2)
  • Проголосовать: нравится
  • +89
  • Проголосовать: не нравится

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +29 Проголосовать: не нравится

first.

B was brutal

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

second.

E was interesting.

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

B broke my back

»
21 месяц назад, скрыть # |
Rev. 2  
Проголосовать: нравится +13 Проголосовать: не нравится

Third.

I will quit competitive programming...

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +23 Проголосовать: не нравится

b was absolute shit

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

I didn't really learn anything from this round. B was either brute forced implementation(like i did) or editorial's edge case heavy direct implementation, C was just observations derived by dry running mutliple cases. Yesterday's global round had a better C.

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +26 Проголосовать: не нравится

For F, my solution does actually do the updates in the normal order. I maintained for each power $$$k$$$ a set of ranges, where each range stores a frequency array of size $$$2^k$$$, as well as a multiset of the lengths of valid ranges (to get the answer). A range is valid if every element in its frequency array is nonzero.

Then each update either modifies or splits $$$\text{log} \, n$$$ ranges. For each power $$$k$$$, find the range that position $$$i$$$ is contained in (or skip if it doesn't exist). If $$$a_i + x$$$ is still within the range, just update its frequency array. Otherwise we need to split it into two ranges. We can use "large-to-small splitting": modify the frequency array of the larger half, then make a new range for the smaller half (if its size is at least $$$2^k$$$). So the total number of frequency array updates per power $$$k$$$ is at most $$$O(q + n \, \text{log} \, n)$$$, so the overall complexity is $$$O((n + q) \, \text{log}^2 \, n)$$$.

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +20 Проголосовать: не нравится

I use bfs in problem C.I thought I would get MLE or TLE pending but I passed the testings.:)

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

got d right after the contest ended

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

IMO D would've fit it's position better if time limits would've cut $$$O(n*m^3)$$$ solutions! Or maybe I'm just salty that I spent way too long finding the $$$O(n*m^2)$$$ solution XD

»
21 месяц назад, скрыть # |
Rev. 2  
Проголосовать: нравится +16 Проголосовать: не нравится

The limit of $$$33$$$ queries (instead of $$$32$$$) is to allow less optimized solutions and other solutions.

This is brutally cruel, I got WA because my max query count was $$$34$$$... Why is there the need to outright destroying implementation that simply lacks a bit of thinking that isn't much crucial to the problem idea?

(Of course, I am a bit salty, and I don't think I should really blame it coz I upsolved either way, but I feel the "less optimized solutions to pass" intention is quite not-so-there.)

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

B made me want to KMS.

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I dislike C so much, how did people come up with the solution?

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

    i thought C was nice lol

    my solution was a bit diff, basically I set $$$a_x=0$$$ and $$$a_y=1$$$, then filled in everything between

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

    in such problems thinking about a few specific cases and then generalizing helps a lot.

    look at the graph formed by the numbers, and first look at the cases when x and y are already friends :

    Case 1: N is odd the smallest case is a cycle of 3. it's easy to see 0-1-2 works. can you generalise this?

    if I were to add 2 nodes between 0 1, it would be 1 and 0 giving us a solution for N= 5. you could extend this for all odd N

    Case 2: N is even the smallest case is a cycle of 4. here 0-1-0-1 works! if I were to add 2 nodes between 0 and 1 it would be 1 and 0.

    with this you've generalised the solution for even N

    now what's left is cases where x and y aren't already friends

    here you can have an odd cycle next to an even cycle, two even cycles, or two odd cycles. like above starting with cycles of length (3,4) , (3,3) ,(4,4) and then generalising it helps you see the construction.

    297495561

  • »
    »
    21 месяц назад, скрыть # ^ |
     
    Проголосовать: нравится -9 Проголосовать: не нравится

    C was, without a doubt, the most utterly repulsive problem I'd ever had the misfortune of encountering.

»
21 месяц назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

very cool contest

tho the difficulty had a very sudden jump on e while maintaining a steady grow from a to d (in my opinion, of course)

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +35 Проголосовать: не нравится

Where is the cycle in the DP transition for D coming from? Surely you can just do $$$g(i, j, x) = \text{min}(g(i, j - 1, x), f(i - 1, j) + kx) + a(i, j + x)$$$, and there is no cycle?

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I was one +-1 away from solving E during the contest... Cool problem though

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

Editorial for B made me pity meself.

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

    Maybe you over-complicated B during the contest and panicked maybe? Few things were obvious like if there is no 'p' or no 's' then ans is obvio 'YES', and if 'p' came before 's' then it's impossible also trivial if we just made the subarray and saw as mentioned in q. The next thing i did was make few cases like 'sspp' etc and checked where i came up with 'YES' is only possible when we have 'sppp..' or 'ssss..p' type of string rest it won't be possible. Usually i am too dumb for qs but maybe yesterday was just lucky day for me.

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

for F you can solve in $$$O(n \log n)$$$ as follows. Observe that when checking for $$$2^k$$$, only sets with size at least $$$2^k$$$ can possibly work, so delay building them until the size is $$$2^k$$$. With this, it is reasonable to use just an array of size 2^k to store the frequecny array. Insetad of merging two sets in time $$$set constnat * min(|A|,|B|)$$$, we can merge this in exactly $$$2^k$$$. A simple amortization will show that this process is $$$O(n)$$$ for each target power of two. So this part works in $$$O(n log n)$$$.

We also have to maintain the maxmium size of all working sets. This costs $$$O(X log X)$$$ for $$$X$$$ at most the number of built set, with the log from constnats of map. Notice that when checking for $$$2^k$$$, $$$X$$$ is bounded by $$$\frac{n}{2^k}$$$. Suming this over $$$k$$$, this part is also $$$O(n \log n)$$$.

Edit: I forgot that we you process queries, the same already built set can be moved in/out of working sets, so unfortunately the last seemingly easiest part is actually the slowest at $$$O(n log^2 n)$$$ I don’t see a good way to save the log in this task.

Second edit: it turns out saving this last log isn’t as bad as I thought. It is not a true savings, but vEB tree (rather, the practical base 64 variant) allows this final part to be done in O(n log n log log n) overall

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Alternative solution to C and D.

C: If $$$(x \gt y)$$$ swap $$$(x,y)$$$, set $$$A_x = 0$$$, and alternate between 0 and 1 and wrap that around on the right side until $$$y$$$ (not including $$$y$$$). Also do the same for the left side. Then $$$A_y = max(A_{y-1},A_{y+1}) +1$$$. This works because $$$A_y \gt =1$$$ because $$$y$$$ connected to $$$x$$$ and $$$A_x = 0$$$.

D: Very similar to the editorial, but I set up the dp differently.

Let $$$dp[i][j][b] =$$$ the minimum cost using to get to $$$(i,j)$$$ shifting the $$$i$$$th row $$$b$$$ times.

Let $$$stored[i][j] =$$$ the minimum cost to get to $$$(i,j)$$$ including operation costs and across all possible shifts.

Intitally, $$$dp[i][j][b] = inf$$$, for all $$$(i,j)$$$. Except $$$dp[0][j] = arr[0][j]$$$.

Transitions are as follows:

$$$dp[i][j][b] = min(dp[i][j-1][b] + arr[i][j],stored[i-1][j] + arr[i][j])$$$

$$$stored[i][j] = max(dp[i][j][b] + b*k)$$$ for all $$$b$$$ from $$$(0,m)$$$

»
21 месяц назад, скрыть # |
Rev. 2  
Проголосовать: нравится +6 Проголосовать: не нравится

I did the following DP in D:

  1. dp[i][j] — mincost of getting to cell (i,j).

  2. dp[i][j] = min_{t <= j} dp[i-1][t] + f[t][j], where f[t][j] — mincost of getting from (i,t) to (i,j).

  3. computing f is the hard part: I compute all f[l][l+len-1] for each given len=1..m.

First, I pushed b[i][l+len-1] - b[i][l-1] + k * (l-1) to a queue with minimum for all l = 1..m. In the queue for each l, I support values of all m cyclic shifts of the row i. Then f[l][l+len-1] for each l = 1..m-len+1 is f[l][l+len-1] = (min of the queue) - k * (l-1). f[l][r] for each row i is calculated in O(m log m), and dp[i][j] is calculated in O(m) for each (i,j), so the total complexity is O(nm^2 log m).

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

    An interesting approach to finding cheapest paths on a "layer" in a matrix!

    I found it in a different way, for each length len do: (assuming that i fixed)

    1. Calculated all sum's (from each start position j) of length len: $$$\sum_{k=j}^{j+\text{len}-1} a_{i,k}$$$

    2. When found minimal of them — let it be for position $$$j = \text{pos}$$$. Then cheapest passing from $$$(i, \text{pos})$$$ to $$$(i, \text{pos}+\text{len}-1)$$$ is the path without a shifting (just $$$a_{i,\text{pos}} + a_{i, \text{pos}+1} + .. + a_{i, \text{pos}+\text{len} - 1}$$$)

    3. Next moving from pos to the left, choose cheapest path from:

      • path without a shift: $$$\sum\limits_{t=j}^{j+\text{len}-1} a_{i,j}$$$
      • path shifted "by one" (started from $$$j+1$$$): $$$\sum\limits_{t=j+1}^{j+1+\text{len}-1} a_{i,j} + k$$$

    My submission: 299116684. Total complexity: $$$O(nm^2)$$$, but in this solution getting lost generality, because it used the fact of a linear penalty for shifting.

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Note that you can do binary search directly on $$$[n/4+1,n-1]$$$ in E. Code:297545783.

»
21 месяц назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Nice contest... I'm happy with my performance, finally reached specialist!

I don't understand why my dp solution for D did not work, it is failing on 2 of the sample inputs. Can someone please point out where I'm going wrong here? (Assuming I'd have optimized the transition to make it O(nm^2) by saving the min values for each row)

My Code
»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

C took 10% of time as compared to B.

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

For C i just keep iterating over answer untill it converged, i did this coz i thought it will converged in ~ 4 steps but not sure exactly how.

can somebody hack my solution? or help me to prove why this works? 297490012

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

    coz i thought it will converged in ~ 4 steps

    Yeah. The thought train is simple: friends should not have equal values, so answer should be at most $$$\text{cnt}_{\text{friends}}$$$. And since you can only have at most $$$3$$$ friends (two adjacent, one predetermined in $$$(x, y)$$$), so...

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

-121 :D

»
21 месяц назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Nice B
Could anyone suggest similar problems or tell me what topic this falls under? Any tips to solve these types of problems faster?

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

This contest was more inclined towards implementation.Though, I became specialist thanks to this contest.

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

for D the second same for is unnecessary and also there is no path from [i][m — 1] to [i][1] so there is no need to write tmp[(j+m-1)%m]

so simpler code is:

for(int i=1;i<=n;i++){
    for(int shift = 0;shift<m;shift++){
        vector<ll>tmp(m,1e18);
        for(int j=0;j<m;j++)tmp[j] = dp[i-1][j] + a[i][(j+shift)%m] + k*1LL*shift;

        for(int j=1;j<m;j++)tmp[j] = min(tmp[j],tmp[(j-1)%m] + a[i][(j+shift)%m]);
        for(int j=0;j<m;j++)dp[i][j] = min(dp[i][j],tmp[j]);
    }
}

(100% correct)

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится -6 Проголосовать: не нравится

$$$B$$$ $$$\gt$$$ $$$D$$$

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

Codeforces Hot News!

Wow! Coder chenlinxuan0226 competed in Codeforces Round 994 (Div. 2) and gained -160 rating points taking place 4892

Share it!

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

Codeforces Hot News!

Wow! Coder chenlinxuan0226 competed in Codeforces Round 994 (Div. 2) and gained -160 rating points taking place 4892

Share it!

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

My 4th contest and Div 2 gives me real reality chevk. Solved only A ... and crossed 1000 barrier yay! 2025 will be a year of CP for me.:)

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For B, we just need to check if array [1,2,3...,n-1,n] or [n,n-1,...,2,1] satisfies the condition. So we don't have to worry about corner cases

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

for problem B, my thought process is segments that need to form permutations must be in subset relation. otherwise there is an element in the smaller segment that is not in the larger segment. then the larger segment is not a permutation.

hence all segments should form a chain of subset. to check this, intersect all the segments and check if it is one of the segment.

submission

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem D, why do you substract (*) to the transition? Is it a substraction or just to explain it later?

Also, I don't get why "the g(i,j−1,k) term is from the case where you move from (i,j−1) to (i,j)", shouldn't it be g(i,j−1,x)?

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

This B made me cry

»
21 месяц назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Why In Problem E. "Make 2 queries [1,n/4] and [n/4+1,n/2]. This tells us which half the 1 is in: it is in [1,n/2] if the query results are different and [n/2+1,n] otherwise." Works?

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

    Since both queries have the same length, their results will be both correct or both flipped. If the 1 is in $$$[1, n/2]$$$, the only 1 in $$$a$$$ will be in exactly one of the queries, so they will have different results. If not, the two results will be both 0 if not flipped or both 1 if flipped.

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Is the proof for B correct?

"However, b cannot contain a: since a is not the entire p, a does not contain pn. However, b contains pn. Contradiction."

Doesn't really make sense

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

    Note that $$$p$$$ must be a permutation, so elements of $$$a$$$ being in $$$b$$$ implies that $$$a$$$ must be a subarray of $$$b$$$.

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

      I get that, but why would that be a contradiction?

      [1, 2] is a subarray of [1, 2, 3]. The first one doesn't contain 3. The second one contains.

      "b cannot contain a: since a is not the entire p, a does not contain pn. However, b contains pn. Contradiction."

      What's the contradiction?

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

        The contradiction is that $$$a$$$ must be a subarray of $$$b$$$, but $$$b$$$ cannot contain $$$a$$$ (i.e. $$$a$$$ cannot be a subarray of $$$b$$$). The two bullet points contradict each other.

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

          i mean to say you wanted to show that (a cannot be subarray of b) but the proof of that is not exactly showing that because its arguing about one element of b which is not in a , but for proof to work we would need to argue with one element of a which is not in b.

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem F is too implementation heavy

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

i like E a lot

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Has anyone approached Problem D using recursive DP not iterative?

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

First 2 probs weren't that hard tho I got +1 each

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

I was practicing old problems and could see Mex Destruction is exactly same as 1696B — NIT Destroys the Universe

https://codeforces.me/problemset/problem/1696/B

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I'm surprised how simple the solution to B is. During the contest, I created a list of segments to check the validity of the ranges. I have completely overlooked the easy solution.

»
21 месяц назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

My approach to F :

Part 1: What the F?
Part 2: Solve without updates
Part 3 : Handling updates

Submission : 299146450

»
20 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Evirir YouKn0wWho

I know I'm super late but I think there is an issue with the checker in the interactive task.

Please check this submission. I ran the program multiple times, I don't see anything wrong.

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

D is cruel :sad:

»
16 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

How is the Idea of Problem — B intutive?

Can anyone help?

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

    the idea is that any permutation should contain all the numbers up to a certain number , if you encounter a 'p' this number will be j+1 where j is the index where 'p' appearred (0 based) , and it will be n-i if it is an 's' , so you can see that if a 's' appear after a 'p' you can not build the permutation beacause some of the numbers you need to build the suffix starting from 's' were used in the prefix ending at 'p' . now remains the case where there is a 'p' after a 's' , here the idea is that you need to check if the space between 's' and 'p' enough to hold all the elements of the intersections of their permutations , suppose 's' is at i and 'p' is at j , the space between them is j-i+1 , and the intersections of their permutations will be min(n-i,j+1) (because of the ranges we discussed earlier) , so the necessarry and sufficient condition will be j-i+1>=min(n-i,j+1) , and you can do this in O(n^2) since n is only up to 500 . my submission

»
14 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone here can check my solution why does it got TLE on problem F ?

Thank you

https://codeforces.me/contest/2049/submission/300638367

»
11 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem F, we can update in forward order also, we just need to break the segments and we can apply small to large there as well.