### flaviu2001's blog

By flaviu2001, history, 3 years ago,

1421A — XORwice

Idea and solution: flaviu2001

Hint
Solution

1421B — Putting Bricks in the Wall

Idea: flaviu2001, solution: flaviu2001 and stefdasca

Hint
Solution

1421C — Palindromifier

Idea and solution: flaviu2001

Hint
Solution

1421D — Hexagons

Idea: flaviu2001, solution: flaviu2001 and koala_bear00

Hint
Solution

1421E — Swedish Heroes

Idea and solution: flaviu2001

Hint
Sneaky corner case
Solution
Proof for the pattern

You can also see the video solutions on stefdasca's Youtube channel

• +158

| Write comment?
 » 3 years ago, # |   0 Auto comment: topic has been updated by flaviu2001 (previous revision, new revision, compare).
•  » » 3 years ago, # ^ |   0 When will the induction end for the problem E?
 » 3 years ago, # | ← Rev. 6 →   -21 Why is it getting WA on pretest 4? Spoilervoid solve(int test_case){ int i, j; int x, y; cin >> x >> y; vector c(7); for(i=1;i<=6;i++)cin >> c[i]; c[1] = min(c[1] , c[6]+c[2]); c[2] = min(c[2] , c[1]+c[3]); c[3] = min(c[3] , c[2]+c[4]); c[4] = min(c[4] , c[3]+c[5]); c[5] = min(c[5] , c[4]+c[6]); c[6] = min(c[6] , c[5]+c[1]); int ans; if(x <= 0 && y <= x){ int dist = (c[4])*abs(x); ans = dist+c[5]*abs(y-x); }else if(x <= 0 && y >= 0){ int dist = (c[3])*abs(x); ans = dist+c[2]*abs(y); }else if(x <= 0){ int dist = (c[3])*abs(x); ans = dist+c[4]*abs(y); }else if(y >= x){ int dist = (c[1])*abs(x); ans = dist+c[2]*abs(y-x); }else if(y <= 0){ int dist = (c[6])*abs(x); ans = dist+c[5]*abs(y); }else if(y > 0){ int dist = (c[6])*abs(y-x); ans = dist+c[1]*abs(x); }cout << ans << '\n'; return; } UPD: submission
•  » » 3 years ago, # ^ |   +23 Just submit it and wait for anwer...
•  » » 3 years ago, # ^ | ← Rev. 4 →   0 I think the condition of else if(x<=0) is wrong. This condition is reached when x<=0 and x
 » 3 years ago, # |   0 I overkilled B initially by using BFS. Then I realized that it can be done by using just a couple of 'if' conditions.
•  » » 3 years ago, # ^ |   0 Yes,it was a bit tricky. Infact i was also thinking bfs kind of stuff. But later realized that it can be solved just with a greedy aproach.
•  » » 3 years ago, # ^ |   0 We can't avoid BFS to check if we can reach F right??
•  » » » 3 years ago, # ^ |   +1 The thing is you're actually not required to check that (you don't need to optimize for minimum "c")
•  » » 3 years ago, # ^ |   0 it is just tricky we have to just take care of 4 nodes i.e is the two neighbouring nodes of S and two neighbouring nodes of F and then we just have to make a combination like this 1. all neighbours of S are 1 and all neighbours of F are 0 2. all neighbours of S are 0 and all neighbours of F are 1
 » 3 years ago, # |   +1 Wow so fast editorial!
 » 3 years ago, # |   +10 C's testcase contains a major hint.
•  » » 3 years ago, # ^ |   +12 In constructive problems, the problem-setter always try to hidden the right construcion approach and even use some misleading approach in sample output, so I prefer to ignore the sample output in such problems.However, this time it did help lol
•  » » 3 years ago, # ^ |   -19 +1
 » 3 years ago, # | ← Rev. 4 →   +16 Another way to go about D is to think of the hexagonal world as a regular 2D grid, where you can move from (0,0) to six directions. [all directions except (-1,1) and (1,-1)]Then it's just checking all the possible routes.NB: The image is not uploading correctly for some reason. If somebody knows why, please let me know. Thanks.
 » 3 years ago, # |   0 Feeling depressed after seeing the solution of C and also that I thinked on it for an Hour , I got the hint part while thinking , but didn't got that we can move n-1 to n+1 .
•  » » 3 years ago, # ^ |   0 I was a little depressed too, it took me 2 days thinking about task C to be able to solve it.A good tip is to just look at the editorial when you think the problem uses some algorithm that you don't know. Otherwise, the best thing to do is not to look at the editorial and fight against task.
 » 3 years ago, # |   0 For A, even OR works (a^(a|b))+(b^(a|b))
