vovuh's blog

By vovuh, history, 7 months ago,

1426A - Floor Number

Idea: fcspartakm

Tutorial
Solution

1426B - Symmetric Matrix

Idea: BledDest

Tutorial
Solution

1426C - Increase and Copy

Idea: vovuh

Tutorial
Solution
Solution 2

1426D - Non-zero Segments

Idea: BledDest

Tutorial
Solution

1426E - Rock, Paper, Scissors

Idea: fcspartakm

Tutorial
Solution

1426F - Number of Subsequences

Idea: fcspartakm

Tutorial
Solution

• +102

 » 7 months ago, # |   +4 Wow that was faster than flash!!
•  » » 7 months ago, # ^ |   0 it's from the future.. XD
•  » » » 7 months ago, # ^ |   0 or gf
 » 7 months ago, # |   0 Fast Tutorial. WOW!
 » 7 months ago, # |   -6 Wow that was fast.
 » 7 months ago, # |   +80 I SUCCESSFULLY GAVE MY FIRST CONTEST. I WAS ABLE TO SOLVE ONE QUESTION.THANK YOU FOR THE EDITORIAL AND I HOPE TO PARTICIPATE IN MORE CONTESTS LIKE THIS AND LEARN NEW THINGS .
•  » » 6 months ago, # ^ |   0 why did this comment get so many upvotes?
•  » » 6 months ago, # ^ |   0 Wow
 » 7 months ago, # |   0 Superb round!
 » 7 months ago, # |   +38 I wonder how many people did prove the correctness of their solution to problem E.
•  » » 7 months ago, # ^ |   +41 Max flow :)
•  » » » 7 months ago, # ^ |   0 Can you tell in problem E editorial what is the use of ord vector??
•  » » » » 7 months ago, # ^ |   0 I am guessing that you are asking as to what is vector in cpp.It is another data type very similar to arrays with being more dynamic in memory allocation. You can read about them at https://www.geeksforgeeks.org/vector-in-cpp-stl/.
•  » » » » » 7 months ago, # ^ |   0 I know what is vector I k saying what is use of that in the code logic I want
•  » » » » 6 months ago, # ^ |   0 Basically testing each order(permutations) of the given operations that needs to done before selecting moves which makes Alice wins. So we try to make Alice lose / tie and deplete the rounds as much as possible, before allowing her to win. For this, there are many orders with which we can perform the given moves (before winning rounds), and order vector helps us to generate that permutations.
•  » » » 7 months ago, # ^ |   0 nishant403, please explain how problem E is related to max flows. I tried a lot to think in this direction but could not figure it out.
•  » » » » 7 months ago, # ^ |   +3 consider rock paper scissor as 3 nodes.player1's three nodes : a1,a2,a3player2's three nodes : b1,b2,b3we link a1 with b1,b2, link a2 with b2,b3, link a3 with b3,b1 (for example a1 is rock,b1 is rock ,b2 is paper ,a1->b1 draw,a1->b2 lose)root is the source with infinite flow,the you should maximize b1+b2+b3the n — b1 — b2 — b3 is minimum win. so maxflow can tell your the answer(make b1 + b2 + b3 flow as much as possible from a1,a2,a3)
•  » » » » » 7 months ago, # ^ |   0 Thanks for the insight :)
•  » » » » » 7 months ago, # ^ |   +4 Is maxflow minflow some algo?? Related to graph??
•  » » » » » » 7 months ago, # ^ |   0 yes
•  » » » » » » » 7 months ago, # ^ |   0 What are the prerequisite to learn it??
•  » » » » » » » » 6 months ago, # ^ |   0 BFS/DFS is enough imo. Then you can look at Ford Fulkerson algo which computes the max flow and min cut.
•  » » » » » » » » » 6 months ago, # ^ |   0 In which question max flow and min cut are usefull??
•  » » » » » » » » » 6 months ago, # ^ |   0 Such as bipartite graph matching,which can be solved with the time complexity of $O(\sqrt n m)$.
•  » » » 2 months ago, # ^ |   0 Hello. I should find the flow of the max cost which fill find how many max times I can loose or drow and then minus this value from n?
•  » » 7 months ago, # ^ |   +18 Some guys from the testers (and authors) who do not bother about proofs just wrote mincost maxflow solutions :D
•  » » » 7 months ago, # ^ |   0 Could you please explain the logic behind E? As in, how do we prove that the optimization problem has that solution?
•  » » 7 months ago, # ^ |   +64 Proof by AC
•  » » » 7 months ago, # ^ |   +7 What a nice proof! I think max flows should be in the tutorial.XD
•  » » 7 months ago, # ^ |   0 See my proof for E E simple proof
•  » » 7 months ago, # ^ | ← Rev. 2 →   +1 Can you provide some resource to understand how maxflow works in such problems or at least topic names?
•  » » 7 months ago, # ^ |   0 I wrote the following during the contest, and it got accepted. I see a lot of confusion in the comments about the problem. Can you please check and tell me if this is a correct approach? I am doubtful (after reading so many comments about 'flow') if I got lucky that it passed all test cases. #include using namespace std; int main() { int n; cin >> n; int a[3]; int b[3]; for (int i = 0; i < 3; ++i) cin >> a[i]; for (int i = 0; i < 3; ++i) cin >> b[i]; int ans1 = max(a[0] - (b[2] + b[0]), 0) + max(a[1] - (b[1] + b[0]), 0) + max(a[2] - (b[2] + b[1]), 0); int ans2 = min(a[0], b[1]) + min(a[1], b[2]) + min(a[2], b[0]); cout << ans1 << " " << ans2; } 
•  » » » 6 months ago, # ^ |   0 This is correct. We can prove it by greedy. The structure of answer at the end must be one of the $( a_1, b_2)$, $(a_2, b_3)$, $(a_3, b_1)$ after deleting the other elements optimaly. Suppose $a_1$ has the biggest remaining value after deletion. (The other situations are similar), then $a_1$ is the hardest to delete. We should delete $a_1$ with all $b_1$ and $b_3$. If we waste $b_1,b_3$ in deleting $a_2,a_3$,the answer will be even worse. If $a_1 < 0$ then the answer is $0$ (we can even save elements from the hardest case, therefore $a_2,a_3$ can also be deleted to $0$), otherwise the answer should be $a_1 - b_1 - b_3$.
 » 7 months ago, # |   +3 i wonder if those russian high school students are also checking out the solutions in coddeforces or not ? Nice problemset btw .
•  » » 7 months ago, # ^ |   +9 Nah, the first few stages of this competition are hosted on our local site with some really dumb restrictions I don't want talk about. But, iirc, the third (semifinals) stage is hosted on some local domain of codeforces.
•  » » » 7 months ago, # ^ |   0 When the rating will be updated?
 » 7 months ago, # |   +10 To all the 'wow that was fast' comments, that's what she said...!!
•  » » 7 months ago, # ^ |   +2 Underrated comment
•  » » 7 months ago, # ^ |   0 OP
•  » » 7 months ago, # ^ |   0 Oof
 » 7 months ago, # | ← Rev. 2 →   +2 In problem E, for finding the min wins many people have directly used max(0, a1 — b1 — b3) + max(0, a2 — b1 — b2) + max(0, a3 — b2 — b3) . Can someone prove how this is correct ?
