### pikmike's blog

By pikmike, history, 8 months ago, translation, ,

1221A - 2048 Game

Idea: Roms

Tutorial
Solution (Roms)

1221B - Knights

Idea: BledDest

Tutorial
Solution (PikMike)

1221C - Perfect Team

Idea: Roms

Tutorial
Solution 1 (BledDest)
Solution 2 (PikMike)

1221D - Make The Fence Great Again

Idea: Roms

Tutorial
Solution (Roms)

1221E - Game With String

Idea: Roms

Tutorial
Solution (Roms)

1221F - Choose a Square

Idea: Ne0n25

Tutorial
Solution (Ne0n25)

1221G - Graph And Numbers

Idea: BledDest

Tutorial
Solution (BledDest)

• +63

 » 8 months ago, # |   0 Very sorry gor noob question, but in G why is F(0.1) number of independent sets in graph?
•  » » 8 months ago, # ^ |   +8 $F(\{0, 1\})$ is such a coloring that no pair of vertices colored $1$ are connected by an edge. So the subtask is to count the number of ways to choose vertices to color them $1$ in such a way that no pair is connected by an edge. And that's the definition of an independent set.
•  » » » 8 months ago, # ^ |   +5 Yeah, I figured it out by myself after some time... I think you should include this explanation in the editorial though, it's not immediately obvious.
•  » » » » 8 months ago, # ^ | ← Rev. 2 →   +11 Meh, I think it's fine. Editorials are not supposed to be immediately obvious for everybody.
•  » » » » 8 months ago, # ^ | ← Rev. 2 →   0 Let me explain (I didn't test my solution yet though). And frankly making it partially for myself. Correct me if I'm wrong.As PikMike said, F({0,1}) means that we can't put 1 on to two connected vertices.Let's just consider on example of 5 verts graph (below are edges):{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 1}First we can put 0 everywhere.Another options?For example, we put 1 on first vert (v1). Where else we can put 1 and how many ways to do it?We can just put 0 on the rest.In order to avoid edges with 2, we can put 1 only on vertices not connected to v1. And such vertices will form independent set. Amount of such combinations of ones (1) obviously is amount of independent sets. So in our case for v1 next combinations are allowed:{v1}, {v1, v3}, {v1, v4}and so on.All together:{v1}, {v1, v3}, {v1, v4}, {v2}, {v2, v4}, {v2, v5}, {v3}, {v3, v5}, {}Total 9 combinations.
•  » » » 8 months ago, # ^ |   0 We should also count empty independent set, right?
 » 8 months ago, # |   0 I still don't understand Why it is not beneficial to increase the length of some board by three or more.... (Problem D) Can anyone help???
•  » » 8 months ago, # ^ |   0 because there is no need. even if u increase by 2 or 1 or 0 u can de different from ur neighbors
•  » » 8 months ago, # ^ |   +13 Assuming $a_i = a_{i-1}$. We can increase $a_i$ or $a_{i-1}$ by 1.If we decide to increase $a_i$ by 1, it doesn't change anything before, so the Fence is still great up to now.If we decide to increase $a_{i-1}$ by 1, it can equal to $a_{i-2}$ and break the Fence's greatness. In this situation, we can continuously increase $a_{i-1}$ by 1 (now, it increased 2 units) or increase $a_{i-2}$ by 1.That's the reason why we should increase every $a_i$ at most $2$.
•  » » 8 months ago, # ^ |   0 The only requirement is that the height of two adjacent board should not be the same. Consider a board 'i' with a height ai, there can be 3 possibilities- 1. all adjacent board has the same height. 2. all adjacent board has a different height. 3. any two board has the same height.In case 1 we can either increase the height first by 1, without altering the height of second, increase height of second or increase the height of second by 2 and third by 1 or vice versa. (this can be applied to any pair). Hence we do not need more than 2 increments for a particular board.Case 2 and 3 are obvious.
•  » » 4 months ago, # ^ |   0 3 case structure theorem
 » 8 months ago, # |   0 can someone explain 5th question . I dont think explanation is correct , how can u convert fourth type segment into 2nd type segment and what about the cases in a>=2*b
 » 8 months ago, # |   0 Can someone give me the proof for problem B?