•  » » 3 years ago, # ^ |   +12 Just use a^b as the answer.
•  » » » 3 years ago, # ^ |   +3 Can anyone actually prove this?
•  » » » » 3 years ago, # ^ |   +3 If you're talking about proof of why XOR a^b works, then it's this way. Let's take i'th digit from both numbers (in binary). 1. If both of them are 1, then for x we put there 1 as well. This way we'll make that digit become 0 in our answer. 2. If one of them is 1, then no matter what we put there at x, that place in the answer will be 1. 3. If none of them is 1, then we need to put 0 there at x, that place in the answer will be 0. From these approaches, you can see that if digits in respective places are same, then that digit will be 0 in our answer, otherwise, it will be 1. And this is what XOR does indeed.
•  » » » » 3 years ago, # ^ |   0 Because the only one situation that can decrease the answer is that same bit of a and b is 1 So actually x=a&b for example if a=1011 and b=1101 than x=a&b=1001 let's foucus on zeroes which means in that bit a is different with b there are 4 situation which NO.1 2 3 4 a= 00 01 10 11 b= 11 10 01 00 We found that for each case the equation (a xor x)+(b xor x)=11=a xor b for the zero bits of x let think about 1 bits in x wo know x=a&b ,so only if the same bit of a and b is 1 can makes the same bit of x equal 1, and obviously 1 xor 1 equals 0 So for the 1 bits in x, wo also get a equation (a xor x)+(b xor x)=00=a xor b By combining the two formulas, we can get the formula (a xor x)+(b xor x)=a xor b Have fun :>
 » 3 years ago, # |   +6 Another way to solve A is we can find the minimum value if we take same 2 digits like 6^6 is zero. so we can take the minimum value from a and b and put that value in x and solve the equation as it said. Simple as that.
 » 3 years ago, # | ← Rev. 2 →   0 Strong pretests for B https://www.imageupload.net/image/inkedadsiz-li.tW8IJ
 » 3 years ago, # |   +7 Can someone please explain the dp solution in E? Thanks in advance :-)
 » 3 years ago, # |   +3 How didn't this solution 95865345 get RE on pretests?
 » 3 years ago, # | ← Rev. 2 →   0 Edit: nvm
•  » » 3 years ago, # ^ |   0 n is inital strings length not current
•  » » 3 years ago, # ^ |   0 The n in (L, n) is w.r.t the new string, which is of length n+1 (after the first (R n-1)).
 » 3 years ago, # |   +55 After solving A-D : Why did I learn algorithms ? ;ㅅ;
•  » » 3 years ago, # ^ | ← Rev. 2 →   +3 Wonsei your solution for problem D looks so simple. Can you please explain your idea and solution in a short? I am not understanding the editorial.
•  » » » 3 years ago, # ^ | ← Rev. 2 →   +3 You can see here that to go from the first dot to the second dot, you can take two choices, c1 or c2+c6. so, we call n1(the cost to the right upper hexagon) minimum of those two values. You do that for all 6 directions. After that, you just go the shortest path to the destination from 0,0 using the new values.
 » 3 years ago, # |   +9 I had a bit different solution to D.it is easy to notice, that we need to move on diagonals, or straight line. And, number of different points we need is quite small. After drawing on piece of paper it is obvious that we need only some cominations of $x, y, x-y, y- x$. So, I added points $(0, x), (0, y), ...$ (ones on staight gorizontal line with $(0; 0)$), then $(x, 0), (y, 0) ...$ — first diagonal, and $(x, x), (y, y) ...$ on second.We have 14 points, its easy to calculater distance between points on same diagonal or line. After doing so I run Floyd-Warshall on this graph, so all solution works in $O(14^3) = O(1)$;) per test case. Code.
•  » » 3 years ago, # ^ |   +3 Did the same without running Floyd-Warshall :)https://github.com/actium/cf/blob/master/1400/20/1421d.cpp
•  » » 3 years ago, # ^ |   0 Great idea! I did it and 9 points is enough.
 » 3 years ago, # | ← Rev. 2 →   +22 How to find the pattern of problem E in 24 minutes like kefaa2? I found it hard for me to solve this kind of problem...
 » 3 years ago, # | ← Rev. 2 →   0 Why my similar approach isn't working for Cfor example: abcd cbabcd cbabcd cbab cbabcdcbab abc printf("L %d\n",n-1); printf("R %d\n",n-2+1); printf("R %d\n",n-2+ n + 1);