•  » » 7 months ago, # ^ |   0 We need to use max(0, a1 — b1 — b3) a1 for b2, It's similar for other two cases. The only problem is that we use some (b1 + b3) for a1 and some (b1 + b2) for a2, will it be enough? We can see that (b1 + b2 + b3) is greater or equal to (a1 + a2), so it's ok.I think this greedy solution works correctly. However, I'm sorry that I can't give a formal prove.
•  » » 7 months ago, # ^ |   0 Is it actually correct??? maybe just the pretests???
•  » » 7 months ago, # ^ | ← Rev. 2 →   0 I did $max(0, b1 - a1 - a2, b2 - a2 - a3, b3 - a3 - a1)$ and it passed. I have some intuition that it would work but not sure at all. Someone prove this please.
•  » » » 7 months ago, # ^ | ← Rev. 6 →   +11 We have a draw if Bob use the same thing as Alice and losing if Bob counters some specific item. Wins for Alice are situations where Bob have some extra $b'_i$, which cannot be matched with $a_i$ or $a_{(i+1)\%{3}}$. If there are no such extra $b'_i$ then Bob can create outplaying strategy.So, if we have some $b_i$, such that$b_i > a_i + a_{(i+1)\%{3}}$then $b'_i = b_i - a_i - a_{(i+1)\%3}$Our goal is to find all these $b'_i$. We can prove that: there is only one such $b_i$ , if it exists.Proof:Let we have 2 of them, $i \neq j$$b_i > a_i + a_{(i+1)\%{3}}$$b_j > a_j + a_{(j+1)\%{3}}$then$b_i + b_j > a_i + a_j + a_{(i+1)\%{3}} + a_{(j+1)\%{3}}$For every $i, j$ the same situation will appear:$b_i + b_j > a_k + a_m + 2a_{3-k-m}$ $(k \neq m)$Also we have this statement$b_i + b_j + b_{3-i-j} = a_k + a_m + a_{3-k-m} = n$And so, next statement is going to be true$a_{3-k-m} + b_{3-i-j} < 0$Or simply$a_p + b_q < 0$For some $p$ and $q$, but every $a_p$ and $b_q$ are >= 0, so we have a contradiction.So, there is only one group of items with extra items in it or there is no such group. Quantity of these extra items is the answer, otherwise the answer is 0.
•  » » » » 7 months ago, # ^ |   0 I think instead of a(i+1)%3 and a(j+1)%3 there should be a(i+2)%3 and a(j+2)%3,correct me if I am wrong. Anyway great explanation!
•  » » 7 months ago, # ^ |   0 Obviously, this is a lower bound, but can we reach it? Just to prove it (1) (a1-b1-b3)+(a2-b1-b2)+(a3-b2-b3) = -n (2) B1=a1-b1-b3 >= -n (3) B2=a2-b1-b2 >= -n (4) B3=a3-b2-b3 >= -n If two of them are greater than 0, then the other is less than -n, but this is impossible, all three are either less than or equal to 0, or only one is greater than 0. I.If B1<=0,B2<=0 and B3<=0，this is correct. II.Only one is greater than 0.For example, a1-b1-b3>0, and after removing the game that Alice won, a1=b1+b3, so that b1 and b3 consume a1. At this time, b1=0, b3=0, a1=0, obviously a2 , a3 can't win b2
 » 7 months ago, # |   0 Was the pretest for E weak or my solution is correct?
 » 7 months ago, # |   0 Was this tutorial posted even before the contest finish? xD
 » 7 months ago, # |   0 You came early
•  » » 7 months ago, # ^ |   +9 That's what she said
 » 7 months ago, # |   0 Lol I solved E exactly same as written in editorial , I initially thought that it was an overkill but now I realized it was intended.
 » 7 months ago, # |   0 https://codeforces.com/contest/1426/submission/94114892 why this doesn't work?
 » 7 months ago, # |   0 Could anyone please show me the steps for n = 42?
•  » » 7 months ago, # ^ |   +1 For the first 5 steps, add 1 to get [6] as the arrayFor the next 6 steps, append 6 in the array to get [6 6 6 6 6 6 6]
•  » » » 7 months ago, # ^ |   0 thanks
•  » » » 7 months ago, # ^ | ← Rev. 2 →   0 How the author arives at the part that not more than O(sqrt(n)) is required if anyone can help me proving this.
•  » » » » 7 months ago, # ^ |   0 You to make the sum at least n. so best way is to go till sqrt(n) and then just append it. Check the sample cases.
•  » » » » 7 months ago, # ^ |   +7 Assume that we are fixing x as the value taken by all elements of the array, and y as the number of elements. We need to check for all (x, y) such that x * y >= n.For a particular x, y can simply be calculated as y = ceil(n / x). Thus, the number of moves required = (x — 1) + (y — 1) = x + y — 2If you take x beyond sqrt(n), you start getting same values for y for continuous range of x, which implies unnecessarily wasting moves by increasing the value of our array element to a higher x, and appending it the same number of times, y. Consider the case of n = 12x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]y = [12, 6, 4, 3, 3, 2, 2, 2, 2, 2, 2, 1]As you can see, after x = sqrt(n), y takes the same value for a range of x out of which we only need the distinct one's.The distinct values of y (after x = sqrt(n)) are y = [3, 2, 1] and corresponding x = [4, 6, 12] which gives the same (x, y) pairs as traversing x till sqrt(n) did, just the values are swapped! P.S. : not a very formal proof, but I hope this helps you understand why checking beyond sqrt(n) is not required.
•  » » » » » 7 months ago, # ^ |   0 int ans=INT_MAX; for(int i=1;i*i<=n;i++){ int x=ceil((n)/(float)i); ans=min(ans,i-1+x-1); } cout<
•  » » » » » » 7 months ago, # ^ |   +1 Not sure if float causes an overflow, try changing it to double ?
•  » » » » » 7 months ago, # ^ |   +6 Thanks a lot for the help. cuber_coder
•  » » » » » 7 months ago, # ^ |   0 soln Actually, it reduces to this, can you please expain the correctness of the above o(1) solution?
•  » » » » » » 7 weeks ago, # ^ | ← Rev. 2 →   0 The minimum move required = x-1+ceil((n-x)/x)ceil((n-x)/x)=(n-x+x-1)/xSo, minimum move = x+(n-x-1)/xAccording to AM-GM inequality,1/2*(x+(n-x-1)/x) >= sqrt(x*(n-x-1)/x) => x^2+4x-4*(n-1) >= 0We can find the root from the quadratic inequality, x >= 2*sqrt(n)-2for finding minimum move, x=2*sqrt(n)-2Using ceil function because x is not a fractional number. We should ceil as if x should be the minimum possible.
•  » » » » » 5 months ago, # ^ | ← Rev. 3 →   0 can't we use ternary search for this unimodal function 'f(k)' f(k): number of moves required to convert'1' to 'k' and then 'k' to 'n' by adding an element .Please see my implementation, and help by finding the bug.98271033
•  » » » » 7 months ago, # ^ |   0 lets say the number of times we do +1 operation be x,then number of steps will be (x-1)+(n-1)/x,minimimum occurs when x-1=(n-1)/x (using A.M.,G.M. property),hence x=sqrt(n).
•  » » » » » 7 months ago, # ^ |   +1 I think it should be x-1 = (n-x)/x. Got the idea of cuber_coder. Thanks!
•  » » 7 months ago, # ^ |   0 Increase the single element from 1 to 7(6 moves) then eppend that 7 to array 5 times (5 moves) ...total 11 moves...arr=7,7,7,7,7,7
•  » » 7 months ago, # ^ |   0 Increment upto 6 steps and then append 5 times. Incrementing 6 times will give 7 and then appending 5 times means multiplying 7 six times which is 42. Hope this helps!
•  » » 7 months ago, # ^ |   0 a[1]-> a[2]-> a[3]-> a[4]-> a[5]-> a[6]-> a[7]-> a[7,7]-> a[7,7,7]-> a[7,7,7,7]-> a[7,7,7,7,7]-> a[7,7,7,7,7,7]
 » 7 months ago, # |   0 For C: Answer = sqrt(4*N — 1) — 1Is this correct ?