•  » » 8 months ago, # ^ |   0 Let's play chess. :v BTW, all black cells $(i,j)$ in a chess board have the same parity of $i+j$ and different from all white cells.
•  » » 8 months ago, # ^ | ← Rev. 2 →   0 I did it using a bfs. Similar to bipartite colouring
•  » » » 8 months ago, # ^ |   0 I do the same with dfs :))
•  » » 8 months ago, # ^ | ← Rev. 15 →   0 Fact 1. If we want to maximize the number of attacks, then we would put black horse on the board, such that any black horse always attacks only white horse. This happens if the black horses are placed in square of the same color. Easy to note that when placed a horse on a black square it always attacks a white squares.Fact 2. We can put an $id$ for each square of the board. Let $n$ the number of black squares and $m$ the number of white squares. A black horse that attacks a white horse is equivalent to claiming that a white horse attacks a black horse. That is: $\sum_{x \in \text{ black}}^n attacks(x) = \sum_{x \in \text{ white}}^m attacks(x)$This is true because we can represent the attacks as a bipartite graph $G$. Let $G [X, Y]$ be a bipartite graph where $X$ corresponds to the black horse vertices and $Y$ to the white horse vertices. Clearly the number of attacks equals the number of edges. $|E| = \sum \text{degree of } V(X) = \sum \text{degree of } V(Y)$
•  » » 8 months ago, # ^ |   0 Editorials solution is true . Lets construct a graph , (i,j) can attack (k,l) . That means (k,l) also can attack (i,j) . Lets connect (i,j) and (k,l) with an edge . Now using bicoloring idea , run a bfs . And you will see all the solution has a pattern repeat BW again and again . My BFS solution: https://codeforces.com/contest/1221/submission/61608838
 » 8 months ago, # |   0 Hold on, what is OR-convolution mentioned in G? I couldn't google it.
•  » » 8 months ago, # ^ |   0 Google sum over subsets DP, that's the thing you need.
•  » » » 8 months ago, # ^ |   0 Yah I solve G using that, just wondering what is OR convolution
•  » » » » 8 months ago, # ^ |   0 I remember seeing it here and here but idk if it's really needed in the solution.
•  » » » » » 8 months ago, # ^ |   0 Thanks a lot. As I know curiosity always leads me to sth more to learn
 » 8 months ago, # | ← Rev. 2 →   0 In problem A , how to prove that if sum of numbers other than numbers greater than 2048 is greater or equal to 2048 then answer will be Yes ?
 » 8 months ago, # |   0 Very weak test cases for A even my wrong code got accepted
•  » » 8 months ago, # ^ |   +5 ?your code is correct
•  » » » 8 months ago, # ^ |   0 Yeah sorry I just saw that numbers were in powers of 2 if not then my code was incorrect
 » 8 months ago, # |   0 Can D be solved without dp???
 » 8 months ago, # |   0 can someone tell me what if (c + m + x - 2 * mid >= mid) means in problem C ?
•  » » 8 months ago, # ^ |   +6 If $mid$ teams are formed, then $mid$ coders are already taken and $mid$ mathematicians are also taken. That leaves us with $(c - mid) + (m - mid) + x$ people to fill the empty spot in each of the $mid$ teams. So if that condition if true, then there are enough people to form at least $mid$ teams.
•  » » » 8 months ago, # ^ |   0 got it! Thank you sir!.
 » 8 months ago, # |   +3 In Problem E, when we consider segments of type 4, where len >= 2*b, how can we say that Bob can always convert it into a segment of type 2? If (a+b) <= len < 3b, if Bob makes his move, the segment will become type 3
•  » » 8 months ago, # ^ |   0 When $len \geq 2*b$, Bob can always get a segment of type $2$ by saving the $b$ right most places and then choosing his segment. Eg. if $b=2$ and $len=8$, $\ ........ \rightarrow\ ....XX..\$ Now he can use the right most place when Alice doesn't have a move left.
 » 8 months ago, # |   0 Please Someone explain how to do D.
 » 8 months ago, # | ← Rev. 4 →   0 Problem F ,if I submit my code with GNU C++17 ,I would Wrong answer on test 2, if I submit my code with GNU C++14,it was Accepted.Could someone tell me why? my code: 60964098 60962460. Sorry for my poor English.
 » 8 months ago, # |   0 In 5, how are we sure that 2b>=a when it is only mentioned that a>b?