•  » » 3 years ago, # ^ |   +1 Don't know if the solution's correct, but maybe put "3" on the top of the answer
•  » » » 3 years ago, # ^ | ← Rev. 2 →   0 Oh, I had forgot to print 3. got it accepted
 » 3 years ago, # |   0 So fast OoO
 » 3 years ago, # |   -8 On problem B, can anyone please tell me what does this statement really means "Waters can move from a square to any other square adjacent by a side, as long as he is still in the grid". Can Waters move from grid[i][j] to grid[i+1][j+1]?
•  » » 3 years ago, # ^ |   0 Yes waters can move from gird[i][j] to 1 position left, or right, or top, or down, on the condition that that position should be in the grid.
•  » » » 3 years ago, # ^ |   0 In this case, 3 S10 101 01F He cannot reach to grid[n][n],then why we have to invert the squares grid[1][2] and grid[2][1]?
•  » » » » 3 years ago, # ^ |   0 Yeah, that's what I had asked as a question during the contest, the reply was "there can be multiple answers"
•  » » 3 years ago, # ^ |   0 "adjacent by side"grid[i+1][j+1] is NOT adjacent by side to grid[i][j]
 » 3 years ago, # |   +11 I really hope this hint/solution format of editorials becomes the norm :)
 » 3 years ago, # |   0 Explain this solution to me please. what is delta? what is delta_a what is delta_b?? I think a and b are the mvoes along the i and j axis. but i cant find formula for delta_a and delta_b. Thanks 95917963 stefdasca
•  » » 3 years ago, # ^ |   +1 I'm not sure why they are named that way in the code, but I recognize that it looks just like what I did. If you set up the linear equations as a matrix, delta is the determinant of the matrix and delta_a/delta_b are the matrix product of inv(A) * (x, y). When divided by delta (the determinant) they give the solutions to the linear equations (i.e. how many steps in each direction are needed).
 » 3 years ago, # |   +12 Magical DP for E: 95899682
•  » » 3 years ago, # ^ |   0 Many red coders have used the dp for E. Can you please explain the dp solution? Thanks :-)
•  » » » 3 years ago, # ^ |   +3 Basically, the first dimension describes the number of minus signs placed modulo 3, and the other dimension is a boolean flag describing whether or not we "broke" the plus-minus-plus pattern or not. In the end, the number of minuses has to be equal to $2n+1$ modulo 3 and the flag must be on.
•  » » » » 3 years ago, # ^ |   0 I have read the editorial and I understand the corner case and the proof.But I still can not understand the way of dp transfer.I have seen this solution: https://codeforces.com/contest/1421/submission/95897364, it is more clear in state transfer.It seems that when we put '+' in an even position (or put '-' in an odd position), we can transfer the illegal state to the legal state.Why we can do this?If you have time, can you explain that? Thank you a lot!
•  » » » 3 years ago, # ^ | ← Rev. 2 →   0 the problem is equivalent to : assign + and — to every number , query the max sum,but "+-+-+-"is illegal and (the number of '-' + n) mod 3 =1. then the dp is obvious.
»
3 years ago, # |
Rev. 2   -29

# define ll long long int

int main() {

string s;
cin>>s;
ll n= s.size();
ll i,ans=0;
for(i=0;i<(s.size()/2);i++)
{
if(s[i] == s[n-i-1])
ans++;
}
if(ans == n/2)
{
cout<<"0"<<endl;
}
else
{
cout<<"1"<<endl;
cout<<"L"<<" "<<n<<endl;
}

}

•  » » 3 years ago, # ^ |   0 the question clearly says only up to n-1,where n is the length of the string and u are using n.
 » 3 years ago, # |   +6 Can someone explain what's the intuition behind the pattern in Problem E. DP aside, it seems very hard to just observe that pattern from the problem. The tutorial proves that it's right but how can we come up with something like that in the contest.
•  » » 3 years ago, # ^ | ← Rev. 2 →   +8 You can brute force it and look at the set of sequences of signs you get
 » 3 years ago, # | ← Rev. 3 →   0 UPD: Found the bug!
 » 3 years ago, # |   0 Can anyone tell me the detailed solution of Problem D. I got the part that we need to consider only two edges but how to calculate the distance after that?