•  » » 7 months ago, # ^ | ← Rev. 4 →   0 let the increment be X, then the answer must be Ans= ceil((n-x)/x) + x-1 So using the arithmetic mean concept answer is greatest when both terms are equal, so ceil((n-x)/x) = x-1 or you can say that x must be in between sqrt(n)-10 to sqrt(n)+10 My submission : 94074745
•  » » » 7 months ago, # ^ |   0 Hi , can you please explain , the reasoning behind considering both terms equal .afaik , Ans >= 2 * (ceil(n-x)/x)*(x-1) acc to AM>=GM . what am i missing ?
•  » » » » 7 months ago, # ^ | ← Rev. 2 →   +2 We know that AM >= GM But we want answer to be minimum possible, That mean AM=GM (least possible) That mean (x+y)/2= sqrt(xy) this is only possible when x=y. => (ceil(n-x)/x) = (x-1) => ((n-x)+(x-1))/x = (x-1) =>. (n-1)/x = x-1 Or we can say that x = sqrt(n) ± 10.. If you are still confused ask me I would be happy to solve them ans pls like this comment :)
•  » » » » » 7 months ago, # ^ | ← Rev. 5 →   0 I am very new to coding, Can you please explain me in problem C, we were trying to make an array but you are using arithmetic mean and geometric mean. How did you reach to that conclusion? I understood solution1 in editorial but cannot reach to AM and GM.
•  » » » » » » 7 months ago, # ^ |   0 In these type of Questions first try to find the function of answer which is dependent on x. Then try to figure out how you would maximize it. Solve as many problems as you can.. this will help alot.. :)
•  » » » » » » » 7 months ago, # ^ |   0 Could you please tell me where can I find such problems ? I want to practise this types of problems more
•  » » » » » » » » 7 months ago, # ^ |   0 I dont know any specific topic, I would suggest keep reading editorial and comments for such tricks :)
•  » » » » » » » » » 7 months ago, # ^ |   +1 Finally I understood AM and GM. Thanks for your help
•  » » » » » 3 months ago, # ^ |   0 i can understand the AM>=GM concept. so (n-1/x) + (x-1) is to be minimised. from here how do you relate it to AM , GM ?Also how did you come to the conclusion that we have to increase till sqrt(n) and then copy
•  » » » » » » 3 months ago, # ^ | ← Rev. 4 →   +3 We know that for two positive numbers a and b,  AM >= GM => (a+b)/2 >= sqrt(a*b) => (a+b) >= 2*sqrt(a*b) and the equality holds only when a=b..so, from there we get that (n-1/x) + (x-1) is minimum only when (n-1/x) = (x-1) solving it we'll get x = 0.5 ± sqrt(n - 1.5) so to be on safe side check for all the values of x form [sqrt(n)-10 , sqrt(n)+10].
•  » » » » » » » 3 months ago, # ^ |   0 Thank you for your valuable time. Also answer my second qn , how did you come to the conclusion that we have to increase till sqrt(n) and then copy ie. before coming up with the before (n-1/x) + (x-1) after taking ceil ie (n-x+(x-1))/x + x-1
•  » » » » » » » » 3 months ago, # ^ |   +3 we have to increase till sqrt(n) We don't have to go till the sqrt, I'm saying that the answer lies between [sqrt(n)-10 , sqrt(n)+10]Also for the ceil part,ceil(x/y) = (x+y-1)/y you can check by putting random numbers.. :)
 » 7 months ago, # |   0 Can anyone please explain the approach in F?
 » 7 months ago, # |   0 How is this possible Editorial was out before the contest xD! The questions were easy but conceptual...Thanks vovuh!
 » 7 months ago, # |   +11 The editorial came in O(1) time.
 » 7 months ago, # | ← Rev. 2 →   -6 I think the problem E should have been B/C question type because, for minimum moves, we had to just generate all permutations in a brute force. Not much logic involved over there!
•  » » 7 months ago, # ^ |   0 this is actually how i do it XD
•  » » » 7 months ago, # ^ |   0 E is not even a problem to 1800, just dont get why testers put a such easy problem to that position
 » 7 months ago, # |   +3 I thought it would be too stupid to generate all combinations on E and I gave up...
 » 7 months ago, # | ← Rev. 3 →   +2 Problem C can be done without even using a loop, basic math. long long i = ceil(sqrt(n)); long long ans = i + (n+i-1)/i - 2; //Print ans 
•  » » 7 months ago, # ^ |   0 can u explain?
•  » » » 7 months ago, # ^ |   0 Try to plot the graph of the formula given in the editorial: x + ceil(x/n) - 2 If you remove the ceiling from the expression, the resulting expression x + x/n - 2 is a lower bound for the initial expression (because always ceil(x) >= x). So, if we find the smallest integer value of that expression for x >= 0, we'll be done. Using some calculus, you can find that this expression is minimized for x = sqrt(n), so ceiling it and plugging it into the original equation will give us the smallest integer value of the expression. (I hope I could explain it correctly...)
•  » » 7 months ago, # ^ |   0 I was thinking the same! Thanks for your solution :)
•  » » 7 months ago, # ^ | ← Rev. 2 →   0 how can you be sure that i = ceil(sqrt(n)) , UPD : got it
 » 7 months ago, # |   0 Can anyone explain for problem C, how adding 1 till we reach a number x and then copying it is the most optimal way?
•  » » 7 months ago, # ^ |   +1 As suggested in the editorial, we can prove this by swapping the order of the two operations and checking which results in a better solution.Let's say you have $x$ in the beginning, if you do increment and add, you'll have $(x+1)+(x+1) = 2x+2$. Instead, if you decide to add and then increment, you'll have $x + x + 1 = 2x+1$. Thus we can see that if we ever do increment after adding to set, we'll always be worse off. That is, we should always increment the value first and then add it.
•  » » » 7 months ago, # ^ |   +1 I am using ternary search to find the minimum of the function given in the tutorial but I am getting WA on test case 2. Could you tell me what is the problem? Submission: https://codeforces.com/contest/1426/submission/94132053
•  » » » » 7 months ago, # ^ | ← Rev. 3 →   +2 The function $F(x) = x + n/(x+1)$ is ternary on real numbers when $x, n >= 0$. But for integers $F(x) = x + ceil(n/(x+1))$ doesn't seem to be ternary. Both functions seem to have their minimum point pretty close tho (I don't have a proof for this. I hope somebody could provide one or even some insight or intuition).You can plug in arbitrary values of n and draw both functions here to visualize it: https://www.desmos.com/calculator.You can avoid this problem in this problem by searching using doubles and try the integers around the minimum point (which may or may not be fractional). I did something similar in this submission: https://codeforces.com/contest/1426/submission/94170281
•  » » » » » 7 months ago, # ^ |   0 What a nice trick!
•  » » » » » » 7 months ago, # ^ | ← Rev. 2 →   +1 Please note that this is not a general trick. It relies on both functions having their minimas pretty close.
•  » » » » » » » 7 months ago, # ^ |   +6 I know, but I guess that the ceil function is what make F(x)=x+ceil(n/(x+1)) not unimodal, so removing the round part give us a better candidate function for ternary search...
•  » » » » » » » 7 months ago, # ^ |   +6 Thanks for the clarification btw
•  » » » » » » » » 7 months ago, # ^ |   0 You're welcome. Thanks for the insights.
•  » » » » » 7 months ago, # ^ |   0 Thanks Bekh. I understood the approach :)
 » 7 months ago, # |   0 Can anyone help me figure out why didn't my solution to C work even though it iterates from 1 to 2*sqrt(n) ? Here's my submission and thank you :)
 » 7 months ago, # |   0 can anyone explain me in C why does x-1 gets added to n-x in sol even though steps for obtaining x have been added initially?
•  » » 7 months ago, # ^ |   0 I was having the same problem instead of explaining I will say take n=5 then u will understand it
 » 7 months ago, # |   +1 Simple Solution for F using combinatorics. 94117068
•  » » 7 months ago, # ^ |   0 Kindly explain your solution.
•  » » » 7 months ago, # ^ |   0 Fix the $b$ in a position and count the number of possible $a$ in the prefix and possible $c$ in the suffix.
 » 7 months ago, # |   0 E felt like easier than A lol
 » 7 months ago, # |   0 Problem A: Can someone explain how did the formula floor(n-3 / x ) + 2 come from? Thanks in advance
•  » » 7 months ago, # ^ |   +2 We have, $2 + x + x + x + ... + x \geq N \Rightarrow 2 + (Step-1) \times x \geq N$This results in $Step \geq \frac{N-2}{x} + 1 \Rightarrow Step = \lceil\frac{N+x-2}{x}\rceil$Now, either you can just use this in your program, or else there is a neat way to do this and that is $\lceil\frac{a}{b}\rceil = \lfloor\frac{a+b-1}{b}\rfloor$. If you use this result, then the above expression converts to $Step = \lfloor\frac{N+2x-3}{x}\rfloor = \lfloor\frac{N-3}{x}\rfloor + 2$.You can try to verify that the ceil to floor conversion works for yourself.
•  » » » 7 months ago, # ^ |   0 I understand the ceil to floor conversion, but would you please elaborate the inequality to equality conversion of Step ≥ N−2x+1 ⇒ Step=⌈ (N+x−2)/ x⌉ ?
•  » » » » 7 months ago, # ^ |   0 Basically, we want the smallest integer which is either equal to or greater than this fraction, which is the definition of the ceiling function.
•  » » » » » 7 months ago, # ^ |   0 Oh, I understood you just transformed the 1 into x / x so you can use the ceiling to flooring conversion. Thank you
•  » » » » » » 7 months ago, # ^ |   0 Actually, you don't even need to do that, you can leave the 1 as is an only change the fraction. It'll come out to be the same.
•  » » » » » » » 7 months ago, # ^ |   0 what is step-1
•  » » » » » » » » 7 months ago, # ^ |   0 Step is the level you are at. So $2$ for the first level and for the remaining $Step-1$ levels you have $x$ rooms, which gives you, $2 + (Step-1)\times x$ total rooms.
•  » » » 7 months ago, # ^ |   0 can you tell me the corresponding mathematical topic of this approach.like how to find the formula
•  » » » » 7 months ago, # ^ |   0 I am not sure how to answer this. I'm tempted to say this comes under linear equations!? After getting the equation, I don't think you need to get the exact formula as stated in the editorial, just solve it as you would on paper.
•  » » » 7 months ago, # ^ |   0 Epic explanation...
 » 7 months ago, # |   0 Why in Problem A, if we remove the first floor, then we subtract 3. And why, after division, we add 2
 » 7 months ago, # |   0 What is the use of ((n-x)+x-1) instead of why not we simply write (n-1) in problem C. Can someone please help me?