•  » » 8 months ago, # ^ |   0 We don't. If a <= 2b then there is just no segment of the third type.
 » 8 months ago, # | ← Rev. 2 →   0 need a proof for problem "A" solution please
 » 8 months ago, # | ← Rev. 3 →   0 Can anyone give links to more questions like Problem C — classic Binary Search? I want to solve more of these Bin. Search questions.
 » 8 months ago, # | ← Rev. 4 →   0 There are accepted solutions (e.g. 60854023)for Problem C (Perfect Teams) which are just$min \{c, m, \left \lfloor{\frac{c+m+x}{3}}\right \rfloor\}$Could someone please prove (or explain the idea behind) the formula?
•  » » 8 months ago, # ^ |   0 By definition, each team consists of a coder, a mathematician, and exactly three people. The resource with minimal availability decides how many perfect teams you can build.
•  » » » 7 months ago, # ^ |   0 i can understand how minimal availability of c & m decide the outcome but what does (c+m+x)/3 here signify? can you please explain? _/\_
•  » » » » 7 months ago, # ^ |   0 You need enough people for 3 people per team.
•  » » » 7 months ago, # ^ | ← Rev. 4 →   0 The resource with minimal availability (either $c$ or $m$) defines an upper bound for the number of perfect teams (which is not necessarily the same as the number of perfect teams).Thus, it is not yet obvious to me what is the idea behind proving that the whole formula is valid for all possible test cases.
 » 8 months ago, # |   0 haizz i am bad at implementation algorithm. That's why i rarely do well in the contest :< . Can someone give me advice on this matter, pls !! sorry for my poor english :D
•  » » 8 months ago, # ^ |   0 Read and understand good codes of the questions you are doing... try to remember loopholes and learn to make your code short and simple. Also each line of the code should be important that is your code shouldn't be redundant.
 » 8 months ago, # |   0 Can E be solved like this? I am not sure! :3First take segment length of dots, then sort them and obviously ignore the lengths smaller than b. Then we declare vector < pair > info which saves what happens if alice starts, if bob starts when there are first i lengths only.Now in these sorted lengths, take the first one and save who wins if there is only that first segment and alice starts the move, if there is only that last segment and bob starts the moveThen we iterate i from 1 to size of segment lengths array. For i-th element, we decide who will win if alice starts, if bob starts the move when there are only first i segments. We decide considering the i-th length and the information from info[i-1]......and save it in info[i]......**sorry for my explanation.....i suck at explaining :(
 » 8 months ago, # |   0 problem A mathematical induction proof: (1) if the sum of the not larger than 1 elements is greater than 1, the numbers can merged into 1. obvious. (2)suppose the sum of not larger than 2^(k-1) elements is greater than 2^(k-1), the numbers can be merged into 2^k. Then if the sum smaller than 2^k elements of is larger than 2^k , the numbers can be merged into 2^k.
 » 8 months ago, # |   0 in D what is the proof that time complexity of the recursion won't be large I did not get how the dp is exactly working
•  » » 8 months ago, # ^ |   0 because all boards will be increased by no more than two.
•  » » » 8 months ago, # ^ |   0 Yeah I know but there's a for loop with calc function with three possibilities x=0 x=1 x=2 so it's o(3^n) complexity isnt it ? Or if statement is making it o(n) but why
•  » » » » 8 months ago, # ^ |   0 No it is 3*3*n
•  » » 8 months ago, # ^ |   0 the proof is in the problem he say It is guaranteed that sum of all n over all queries not exceed 3*1e5 so it make dp fit in the time .
 » 8 months ago, # |   0 My recursive DP solution for problem D is timing out , It times out in the test case where answer is always 0 , the weird thing is that it times out even after I hardcode the case where number of elements are always 1! can someone please check and let me know where I can optimize ? Thanks ! https://codeforces.com/contest/1221/submission/61071469
•  » » 5 months ago, # ^ |   0 I hope your problem is already solved by now, but I'm writing it for other people!Use fastio. I was having the same problem! Write this inside main(): ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
 » 8 months ago, # | ← Rev. 4 →   0 Can anyone please help me in problem D. I am getting wrong answer on teset case 2. I am using tabulation approach instead of memoization. Here is also Link to my submission. Thanks in advance #include using namespace std; #define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) typedef long long int ll; int main(){ SPEED; ll t; cin>>t; while(t--){ ll n; cin>>n; ll a[n]; ll b[n]; for(ll i = 0; i< n; i++){ cin>>a[i]>>b[i]; } ll dp[3][n]; dp[0][0]=0; dp[1][0]=b[0]; dp[2][0]=b[0]*1ll*2; for(ll i = 1; i <= n-1; i++){ for(ll j = 0; j< 3; j++){ ll temp = LONG_MAX; for(ll k = 0; k < 3; k++){ if(a[i]+j!=a[i-1]+k){ temp = min(temp, dp[k][i-1]+b[i]*1ll*j); } } dp[j][i]=temp; } } cout<