•  » » 3 years ago, # ^ | ← Rev. 2 →   +1 find two or one direction to get to the desired hexagon. Next, try to reduce the cost of transition by adjacent hexagons (c [i] = min (c [i], c [i + 1] + c [i — 1]);). Then you find the number of steps and find the answer in long long. I found the distance just by brute force, but I'm sure that it can be better)).
 » 3 years ago, # |   0 I didn't think about quadrants and shortest paths in problem D
 » 3 years ago, # |   +1 Can someone please explain a solution which involves linear algebra/matrices on problem D?
 » 3 years ago, # |   +34 More like IfElseForces.
 » 3 years ago, # | ← Rev. 2 →   0 An interesting adaption based on B: For a given graph, what is the minimum number of invertions we need to make so that Pink Floyd can successfully go through (1,1) to (n,n)?For example: 4 S000 0000 0001 001F Then the answer should be 1. For if we invert the cell (3,4), Pink Floyd can go from (1,1) to (n,n). And it can be proved that 1 is the most optimal answer. So how to solve this?
•  » » 3 years ago, # ^ |   0 An optimal set of inversions must either only change 0-cells to 1-cells and create a path consisting of 1-cells, or only change 1-cells to 0-cells and create a path consisting of 0-cells. So, you can run two instances of either 0-1 BFS or Dijkstra's algorithm, one to find the cost of making a path of 0-cells, and one to find the cost of making a path of 1-cells. Then the answer will be the minimum of these two values.
•  » » » 3 years ago, # ^ | ← Rev. 2 →   0 Thx a lot !
 » 3 years ago, # |   0 I am surprised that nobody I've seen did binary search for D... (The official solution seems easier, though :p)
 » 3 years ago, # | ← Rev. 2 →   0 I'm sorry if this is duplicated question. Why problems aren't rated after contests?
•  » » 3 years ago, # ^ |   0 I can take time to set problem ratings after end of round.
 » 3 years ago, # |   +1 I got the idea for B using 16 if-else immediately. But later spent 1 hr thinking how to implement it better. Finally ended up with an 8 if-else implementation.. Should have gone with the 16 if-else implementation itself...
 » 3 years ago, # | ← Rev. 2 →   +9 How to prove the conclusion of E((n+m)%3=1) via induction？