•  » » 7 months ago, # ^ |   0 That is for the sake of understanding that we need ceiling of {(n-x)/x} , and a popular way to implement it is to write ((n-x) + (x-1) / x). And for the same reason, if you want ceiling of (n/5) you would write (n+4)/5 !
•  » » » 7 months ago, # ^ |   0 Thanks,Dude :)
 » 7 months ago, # |   +3 Another solution for finding minimum wins for ALICE in E . a1 b1 c1 ---> set with alice a2 b2 c2 ---> set with bob.to minimize wins a1 must be played against a2 and c2 thus a1 depends on a2 , c2 b1 depends on b2 a2 c1 depends on c2 b2.also I claim that a2 handles a1 and b1 . b2 handles b1 , c1 , c2 handles c1 and a1 that is cyclic order. lets supppose any one of the following situation is true.a2>=(a1+b1) --->(1)b2>=(b1+c1) ---->(2)c2>=(c1+a1) ---->(3)in any of this case we can make 2 values of a1,b1,c1=0; example in case of situation 1 a1 b1 can become 0. c1 can be minimised by remaining b2 c2. by subtracting minimum accordingly. thus remaining value of c1 is answer. similarly we can check for other cases.In case any of these situation is not true then simply the minimum value is 0. proved belowProof.Since a2<(a1+b1); b2<(c1+b1)==>(4); c2<(c1+a1) -->(5) since every 3 inequality holds so at first we can reduce any one of a1 and b1 by amount a2 and make a2 completely 0. then from inequality 4 we can make b1 0 and b2 0 or still if we have b2 left we can reduce c1 . now reduces some a1+c1==c2 as we reduced a1 +b1+c1 by amount a2+b2 and since a1+b1+c1==n thus reduced value is n-(a2+b2)==c1. thus remaining sum from first set equals last remaining value. Hence we can make alice lose every match in this scenario.Submission!94112692
•  » » 7 months ago, # ^ |   +3 Small correction brother, thus reduced value is n-(a2+b2)==c2 not c1
•  » » 7 months ago, # ^ |   0 nice observation
 » 7 months ago, # |   +4 Can someone explain a solution using flows in problem E? I know only the basics on this topic and I'm really curious how it can be applied in this problem.
•  » » 7 months ago, # ^ |   +5 You can link the source to Alice's options (1-rock, 2-scissors, 3-paper) with their respectives capacities (a1, a2, a3) and the sink to Bob's ones (4-rock, 5-scissors, 6-paper) with capacities (b1, b2, b3). Then link the options that make Alice not win (draw or lose) mentioned in the editorial with infinite capacity and just calculate the max flow in that network. The edges will be: (source, 1) (source, 2) (source, 3) (1, 4) (1, 6) (2, 4) (2, 5) (3, 5) (3, 6) (4, sink) (5, sink) (6, sink) Note that (4, 5, 6) are Bob's options (rock, scissors, paper) but with other numbers to not repeat nodes. So the minimun number of rounds Alice can win will be (n — max flow) Code: link
 » 7 months ago, # |   0 Can we do the third problem using DP?
•  » » 7 months ago, # ^ |   0 no because of time limits. otherwise we can make a recursive solution easily.
•  » » » 7 months ago, # ^ |   0 How? can you tell the recurrence?
•  » » » » 7 months ago, # ^ | ← Rev. 3 →   0 ok so we have to make n from least steps. and our recurrence depends on two things. first -> value we want , second- > max value we have. n = value we want, maxi = max value we have. solve(n, maxi) = 1 + min((solve(n-1, maxi+ 1), solve(n-maxi, maxi)); here base case is if n <= then return 0;
•  » » » » » 7 months ago, # ^ |   0 great!! I couldn't come up with it but got the thought process.
 » 7 months ago, # |   0 https://codeforces.com/contest/1426/submission/94126035Can anyone tell me the error in this sol for D?
•  » » 7 months ago, # ^ |   0 Line 39 — variable cur overflows the range of int
•  » » » 7 months ago, # ^ |   0 i also tried with long and long long but still same error
•  » » 7 months ago, # ^ |   0 You should be using set instead of set because even though you change you cur variable to long long, set value will overflow
 » 7 months ago, # | ← Rev. 2 →   +1 Please look at these two submissions to problem C. Sub1 uses ceil function and Sub2 uses the technique in author's solution 1.Sub2 is accepted while Sub2 gaves TLE. Can you please explain why ceil function lead to TLE? ;_;
•  » » 7 months ago, # ^ |   0 Maybe some problem with double or something. People usually avoid using ceil for these reasons.P.S. I don't know if you know this or not, but C++ has a build-in swap function.
•  » » » 7 months ago, # ^ | ← Rev. 2 →   +4 Done!! I would never use ceil again ;_;.Oh ok, I didn't know about swap.
 » 7 months ago, # | ← Rev. 2 →   +1 Its just me or someone also felt that nowadays problem A,B,C in codeforces Div-2 and Div-3 rounds are of almost same levels?
 » 7 months ago, # | ← Rev. 3 →   +44 another nice solution for F with math.we can make substring abc by taking a charecter b and one a in left(left of b) and one c in right side(right of b)hence for a string s (which doesn't contain '?') we can calculate the number of abc subsequence as the following method : $a_i =$number of a before the positon i$c_i =$number of c after the positon i$ans = \sum_{\text{s[i]=='b'}}{a_i\times c_i}$If we allow '?' in our string then what will happen? :$p_i =$number of ? before the positon i$q_i =$number of ? after the positon iThen for $i^{th}$ position if it is 'b' then in left there might be $a_i + k$ numbers of a , where $0\le k \le p_i$how many string are there that have exactly $a_i + k$ numbers of a before the $i^{th}$ position ?the answer is $\binom{p_i}{k}\times 2^{p_i-k}$(we can do , pretty much the same things for right side)summing up the idea we got our total answer:$\text{answer = }\sum_{\text{s[i]=='b' or s[i]=='?'}}{(\sum_{k=0}^{p_i}{(a_i+k)\times\binom{p_i}{k}\times 2^{p_i-k}}) \times (\sum_{k=0}^{q_i}{(c_i+k)\times\binom{q_i}{k}\times 2^{q_i-k}})}$we can write $(\sum_{k=0}^{p_i}{(a_i+k)\times\binom{p_i}{k}\times 2^{p_i-k}})$ this part as this $(3^{p_i-1}(3a_i+p_i))$this things reduce our solution in :$\text{answer = }\sum_{\text{s[i]=='b' or s[i]=='?'}}{(3^{p_i-1}(3a_i+p_i)) \times (3^{q_i-1}(3c_i+q_i))}$we can easily calculate this in O(n) you can see my solution here
•  » » 7 months ago, # ^ |   0 steinum we can write (∑pik=0(ai+k)×(pik)×2pi−k) this part as this (3pi−1(3ai+1)). Is there any formula for this line you wrote??
•  » » » 7 months ago, # ^ | ← Rev. 3 →   +12 well , I dont know if there is any well known formula for it or not.$\sum{(a_i+k)\times \binom{p_i}{k} \times 2^{p_i-k}}$$\Longrightarrow a \sum{\binom{p_i}{k} \times 2^{p_i-k}} + \sum{k \times \binom{p_i}{k} \times 2^{p_i-k}}$$\Longrightarrow 3^{p_i}a+3^{p_i-1}p_i$for the first part we can define a problem : you have to choose 'k' ball from p ball and color them white, for the rest of the ball you can color them with eigther black or blue this problem can relate $\sum{\binom{p_i}{k} \times 2^{p_i-k}}=3^{p_i}$and, for the second part we can define another problem : you have to choose 'k' ball from p ball and color them white, for the rest of the ball you can color them with eigther black or blue. And from the white balls you can pick one ballthis problem can relate $\sum{k \times \binom{p_i}{k} \times 2^{p_i-k}}=3^{p_i-1}p_i$
•  » » » » 7 months ago, # ^ |   0 2nd formula will give p×pow(3, p-1)
•  » » » » » 7 months ago, # ^ |   0 yes... I got some Typo ... thanks
•  » » » 7 months ago, # ^ |   0 Use binomial expansion like-:First Part- (1+x)^N = (Nc0) + (Nc1)x + (Nc2)x^2 + (Nc3)x^3 ................(NcN)x^N. ---eq(1)Put x = 2 both side and use relation n choose r is equal to n choose (n-r) Left side = 3^N ,Right side = Req sumSecond Part — Differentiate eq1 both side wrt to x : L.H.S- N*[(1+x)^(N-1)]R.H.S becomes the second part after some modifications .Put x = 2 like first part L.H.S= N*(3^(N-1))Hope it helps.
 » 7 months ago, # |   0 Can C be solved using Binary search ? it worked on sample test cases but i guess i was not able to converge the search space?