•  » » 8 months ago, # ^ |   0 just use temp=1e18 insted of LONG_MAX
•  » » » 8 months ago, # ^ |   0 Got AC !!!Thanks @i_ll_try_123.Can you please tell me why using 1e18 is working instead of LONG_MAX
•  » » 8 months ago, # ^ |   0 can you please post it's recursive solution
•  » » » 8 months ago, # ^ |   0 You can check editorial solution for that.. which is a memoization approach
•  » » » » 8 months ago, # ^ |   0 That's not memoization, that is bottom — up approach.Can you post top down approach
 » 8 months ago, # |   0 Hi, i am trying to understand the tutorial for 1221F where it says "let's reformulate the problem the following way: we have to find the segment (l,r), such that the sum of the segments fully covered by it is maximal." segment(l,r) is formed by the two points (l,l) and (r,r) belonging to the bisectrix? Is the sum about our cost function and if so why do we calculate it for the segments and not for the points?
 » 8 months ago, # | ← Rev. 2 →   0 I don't understand the formula of DIV2C c + m + x — 2 * mid >= mid__ Please help...
•  » » 8 months ago, # ^ |   0
 » 8 months ago, # |   0 Why am I getting a TLE in this solution to the D. Link: https://codeforces.com/contest/1221/submission/61306295
•  » » 8 months ago, # ^ |   +3 write this in your codeios_base::sync_with_stdio(false); cin.tie(NULL);
 » 8 months ago, # |   0 I can't understand editorial for problem F. What do you store in the segment tree, also how do you initialize it?
 » 8 months ago, # |   0 In problem E if 2b
 » 8 months ago, # |   0 In problem C.Perfect teams. For the test case where c=18 m=13 and x=0; the maximum possible number of teams can be 9...but the test cases give solution to be 10. I think the test case has wrong answer .Where am i wrong???Please help.
•  » » 7 months ago, # ^ | ← Rev. 2 →   0 C C C C C C C C C C C C C C C C C C M M MM M M M M M M M M M
 » 8 months ago, # |   0 Helpp!! Why is this not working for A public static boolean gg(int x) { if(map.getOrDefault(x, 0)>=1) { int xx=map.get(x); map.put(x, xx-1); return true; } if(x==1)return false; boolean a=gg(x/2); boolean b=gg(x/2); return a&&b; } 
 » 7 months ago, # | ← Rev. 2 →   0 I have an alternate solution and (maybe more intuitive) for problem A.Let's ignore elements > 2048.If 2048 is already in the multiset, we are done. Otherwise, notice that the merging of the smallest two equal elements in the multiset can be used in any sequence of merges that can obtain 2048 without making the "Yes" answer become "No". This is because that is the only way to merge any of those two elements with some other element. We keep doing this until we have 2048 in the multiset or we cannot merge any more elements. Due to our invariant that the merging at each step will not affect the answer, if we cannot obtain 2048, then the answer is clearly "No". Otherwise, the answer is "Yes".Note that if at any point during the merging, if the smallest two elements in the set are not equal, we can discard the smaller one because that element cannot be merged with any element and can be declared useless.To support the efficient merging and extraction of minimum elements, we can use a priority queue.Time complexity: $\mathcal{O}(NlogN)$ for each test case because we can have $\mathcal{O}(N)$ merges/discards per test case.
 » 5 months ago, # |   0 Can anyone tell me the botton up solution of question D? Link is ->Question D
•  » » 5 months ago, # ^ |   0 Because both the tutorials have top down method
•  » » » 7 weeks ago, # ^ |   0
 » 7 weeks ago, # |   0 Bottom UP dp solution for C (make that fence great Again): link https://codeforces.com/contest/1221/submission/76082735
 » 6 weeks ago, # |   0 can we solve E using grundy numbers?