•  » » 3 years ago, # ^ | ← Rev. 2 →   0 This is clearly true for the base cases when $n = 1, 2, 3$. Now, consider a general $n$. Suppose for a moment that you have already performed the sequence of operations $n - 2$ times, and are therefore left with just two remaining elements: $X_1$ and $X_2$. Note that $X_1$ is really just the sum (with possible -1s in front) of some elements of $a$. Let $n_1$ be the number of elements in that sum, and similarly define $n_2$. (Note: $n$ = $n_1$ + $n_2$). Let $m_1$ and $m_2$ be the number of elements with -1 in front in $X_1$ and $X_2$ respectively. By our induction hypothesis, we have that $m_1 + n_1 \equiv 1 \text{ (mod 3)}$ $m_2 + n_2 \equiv 1 \text{ (mod 3)}$Let $m$ be the number of terms with -1 in front in our final sum after doing the operation to $X_1$ and $X_2$. Then $m = n_1 - m_1 + n_2 - m_2$.Finally: $n + m \equiv n_1 + n_2 + n_1 - m_1 + n_2 - m_2 \equiv 2n_1 + 2n_2 - m_1 - m_2 \equiv 2n_1 + 2n_2 - (1 - n_1) - (1 - n_2) \equiv 1 \text{ (mod 3)}$
 » 3 years ago, # |   0 It's very nice to see this form of Tutorial.Thanks a lot!
 » 3 years ago, # |   0 MikeMirzayanov any updates when will rating get updated for previous rounds problems ?
 » 3 years ago, # |   0 Can someone explain why ternary search will work for D? I mean ternary search like this:We do ternary search on distance moved along $y=x$ using C1 or C4. Then for remaining we use the Manhattan distance and use the required amount of other direction vectors. Why does this function always form a V shaped graph?
 » 3 years ago, # | ← Rev. 2 →   0 How to do it for multiple bits? Single bit proof is trivial for this : We'll leave it up to you to prove that (a ⊕ (a & b)) + (b ⊕ (a & b)) = a ⊕ b, where ⊕ is the bitwise XOR.Upd: I got the proof. Thanks.
 » 3 years ago, # |   0 If you print: 3 L 2 R 2 R 2n-1 At C task, you'll get ac too, it's good to think about that.
 » 3 years ago, # |   0 The solution for D is tiring. I'm bored of it.
 » 3 years ago, # |   0 D could be solved by running Simplex without making any observations :b
 » 3 years ago, # |   0 problem C: Palindromifierconsider substring s[2...(n-1)] as a single character. Now you get a string of length 3.now apply R 2 L n L n-1 works!example "abc" here 'b' is s[2...(n-1)after operation 1=> abcb //here 1st b is actual b and 2nd one is reversedafter operation 2=> cbabcb // 1st b is reversed , 2nd one is actual and 3rd one is reversedafter operation 3=> bcbabcb // 1st b is actual, 2nd one is reversed, 3rd one is actual and 4th one is reversed
 » 3 years ago, # | ← Rev. 2 →   0 I struggled to solve problem D even after reading the editorial and the comments so I will try to explain it my way: we can notice that each cell(hexagon) can be indicated by (x,y) coordinates same as the picture in the problem statement but we can notice also that each cell can be indicated as diagonals coordinates we have 2 types of diagonals: main diagonal(/) we call it d1 and secondary diagonal() we call it d2 so each cell can be indicated by (d1,d2) coordinates to convert from (x,y) coordinates to (d1,d2): notice that all the cells on the same main diagonal have the same value y-x notice that all the cells on the same secondary diagonal have the same value y so to convert we do this: (d1,d2) = (y-x,y) so we will use the (d1,d2) coordinates instead of (x,y) coordinates in the problem statement we have six operations with costs C1,C2,...C6 each operation might change d1 or d2 or both so the ops array(in my code) stores the changes of d1 and d2(d1x and d2x) of each operation the numbering of the operations follows the one in the picture in the problem statement lets prove that only 2 operations are required in the optimal solution: as we know each operation has another opposite operation op1 opposite of op4 op2 opposite of op5 op3 opposite of op6 that means in the final solution we can use at most 3 operations out of the six because using the operation and it's opposite has no benefit we can also notice that to be able to use 3 operations they have to be adjacent operations in order to avoid using opposite operations together, for example you can use (op1,op2,op3) or (op2,op3,op4) .... or (op6,op1,op2) and lets call each one a triplet we can notice that for each triplet the middle operation can be replaced by the other 2 operations in the same triplet: (op1,op2,op3) --> op2 = op1 + op3 (op2,op3,op4) --> op3 = op2 + op4 ....... (op6,op1,op2) --> op1 = op6 + op2 in the same way we can say for each triplet the other 2 operations can be replaced by the middle one (we choose according to the minimum cost) so if we have a valid solution(not necessary minimal solution) this solution will use one of the triplets and if we do operation replacement for example (replace op2 by op1+op3 or replace op1+op3 by op2) we end up with only 2 operations in the solution(think about it and try it on paper) that means the final solution will have only 2 operations where we have to do the replacement to minimize the cost to solve the problem we need to shift the start cell (0,0) to the target cell using 2 operations and each operation might be used 0 or multiple times so we brute force to try all pairs of operations we notice that shifting a cell to another cell means shifting the d1 diagonal and the d2 diagonal of the first cell to match the (d1 and d2) of the target cell each operation might be used to shift the d1 diagonal or the d2 diagonal so we also brute force 2 possibilities for each operation to try to shift on one of the diagonals each time here is my code
»
3 years ago, # |
Rev. 3   0

For Div2D, I had the following approach

### Theoretical Approach -

Consider that all (X, Y) is such that X>=0 and Y>=0 We have the following setup — To go right, we can take — upright and downright steps. Similarly for going upright, we can take right and upleft.

Note that we don't care for other dimensions, since X>=0 and Y>=0, and other dimensions(namely left, and downleft) take us away from the target.

Finally, we can add up the values — minPossibleRight = min(right, upright + downright) and minPossibleUpright = min(upright, upleft + right)`

### Implementation

We can rotate our coordinate axis such that right>=0 and upright >=0 For rotation, we can consider the following idea —

Click

After rotating the coordinate axis, it becomes much more simpler.

Click
 » 3 years ago, # | ← Rev. 2 →   0 For the problem E : "The proof can be done via induction", I didn't quite get how exactly that will work. Assumption : For $n-1$ elements, let's say we have $m'$ minus signs. So, we will assume that $(n-1+m')\%3=1$. Now we can have the next element as a positive one or a negative one.Say it's positive : Then we have $n$ elements with $m'$ minuses, giving $(n+m'-1+1)\%3 = 2$ which isn't equal to 1.Again, if it's negative : Then we have $n$ elements with $m'+1$ minuses, giving $(n+m'+1+1-1)\%3 = (n-1+m'+2)\%3 = 0$, which again isn't equal to 1. Obviously I am doing something wrong, but can someone point what?