•  » » 7 months ago, # ^ |   0 You can solve it using the ternary search. The answer will first decrease and then increase. But if you have the intuition that the answer won't be too big, you can just iterate until your new answer is better than the previous one.
•  » » » 7 months ago, # ^ |   0 https://codeforces.com/contest/1426/submission/94078419 Can't it be done only using binary search?
•  » » » 7 months ago, # ^ |   0 Ternary search may not work. The answer is not strictly increasing or decreasing.
•  » » » » 7 months ago, # ^ |   0 Yes, you're right, Thanks!
•  » » » » » 7 months ago, # ^ | ← Rev. 2 →   0 I have done it using ternary search. The only thing you need to take care is that when the (value at mid) == (value at (mid + 1)) then recursively search for the best answer in both (low, mid -1) and (mid + 1, high) ranges.My submission : 94125048
•  » » » » 7 months ago, # ^ |   0 Actually ternary search can work here..you can have a look at the graph :
•  » » » » » 7 months ago, # ^ |   0 Note x can only take integer values.
•  » » » » » » 7 months ago, # ^ |   0 Oh yes I missed putting floor...but still the graph does decrease then increase/
•  » » » » » » » 7 months ago, # ^ |   0 But not "strictly", for ternary search the graph must be strictly decreasing then strictly increasing.
•  » » » » » » » » 7 months ago, # ^ |   0 But then how to account for those sparsely distributed values on the left side of minima ?
•  » » 7 months ago, # ^ |   0 yeah，I solved C by using binary search：https://codeforces.com/contest/1426/submission/94096870
 » 7 months ago, # |   0 In D, Why do we need to clear the set? Can't we keep the prefixes from the left side and move on?
•  » » 7 months ago, # ^ | ← Rev. 2 →   +5 Then you will count extra. Ex- 1 -2 1 -2 3. In this case you will add a positive no in bw -2 and 1. but you will add extra since you will consider tha array -2 1 -2 3 also as sum=0.. But we have already inserted a no so sum is not 0. Hence we don't want to save arr[0] and arr[1] contribution in set.
•  » » 7 months ago, # ^ | ← Rev. 2 →   0 As far as I understand, when we are inserting the large element to change the sub segement which just now became 0, we can consider the list, 1 2 -2 1 -1 Here, we have prefixes, 1 (1st element) 1 + 2 = 3 (1st 2 elements) 1 + 2 — 2 = 1 (1st 3 elements) Here as we are getting same prefix sum repeatedly we can say the subsegment between the sum 1 and 1 is 0. so, 2 -2 is 0. Now according to the statement we can insert a large element to change this 0 sub segment. We insert into the middle of two elements. We insert here, 1 2 X -2 1 -1 (X = large element) So, we are sure that 1 2 X, if we take this prefix we will never get a 0 sum as X is huge. But later down the road we may end up getting 0 subsegments. So we ignore the whole prefix with the large element X and consider the latter portion, -2 1 -1.
•  » » 7 months ago, # ^ |   0 No, because the prefixes "restart" after you insert a big number.
•  » » 7 months ago, # ^ |   0 Turns out we don't need to clear the previous set if we keep changing the big number. Link to my Solution
 » 7 months ago, # | ← Rev. 2 →   0 Please explain what this line means in E : "It can be shown that if we started using some combination we are better to end it before using the other one. " . Does it means that we need to minimise both of them by minimum of them ?
•  » » 7 months ago, # ^ |   0 It means suppose we are currently Choosing paper for Alice and Scissor for Bob , then we must continue until its not possible to use this combination (because either paper finished or scissor finished or both).
•  » » » 7 months ago, # ^ | ← Rev. 2 →   0 Fine. But then editorial also says that order of these steps does not matter.So i perform for first test case as follows:Take a1 and b1. Nothing changes.Take a2 and b2. Both reduces to 0.Take a3 and b3. Nothing changes.Only value remains are b1 and a3.So minimum value should be 1 using these steps . Where i went wrong ?
•  » » » » 7 months ago, # ^ |   0 You should take first b3 with a2, then b2 with a1.
•  » » 7 months ago, # ^ |   0 So these are the 6 types of combinations in which Alice loses. So it can be shown that if you are going to use some combination more then once, for example — a2 and b1, it is never efficient to use a2 — b1, then use another combination and again a2 — b1. So now you have six different segments of combinations which you got to reorder in the best way. You try all 6! variations and choose the best one.
•  » » » 7 months ago, # ^ |   0 I get it that we can try all orders of combinations.But why editorial says "It is also possible that the order of these combinations does not matter, but we didn't prove that fact " ?
•  » » » » 7 months ago, # ^ |   0 Aah, it means that there might be a constant solution where you don't have to try all variations but they haven't proved it.
 » 7 months ago, # | ← Rev. 2 →   0 Why are we inserting 0 into the set in problem D? I can see in submissions that people inserted 0 initially and then every time the set was cleared 0 was inserted again.UPD: I guess when the whole prefix becomes 0 we need to reset which is why we are using the initial 0 in set.
•  » » 7 months ago, # ^ |   0 Say the prefix sum came out to be 0. You can either check it yourself or just let the set operation check it for you.You can Explicitly check if it was 0, or you can let "the Set" do it for you
 » 7 months ago, # | ← Rev. 2 →   0 For problem E how we will get minimum win as 119 for following example? 319 10 53 256 182 103 34 129 is lowest I can think of.
•  » » 7 months ago, # ^ |   0 First use all your Papers against their Paper and Scissors. In this way, you'll have 119 Papers remaining.
•  » » » 7 months ago, # ^ |   0 Got it. Thank you! I want to know how this max flow works in such problems. I think 94072786 uses max flow.If you can provide me with some resource that would be great. Thank you again.
 » 7 months ago, # |   -6 This contest was one of the greatest programming contest..I have ever seen in my life..(problem statements pleased me!)
•  » » 7 months ago, # ^ |   +3 yeah..that was super..the problem statements are so cool..and I like easy small codes..and wanted more contests from..BledDest,fcspartakm and vovuh..thank you guys for this contest..
 » 7 months ago, # |   0 C was a case work ^^'
 » 7 months ago, # |   0 I think max flow is easier than other approaches for E xD. Maybe it should be written in the tutorial, at least it's interesting.
 » 7 months ago, # |   0 Problem F is a thing of beauty.
•  » » 7 months ago, # ^ |   +3 joy forever
•  » » 7 months ago, # ^ |   +3 John Keats fans...
•  » » 7 months ago, # ^ |   0 can you help me how to handel the case of question mark in problem f??? thanks for help
 » 7 months ago, # |   +1 So,I think I have a more clear solution for D.For every [l,r] which is 0 ,we want to insert as few as possible numbers to break them,we have an idea that find all the [l,r] which equals 0 and select as few as possible points to "touch" all the intervals(which I means that makes every interval has at least one point) and we find it is O(n^2) but the greedy model is classical,we sort the intervals and select as right point as possible and we find that for [l1,r] [l2,r](l2>l1) if we can touch [l2,r] we can touch [l1,r] so,we just store the [l2,r]=0 which the l2 is the biggest.Sorry for my poor English,and it may sound silly...But I hope it will be helpful for you bros
 » 7 months ago, # |   0 Can anybody tell why the best solution will be near sqrt(n) in Problem C
•  » » 7 months ago, # ^ |   0 We should get n (or nearest) by multiplying two numbers and the sum of two numbers should be minimum (greedy) hence, the rule!
•  » » » 7 months ago, # ^ |   0 Which numbers do we multiply? We can only do +1 or copy (sum). Where is the multiplication?
•  » » » » 7 months ago, # ^ |   0 You increment some times ok, this takes you to $X$. This will take approx $X$ steps. Now you'll add it $Y$ times to make the sum $N$. This will take $Y$ steps. You want $X+Y$ to be minimum, given $XY=N$. Which gives you the desired condition of approx $\sqrt{N}$
•  » » » » » 7 months ago, # ^ |   0 Finally got it, thanks. Also used the example with 42 above. Summarizing:We +1 until we reach xThen we copy: add x times x (multiply x*x)e.g. for 42:First five moves: [1] -> [2] -> [3] -> [4] -> [5] -> [6]Next six moves: -> [6,6] -> [6,6,6] -> [6,6,6,6] -> [6,6,6,6,6] -> [6,6,6,6,6,6] -> [6,6,6,6,6,6,6]Total: eleven moves(noted as ->)
 » 7 months ago, # |   0 For C, How did you come up with the intuition that we only need to go till sqrt(n) instead of n/2?
•  » » 7 months ago, # ^ |   0 This comment might help!
•  » » » 7 months ago, # ^ |   0 It did. It's the same as when we try to find divisors of a number. Thanks
 » 7 months ago, # |   0 How come this submission was accepted? He didnot go through all the permutations. void solve() { int n,a1,a2,a3,b1,b2,b3; cin>>n>>a1>>a2>>a3>>b1>>b2>>b3; int ans2=min(a1,b2)+min(a2,b3)+min(a3,b1); int d1=min(a1,b1),d2=min(a2,b2),d3=min(a3,b3); a1 -= d1; a2 -= d2; a3 -= d3; n -= (d1+d2+d3); int ans1=min(b1,a2)+min(b2,a3)+min(b3,a1); cout<
 » 7 months ago, # |   0 Can anyone tell me on what test case my solution for B fails? It is hacked. Here is my submission.
 » 7 months ago, # |   0 I misunderstood problem C and wrote a completely different solution but it still seems to pass the tests. Can someone hack my solution or tell me why it is correct. This 94146956 is my submission. Also I wonder if anyone else wrote a similar solution?
 » 7 months ago, # |   0 It was my first contest on codeforces . I don't know about the rating system. Did the rating got updated for u guys? I solved 1 question successfully but it is showing 0 ratings for me,
 » 7 months ago, # |   0 can anyone explain problem c
 » 7 months ago, # |   0 i think proplem E must D and D must be E any one see that E is so easy
 » 7 months ago, # |   0 for Problem C why the array could not consists of copies of different numbers?
 » 7 months ago, # |   0 for problem 1 instead of the formula mentioned above i used (ceil((n-2)/x)+1), this too passed all the test cases, could someone explain me whether this is equal to the formula mentioned above or is there any test case where this fails
•  » » 7 months ago, # ^ |   0 It's same, you can use $\lceil \frac{a}{b}\rceil = \lfloor\frac{a+b-1}{b}\rfloor$
 » 7 months ago, # |   0 Why is p(r) and p(l-1) has to be same in problem D??? Can someone please explain it to me
•  » » 7 months ago, # ^ |   0 If p(r) = p(l-1) we can say there ia no change is prefix sum or sum of the interval (l,r) is 0.Arr[l] + arr[l+1]....arr[r] = 0P(r) — p(l-1) = 0So, P(r) = p(l-1)
 » 7 months ago, # |   +3 It is also possible that the order of these combinations does not matter, but we didn't prove that fact.What does this statement mean? I tried running the brute against this:4714 21 1230 9 8But for more than 500 different orders the minimum calculated was different from the actual one.
•  » » 7 months ago, # ^ | ← Rev. 2 →   +5 He might be taking of some special cases Like, a= {3,3,3} and b= {5,5,5}Here the order of permutation doesn't really matter.
•  » » » 7 months ago, # ^ |   +3 Okay, I thought that the order doesn't matter in general because mentioning that it won't matter for some special cases is unnecessary.
 » 7 months ago, # |   0 can any one help me with problem F .... how to handel the case of question mark in DP solution.... thanks for the help
 » 7 months ago, # |   0 In Problem C why this code is giving TLE can anyone tell plz https://codeforces.com/contest/1426/submission/94158754 Thanks in advance
 » 7 months ago, # |   +1 Can anyone explain me 1426D — Non-zero Segments problem's logic please. I am not able to understand the provided solution.
•  » » 7 months ago, # ^ |   +8 Let us define $presum_{k}$ as the prefix sum, $a_k$ as the array. $presum_{k} = \sum_{i=1}^{k} a_i$Let's define $i,j$ as indexs($i < j$)： if $presum_{i} == presum_{j}$, we can know that $presum_{j} - presum_{i} == 0 \rightarrow sum\{a_{i+1}, \cdots, a_{j}\} == 0$In order to avoid this situation， we insert a $+\infty$ behind the $a_j$. Like this: $a_{i+1}, \cdots, a_{j-1}, \stackrel{\infty}{\downarrow},a_{j}$.Then reconsider from $a_j$Part of the core code: void solve(){ int n = read(); unordered_map mp; // 0 --> not in, 1 --> in mp.clear(); LL sum(0), ans(0); for (int i = 0; i < n; ++ i){ int f = read(); sum += f; if (sum == 0 || mp.count(sum)) { ++ ans; mp.clear(); // Simulate inserting an infinite number sum = f; mp[sum] = 1; } else mp[sum] = 1; } cout << ans << '\n'; } 
•  » » » 7 months ago, # ^ |   0 Thanks
•  » » » 7 months ago, # ^ | ← Rev. 2 →   0 hi first of all nice explanation thank you for that.but i have certain doubts why we have clear the prefix sum left of the new element added, why we dont consider it anymore and can you tell how the complexity of the program in the editorial is nlogn?thanks
•  » » » » 7 months ago, # ^ |   0 hi, — Under normal circumstances, we need to add an infinite number to the prefix sum before the insertion positionObviously, after inserting an infinite number, we can guarantee that this situation does not exist： $presum_i == presum_j$So we don’t need to consider the prefix sum before the insertion position.If we do not clear it, it will affect subsequent operations.Like this: $\overset{+\infty}{\overbrace{1}}, \overset{+\infty}{\overbrace{5}}, insert, 1, 5$If you do not clean up, it will appear $mp.count(5)$. Actually that is $5 + \infty$. So cleaning is to simulate the process of adding infinity.i do not konw the extacly complexity of the program in the editorial, because i konw little about py, if the operation in and add are $\mathcal{O}(1)$, the algorithm is $\mathcal{O}(n)$, if the operations are $\mathcal{O}(\log n)$, the algorithm is $\mathcal{O}(n \log n)$
•  » » » 7 months ago, # ^ | ← Rev. 2 →   0 I have a little bit confusion about test case 3 (-1 1 -1 1 -1 1 1 -1 -1) .Here prefix sum is-1 0 -1 0 -1 0 1 0 -1 . I have to insert a number before every 0 according to describe above. Then there is only 4 options. So ,How the result is 6?. Please clarify my fault.
•  » » » » 7 months ago, # ^ |   0 yeah i had this same doubt
•  » » » » 7 months ago, # ^ |   0 OK， I have also had this problem, which is also the key to solving this problem. You insert a number before the first 0 like this $-1, \stackrel{\infty}{\downarrow}, 1, -1, \cdots$then, the prefix sum will be [Reconsider the next element from infinity]: $1, 0, 1, \cdots$To make it clearer： $\begin{array}{ccccccc} &befor\; insert& -1& 0& -1& 0& \cdots \\ &after\; insert& NULL& \underset{new\,\,start}{\underbrace{1}}& 0& 1& \cdots \\ \end{array}$You can find that after inserting a number, a new 0 may be generated while eliminating a 0. Your idea is offline, but this is actually an online problem. Need dynamic processing. in my opinion
 » 7 months ago, # |   0 My solution to F, it maintain 6 cnt for a, ?, ab, a?, ?b, ?? and get the result.
 » 7 months ago, # |   0 I couldn't get the logic of copy and increase problem can anyone help me .
 » 7 months ago, # |   0 Can anyone please tell me how to solve C via Binary Search??
•  » » 7 months ago, # ^ |   0 If you plot the graph of f(x) = (n/x) + x — 2 you will see it looks like graph of quadratic function with (a>0). So we can apply ternary search here to find optimal x at which f(x) is minimized. However no need for that, because we can differentiate the above function to see that x = sqrt(n) ! We can confirm that it is minima by double D or by just looking at graph. Since the actual answer is ceil(n/x) + x — 2 we vary x from sqrt(n)-5 to sqrt(n)+5 to get the minimum as mentioned in tutorial.
 » 7 months ago, # |   0 Hi guys, In problem C: why do we need to add the bolded term? ans = min(ans, x — 1 + ((n — x) + x — 1) / x); and in the tutorial he didn't mentioned this term thanks.
•  » » 7 months ago, # ^ |   +1 It's actually a trick for ceiling Instead of doing ceil(a / b) you just do (a + b — 1) / b
 » 7 months ago, # |   +1 How to solve C if instead of having sum greater than or equal to n it were to be exactly equal to n?
•  » » 7 months ago, # ^ | ← Rev. 7 →   0 The cost for reaching sum exactly n, and for reaching sum at least n is the same. Suppose you are duplicating number i. If n % i = 0 then you reach sum exactly n. Otherwise, while increasing 1 to i with operation one, when you reach n % i duplicate it, this way you save the need to duplicate i one more time, because the n % i will reach the sum to n. As you can see, the cost is the same. Funny thing, same code gets AC in both problems.
 » 7 months ago, # |   0 in c why the solution doesnt go beyon root n?thanks
•  » » 7 months ago, # ^ |   0 If you increment your number to $sqrt(n)$, you won't need more than $sqrt(n)$ copies of it since $sqrt(n) * sqrt(n) == n$
•  » » » 7 months ago, # ^ |   +6 ohh thank you got it.
 » 7 months ago, # | ← Rev. 5 →   +5 As noted, Problem F can have different solutions with varying implementation complexity. Here is a simpler one, imo.At each index, find count of subsequences 'a', 'ab' and 'abc' ( conveniently named as such in the code below ) appearing in all combinations so far. One thing to note is, when appending 'a', the new 'a' appears in how many? count of number of strings so far (call it 'm' below). Leaving out mods in the (pseudo) code for easier reading. a = 0, ab = 0, abc = 0, m = 1; for (char c : s) if (c == 'a') a = a + m; else if (c == 'b') ab = ab + a; else if (c == 'c') abc = abc + ab; else { // '?' abc = 3 * abc + ab; ab = 3 * ab + a; a = 3 * a + m; m = 3 * m } return abc; My submission: 94171796ps: I see this is very similar to DP, except no explicit dp table. It's similar to how you can calculate fibonacci(n) using a=0, b=1, c=1; for(3..n) b = c = a + (a = b); print c; Bad idea to do it in one statement, but meh! you get the idea :)
•  » » 7 months ago, # ^ |   0 i also thought about same solution. but i couldn't think about m. why m= m*3 and a += 2*a + m what i was doing was a = 3*a + 1. can you explain why you used m and how it work a little more.
•  » » » 7 months ago, # ^ |   0 m is total number of different strings so far. For each '?' we have 3 choices ( a, b, c ) so x 3 for each '?'. I updated code to a = 3 * a + m; which is the same. When you see '?', you replace it with one of 'a' or 'b' or 'c'. In each of these cases, you carry over previous counts of a, ab, abc (those not including this index). Additionally, if you include this index, each of them increase by the same factor as in their corresponding cases above. I would suggest you to work out a small case on paper. Some times trying out a case gives you ideas. Eg: Try this "a??b?", Result should be 25.
•  » » 7 months ago, # ^ |   +3 Amazing solution
 » 7 months ago, # |   0 my rating still hasnt updated...
 » 7 months ago, # | ← Rev. 2 →   0 Can someone please explain to me the C part in a less mathematical way or just in a more understandable method for beginners, please. I just can't relate between the logic that's mentioned above and the question. Thanks.
•  » » 7 months ago, # ^ |   0 Let draw some pattern by incrementing and by appending 1.[1],[1,1],[1,1,1],[1,1,1,1].... 2.[1],[2],[2,2],[2,2,2],[2,2,2,2]...... 3.[1],[2],[3],[3,3],[3,3,3],[3,3,3,3].... 4.[1],[2],[3],[4],[4,4],[4,4,4],[4,4,4,4]..... 5.[1],[2],[3],[4],[5],[5,5],[5,5,5],[5,5,5,5].... You can observe that for a given N if you come through a path which including its sqrt then it is taking minimum steps to reach N let say you want to reach N=4 then if you choose 2nd path it will cost minimum steps let say you want to reach N=21 then if you can choose 4th or 5th path it will cost minimum steps so by this, you can conclude that to reach N, first reach to its sqrt then after that start appending it and hence it will need minimum steps. Hope you got it.
•  » » » 7 months ago, # ^ |   0 Oh, now I get it, thanks a lot. Just one last, while solving this question you are supposed to observe this pattern?
•  » » » » 7 months ago, # ^ | ← Rev. 3 →   0 In this question, if you try for some values of N by rough work you will notice this. Here you can also see Y(N which we need to reach) increasing fast wrt to X(minimum steps which we want) like a parabola.
•  » » » » » 7 months ago, # ^ |   0 thanks again man.
 » 7 months ago, # |   0 binary search solution for problem C ???
 » 7 months ago, # |   0 when will the rating get updated?
 » 7 months ago, # |   0 https://codeforces.com/contest/1426/submission/94110938 Can anyone help me why this code is giving wrong answer on test case 10? Problem:D
 » 7 months ago, # |   0 In question 3 how do we know that we get our solution by iterating upto sqrt(n). Is there a mathematical statement? I don't know why but ,I was trying binary search and was getting close answers.
•  » » 7 months ago, # ^ | ← Rev. 3 →   0 If you plot the graph of f(x) = (n/x) + x — 2 you will see it looks like graph of quadratic function with (a>0). It is actually hyperbolic. So we can apply ternary search here to find optimal x at which f(x) is minimized. However no need for that, because we can differentiate the above function to get that x = sqrt(n). We can confirm that it is minima by double differentiation or by just looking at graph.Since the actual answer is ceil(n/x) + x — 2 we vary x from sqrt(n)-eps to sqrt(n)+eps to get the minimum as mentioned in tutorial.
•  » » 7 months ago, # ^ |   0
 » 7 months ago, # | ← Rev. 2 →   0 Can anyone help why this solution not working. I'm not going till sqrt(n) but going to 63944 which should also pass the testcase. I got tle and I'm confused that is time given for every testcase is different. https://codeforces.com/contest/1426/submission/94093082
 » 7 months ago, # |   0 Have a look at my code (accepted) for 1426C - Increase and Copy  int n ; cin >> n ; int a = sqrt(n); int b = n / a ; if(n % a) b++; cout << a + b — 2 << endl ; I hope , you will find it more easy (no loops or iterations) than many other approaches :)
•  » » 7 months ago, # ^ |   0 Please explain your logic !
•  » » » 7 months ago, # ^ |   0 You have to find two integers a and b such that a*b >= n and a+b is minimum possible. These values will be sqrt(n) and ceil(n / sqrt(n)). Approach : You have to increment the 1 (present in array) to a . This will take (a-1) moves. Then append this number a to the array (b-1) times. Ultimately, element a is present b times in array which gives a*b >= n Total moves will be : (a-1) increments + (b-1) appends So answer will be (a + b — 2).Hope it helps :)
 » 7 months ago, # | ← Rev. 2 →   +1 The ratings are not updated yet. Is there any problem with the contest or something else? When can we expect rating change? UPD: the ratings are changed now.
•  » » 7 months ago, # ^ |   0 same doubt
•  » » 7 months ago, # ^ |   0 same question
 » 7 months ago, # |   0 When will the contest be rated?
 » 7 months ago, # |   0 The name of problem C gives a hint to solve it.
 » 7 months ago, # | ← Rev. 2 →   0 For problem E, the answer for the minimum can directly be given as $max({0, b_1 - a_2 - a_3, b_2 - a_3 - a_1, b_3 - a_1 - a_2})$.
 » 7 months ago, # |   0 I understand that number of moves will be x-1+((n-x)/x) but why ((n-x)+x-1)/x ?? I cant understand the "X-1" at the second part
•  » » 7 months ago, # ^ |   0 say you wish to get the sum 9 using 4s. you have to use 3 4s (4+4+4=12) but a simple n/x gives answer 2 in this case. suppose there is a case where we need to get sum 9*k using k's. you need 9 k's. for all n where 9*k < n <= 10*k, the answer has to be 10. (n+k-1)/k ensures this.
•  » » » 7 months ago, # ^ |   0 I didn't get it. sorry
•  » » » » 7 months ago, # ^ | ← Rev. 2 →   0 take x=4. now, - n/x=0 for n=0,1,2,3. - n/x=1 for n=4,5,6,7. and so on. But we need this: - f(n)=0 for n=0. - f(n)=1 for n=1,2,3,4. - f(n)=2 for n=5,6,7,8. we can get this if f(n)=(n+x-1)/x. (we are shifting those x-1 numbers into the next group by adding x-1 to n.)
•  » » 7 months ago, # ^ |   0 To avoid floating point operations we write it as Ceil(A/B) = Floor(A + B - 1 / B); For example Ceil(7/3) = Floor(7 + 2 / 3) = 3 Or Ceil(7/2) = Floor(7 + 1/ 2) = 4 We convert Ceil to floor because in most programming languages floor(A / B) = A / B. Note : This only works for positive values. For negative values we have to make some changes.
 » 7 months ago, # | ← Rev. 2 →   0 can someone explain F?. It is hard to understand from the tutorial.
 » 7 months ago, # |   0 In E, how to proof the maximum number? The editorial doesn't have an detail. Example: 22 5 8 9 11 7 4 The maximum number is min(5,7) + min(8,4) + min(9,11) = 18 And the thing left is 0 4 0 2 2 0 So The answer is 18-2 = 16 ? Alice has 4 scissors and Bob has 2 scissors and 2 rocks, so Alice has to lose 2 more rounds? But the correct answer is 18, so I'm confused. Can anyone explain please ?
•  » » 7 months ago, # ^ |   0 Alice won 18 rounds and lost 2 rounds , there is no points thing here so doing 18-2 is not needed. the maximum number of wins will just be 18
•  » » » 7 months ago, # ^ | ← Rev. 2 →   0 Thanks for clarifying! I misread the statement.
•  » » » » 7 months ago, # ^ |   0 thanks is said by upvoting. :sad:
•  » » » » » 7 months ago, # ^ |   0 I already did it. I guess someone is downvote your comment too.
 » 7 months ago, # |   0 #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long int ll; const int mod = int(1e9 + 7); // problem c ///////////why this code give wrong on 1337 please help.\///////////// // it is optimal to increase and then copy the element // my approch to binary search-> //my search range is l=0 to h=n; here h=n b/c [ maximum moves we can obtain by increasing only] //for example -- //n=42 //l=0 and h=42 //mid=12 moves // 2 2 3 3 4 4 5 5 6 6 7 7 // let x=(mid/2)+1 -->(6+1=7) //so sum=2*((x*(x+1))/2)-2 --> ( -2 for 1 1) //if(sum>=n)then search in the left // if(sum=n)return 1; else return 0; } else// when mid is odd { int x=m/2; x=x+1; ll sum=(x+1)*1ll*(x+1); sum=sum-2; if(sum>=n)return 1; else return 0; } } ll solve(ll n) { if (n==1)return 0; ll l=0,h=n-1; ll mid=l+((h-l)/2); ll ans=INT32_MAX; while(l<=h) { mid=l+((h-l)/2); if(check(mid,n)) { h=mid-1; ans=min(ans,mid); } else { l=mid+1; } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; cin>>t; while(t--) { ll n; cin>>n; cout<
 » 7 months ago, # |   0 can any one explain when m is odd in problem B the answer be NO !!
•  » » 7 months ago, # ^ |   0 How does that happen ?
•  » » 7 months ago, # ^ |   0 Since m*m is not divisible by 2*2 for odd m so you can never tile this m*m square in the first place.
•  » » » 7 months ago, # ^ |   0 think you a lot !
 » 7 months ago, # | ← Rev. 2 →   0 F is a nice problem
 » 7 months ago, # |   0 soln can someone tell why this solution works for problem c?
 » 7 months ago, # | ← Rev. 2 →   0 easy solution for C formula...(ceil)(((sqrt(n)-1)*2.0))...
•  » » 7 months ago, # ^ |   0 Can you please explain, how you arrived at this conclusion?
 » 7 months ago, # |   0 in rock paper scissors i got the max win part but i am not able to i understand the min win part
 » 7 months ago, # |   0 int main() { int o=1,a=0,ab=0,abc=0,n; scanf("%d",&n); scanf("%s",s+1); for(int i=1;i<=n;i++) { if(s[i]=='a')a=(a+o)%mod; if(s[i]=='b')ab=(ab+a)%mod; if(s[i]=='c')abc=(abc+ab)%mod; if(s[i]=='?') { abc=(3ll*abc+ab)%mod; ab=(3ll*ab+a)%mod; a=(3ll*a+o)%mod; o=o*3ll%mod; } } printf("%d\n",abc); return 0; } Can anyone tell why we are changing abc,ab,a,and o values in reverse order in s[i] = '?' section and what is the significance of using variable 'o' here
•  » » 7 months ago, # ^ |   0 "o" is the total number of possible strings so far (it's equal to 3^(number of ?s))The assignments are done "backwards" because to calculate abc you need the original ab, not the updated one (same for ab and a, and a and o).
•  » » » 7 months ago, # ^ |   0 Got it, Thank you! friend
•  » » » 6 months ago, # ^ |   0 "o" is the total number of possible strings so far. ok but why do we need this. when we find "b", we increase count of "ab" by count of "a" so far. because all previous "a" can pair up with this "b" giving us "ab".... and same with "c", when we find "c" , we increase count of "abc" by count of "ab" so far. because all previous "ab" can pair up with this "c" giving us "abc".but when we find "a", why are we not just increasing count of "a" by one? because "a" doesn't need to be paired up with any previous character?why are increasing count of "a" by number of string? please help
 » 7 months ago, # |   0 can anybody help me to approach problem D? I am not able to understand the editorial also.
 » 7 months ago, # |   0 I need a little help in Problem E :- I thought of solving it like counting number of subsequence of "abc" the transition is trivial to counting number of sub-sequence . i.e if the current characters match then dp[i][j] = dp[i-1][j-1] + dp[i-1][j] and when it doesn't match and current is not equal to '?' then dp[i][j] = dp[i-1][j]. For the question mark part dp[i][j] = dp[i-1][j-1] + 3*dp[i-1][j] actually out of 3 possible character one of it is going to be equal and rest 2 not so this case is just sum of previous 2 cases.Everything is fine but for consecutive '?' it is wrong for eg for the last sample test-case it gives 44 instead of 46. Can anyone provide feedback am i missing some case??
•  » » 6 months ago, # ^ | ← Rev. 3 →   0 You are missing one thing. Let's say $dp(i,\text{'a'})$ is the amount of times $\text{'a'}$ appears in all sequences on prefix $[1\dots i]$. Let's also say that $k_i$ is the amount of question marks on prefix $[1\dots i]$Then, $dp(i,\text{'a'}) = dp(i-1, \text{'a'}) + [s_i = \text{'a' or } s_i = \text{'?'}] \cdot 3^{k_{i-1}}$
 » 7 months ago, # |   0 cannot understand the condition ? please someone elaborate the condition : x-1+((n-x)+x-1)/x
 » 7 months ago, # |   0 Why do we add a 0 to the set after clearing it?
 » 7 months ago, # |   0 Can anyone tell what is wrong in my code pleasehttps://codeforces.com/contest/1426/submission/94357901
 » 7 months ago, # |   0 Can someone explain problem D
 » 7 months ago, # |   +3 #include #include using namespace std; int main(){ long long MOD=1000000007; int n; cin>>n; string s; cin>>s; long long a=0,b=0,c=0,mul=1; for (int i=0;i
•  » » 7 months ago, # ^ |   0 Can you explain it?
•  » » » 6 months ago, # ^ |   0 Of course. a : the number of substring "a" b : the number of substring "a...b" c : the number of substring "a...b...c" mul: the number of strings recently So u can see that when strings were added - "a": a += mul; - "b": the number of substring "a..b" increases by the number of "a" - "c": the same as b; - "?":this is more interesting. The number of strings, a, b, c multiplies by 3 and then increases by 0, mul, a, b respectively. So the answer is c.
 » 7 months ago, # |   0 can anyone explain how he find min numbers of win in E problem I didn't understand what he was telling in tutorial.
•  » » 7 months ago, # ^ |   0 try all the combinations.6C2=720 ways to permute all possible options to find the best way. Use next_permutation from C++ for trying all possible combinations.
 » 7 months ago, # |   0 Hi everyone,I am struggling with Div3 E problem(Stone Paper Scissors).Actually i don't understand how to find the minimum wins even after looking the editorial as well as other coders's code? What is the concept of max-flow as well as min-flow in that question ? Can anyone explain it to me ? Thanks in advance
 » 7 months ago, # |   0 In problem E the order matters. Random order will give WA.
 » 7 months ago, # |   0 Can anyone explain me why this logic was not working for question 1 --->~~~~~ `float n,x; cin>>n>>x; cout<
 »