### liouzhou_101's blog

By liouzhou_101, history, 4 months ago,

Author: Milesian

Tutorial
Solution

Author: liouzhou_101

Tutorial
Solution

Author: liouzhou_101

Tutorial
Solution

Author: Milesian

Tutorial
Solution

Author: Milesian

Tutorial
Solution

Author: liouzhou_101

Tutorial
Solution

Author: antontrygubO_o

Tutorial
Solution

Author: liouzhou_101

Tutorial
Solution

• +23

 » 4 months ago, # | ← Rev. 2 →   0 Guys, i dont know about editorial (it is not ready yet) but it is possible to solve C in 1 if statement. Check my submissionUPD: Okay, same as in editorial but anyway... Feel free to ask any questions :)
•  » » 4 months ago, # ^ |   0 Can you explain how you thought of it during the contest ??
•  » » » 4 months ago, # ^ |   +7 Sure! At first lets say thet even numbers are "0" and odd numbers are "1" cause we care only about parity, right? Then i started implementing simple cases like: "1100", "111111", "10000", whatever, and asked myself "what if we gonna add/remove more 0s/1s?" After a while i realized that if Alice gonna use simple copy strategy, then she will always win if amount of 1s % 4 == 0. And then i tried to think about other remainders and came up with complete solution.Sorry for my bad english btw, it is not my native lang :|
•  » » 4 months ago, # ^ |   0 wonderful solution ,but can you please explain me why "(odd + 1) % 4 == 0)" this is independent of parity of 0's ??
•  » » » 4 months ago, # ^ | ← Rev. 3 →   0 Sorry for not answering for a long time.Yes it is. When amount of odd numbers % 4 == 3 it means that Alice can take any odd number on her first move and then just copy Bobs move. This is simillar to case when odd % 4 == 0. Alice always gonna get odd number of 1's + 1 she already have after first move. Just try to implement some trivial exmaples like "111" and add some 0's to this. You'll see that this is actually worksSome codeforces lags below, nevermind
•  » » » 4 months ago, # ^ |   0 Sorry for not answering for a long time.Yes it is. When amount of odd numbers % 4 == 3 it means that Alice can take any odd number on her first move and then just copy Bobs move. Just try to implement some trivial exmaples like "111" and add some 0's to this. You'll see that this is actually works
•  » » » » 4 months ago, # ^ |   0 thank you for you help , i got this
•  » » 4 months ago, # ^ |   0 Fairly sure my submission can also be reduced to only 1 if statements since it also checks all conditions whereby alice wins then if all of those don't apply returns boris.However your solution is more elegant since it entirely disregards the number of 0s (which is cool haha).Also I've coded for like a combined 30 hours so don't bully me for my code haha, advice would be appreciated though (also I did't do this in competition, and have never tried a contest, how much time should this task have taken me?).
•  » » » 3 months ago, # ^ |   0 I strongly recommend you to participate in competitions. Rather, there is no point in not participating in them (except time issue ofc). It doesn't matter how much time you spend on a task, as long as you can solve it yourself. Actually it's the best way to improve in mu humble opinion. The next time you encounter a similar problem, you will solve it faster
•  » » 3 months ago, # ^ |   0 can you please explain me why we are taking modulo with 4?
•  » » » 3 months ago, # ^ | ← Rev. 2 →   0 Just try to analyze some simple case. For example "1111". It doesn't matter who goes first, Alice can always get an even number. Why so? because after one "cycle" of moves amount of odd numbers will decrease by 2 and Alice gonna get exactly 1. So, she needs even number of ones. two 1's multiplied by even number. That is why we are talking about modulo 4. Everything else can be deduced from this case
•  » » » » 3 months ago, # ^ |   0 hey! thanks i got it.
 » 4 months ago, # |   +20 pov : you got wrong answer on test 5 or 36 in problem B :
•  » » 4 months ago, # ^ |   +1 I got wrong answer on test 11 though.
•  » » 4 months ago, # ^ |   0 Somehow we not able to think that zero is greater number than negative . so we can not keep like this 0 0 -6 because we thought that -6 is good to keep instead of dividing it like -3 -3 or -2 -2 -2 and make it bigger
•  » » » 4 months ago, # ^ |   0 if the ans. for testcase 36 is yes ,can u tell me the possible array for that??
•  » » » » 4 months ago, # ^ |   0 Sure 7 4[-6 ,-7, -8 ,-9]Ans={-2 ,-2 ,-1, -1 ,-1, -1 ,-1}
•  » » » » » 4 months ago, # ^ |   0 yeah,i got that before,btw thx
 » 4 months ago, # |   0 This def needs a fix
 » 4 months ago, # |   +5 Why weak pretests for problem B?
 » 4 months ago, # |   -16 Worst round
•  » » 4 months ago, # ^ |   0 damn how is this possible??
•  » » 4 months ago, # ^ |   +1 Something similar happened to me :(
•  » » 4 months ago, # ^ |   0 what is 1167? potencial ranking?
 » 4 months ago, # |   -6 super fast editorial.. great contest.
 » 4 months ago, # |   0 Q. how many weak pretests do you want?? A. YESSS!!!
 » 4 months ago, # |   +19 SpoilerWork out E in 30 minutes after the competition,and FST in problem B(QAQ).My solution for E:Firstly,consider a special case:there's no zero in array $a$.We notice if we put a "partition" between $a[i],a[i+1]$,we must put a "partition" between $a[j],a[j+1]$ as well,where $(a[1]+...+a[i])=(a[j+1]+...+a[n])$ (except the "partition" in the middle).In other words,the "partitions" are in pairs.So the answer is $2^{k},k$ is the number of pairs.Now let's consider an array $a$ including zeros.Remove all zeros from $a$,we get a new array $b$.The same as the special case above,if we put a "partition" between $b[i],b[i+1]$,we must put a "partition" between $b[j],b[j+1]$ as well,where $(b[1]+...+b[i])=(b[j+1]+...+b[n])$.Because there're zeros,the situaion is a bit more complex:Assume there're $x$ zeros between $b[i],b[i+1]$ and $y$ zeros between $b[j],b[j+1]$,the number of plans to place "partitions" is:$\Sigma^{min(x+1,y+1)}_{i=0}C^{x+1}_{i}C^{y+1}_{i}$.Take care of boundary conditions:-all numbers are zeros-the array has zeros at both the beginning and the end-the "partition" in the middle
 » 4 months ago, # |   0 :(
 » 4 months ago, # | ← Rev. 2 →   +20 fsy_jiaxun_when 's currently accepted submission for problem H seems to fail on Ticket 16227 from CF Stress.Can someone hack it for me? or let me know if I constructed an invalid testcase? Thanks.
•  » » 4 months ago, # ^ |   +26 Good job! It's a pity that this case does not appear in system tests.
 » 4 months ago, # |   0 Is there a dp solution for C ? if yes please share
•  » » 4 months ago, # ^ |   +16 Added.
•  » » » 4 months ago, # ^ | ← Rev. 2 →   0 Can you please explain your intuition for C with DP? I did not even realize that this could also be solved with DP.
•  » » » » 4 months ago, # ^ |   0 Intuition is that you can calculate if Alice can win or not in position of 1 even, 1 odd using position 0 even, 1 odd and 1 even, 0 odd. dpAlice[i][j] = !dpBob[i-1][j] || !dpBob[i][j-1] and some other logic.
•  » » » » » 4 months ago, # ^ |   0 Thank you shiro and CoolManGame for explaining and putting your time into replying.
•  » » » » 4 months ago, # ^ | ← Rev. 3 →   0 the states of the game are of the form (howManyEvensLeft, howManyOddsLeft, Alice's current sum's parity, whoseTurnIsIt). from this you could write a recursive function and add memoization (thank god n is only 100 lol)
•  » » » 4 months ago, # ^ |   +1 Could you please explain the z+y+1 in the code?
•  » » » » 4 months ago, # ^ | ← Rev. 2 →   +1 I am not 100% sure, but here are my thoughts. This DP solution was not very intuitive for me but it is clever so if liouzhou_101 can correct me.dp[x][y][z] gives whether Alice or Bob at a particular stage of game can get even/odd parity (z = 0 or 1) from x even and y odd numbers.Where x is the current count of even numbers, y is the current count of odd numbers remaining in listNote that when y is even parity of (z+y+1)%2 changes when moving from Alice to Bob or vice-versa.and when y is odd parity of (z+y+1)%2 remains the same when moving from Alice to Bob or vice-versa.Now in the recursion when moving from Alice->Bob or Bob->Alice we have casesCases for Alice->Bob includeCase 1: y is odd and Alice wants even parity from remaining moves.Bob will try to defeat Alice by taking even parity sum from remaining numbers. Hence Bob will want even parity. So parity remains same from Alice to Bob.Case 2: y is odd and Alice wants odd parity from remaining moves.Bob will try to defeat Alice by taking odd parity sum from remaining numbers leaving only even parity sum for Alice. Hence Bob wants odd parity, same as Alice.Case 3: y is even and Alice wants even parity sum from remaining moves.So Bob will take odd parity sum from remaining moves. We can see parity switches from Alice to Bob.Case 4: y is even and Alice wants odd parity sum from remaining moves,So Bob will take even parity sum from remaining numbers. Again, parity switches from Alice to Bob.So we can see when odd count of odd numbers is there, parity stays same. But if even count, parity switches from 0 to 1 or 1 to 0. The same logic is there for Bob to Alice.so parity switch in recursion is defined as z' = (z+y+1)%2leetcode07 ayush002024 lbm364dl if it helps.
•  » » » » » 4 months ago, # ^ |   0 Thank you. I understood it. To sum up, in general say you want parity $z$, and the total parity is $y$. To achieve this, the other player should get parity $y - z \equiv z + y \mod 2$, so if they don't want you to achieve your goal, they should try to get the opposite parity, which is $z + y + 1$.
•  » » » » » » 4 months ago, # ^ |   0 Yep, that is the mathematical proof which is something I was missing with only the observations. Thank you!
•  » » » 4 months ago, # ^ |   0 can you please explain (z + y + 1) % 2 in your solution?
•  » » 4 months ago, # ^ |   0
 » 4 months ago, # |   0 What's wrong with my solution for problem A?solution
•  » » 4 months ago, # ^ |   0 Try 1 3 1 1 0 83 18 56 
•  » » » 4 months ago, # ^ |   0 Thanks got it
 » 4 months ago, # |   +13 I didn't realize that D was supposed to be a graph problem, and I think the problem can be simplified a lot. Basically, the original (unknown) array $a$ can be partitioned into subarrays that alternate between $\leq k$ and $> k$. For each element in a given subarray, the value it generates in the result is always the last element of the previous subarray. The only exception is the first subarray, which always generates either $0$ or $n + 1$, depending on whether it is $\leq k$ or $> k$ respectively.So what I did was build a 2D vector, where $ind[i]$ stores all of the indices of $b$ that contain the value of $i$. Then each $ind[i]$ actually represents an entire subarray of the unknown $a$. The first subarray is either $ind[0]$ or $ind[n + 1]$ (exactly one will be non-empty). Given the elements of a subarray, exactly one element (the one that appears last in $a$) will be generated by the next subarray while the remaining elements (if any) will not be generated at all. So we can check each element of the current subarray to find which element $x$ has $ind[x]$ as non-empty. Then we need to ensure that $x$ is added last from this subarray, and then move to the next subarray, which is $ind[x]$. Repeat until we reconstruct the entire original array.As for finding $k$, we can add elements from the $\leq k$ subarrays to a set. We can identify whether the first subarray is a $\leq k$ subarray (if it's $ind[0]$) or a $> k$ subarray (if it's $ind[n + 1]$) and then it simply alternates. The value of $k$ is the largest element in this set at the end. My Submission: 174151773It's possible that what I just described is actually identical to the graph approach (where this 2-dimensional vector is the adjacency list?), but I don't think any graph knowledge is necessary at all to easily solve this problem. At the very least, I think this is easier than either the $O(1)$ or the DP solutions for C...
•  » » 4 months ago, # ^ |   +3 I did exactly the same!
 » 4 months ago, # |   +5 terrible pretests for B, what is this trend of skipping common edge cases in pretests?
•  » » 4 months ago, # ^ |   +2 Indeed, no matter whether you believe or not, we didn't realize why many people were hacked. We tried to list every kind of generator in pretests. But even some final tests were provided by real hackings you know?
•  » » » 4 months ago, # ^ |   0 the most common fst was not considering first index of prefix sum array being negative, did u guys really forget to include that basic edge case in pretest?
•  » » » » 4 months ago, # ^ |   +2 No, actually, I think. The system already noticed us all boundary cases were contained by automatically checking.
•  » » » » » 4 months ago, # ^ |   0 no, that case was main test 36 which is not in pretests, you can code a solution ignoring s[0] being negative and check if u want
•  » » » » » » 4 months ago, # ^ |   +21 To be honest, that comes from hack. My bad.
•  » » » » » » » 4 months ago, # ^ |   0 :(
•  » » » 4 months ago, # ^ |   +17 Pretest 2 didn't have any negative numbers; that was the main reason why there were hacks.
 » 4 months ago, # |   0 Can Someone Please elaborate about the second"NO" condition in Problem B.
•  » » 4 months ago, # ^ | ← Rev. 2 →   0 what's the maximum number you can place in the last n-k+1 elements it's the a(n-k+2) which you calculated. Now If the sum of last n-k+1 elements is greater that (n-k+1) * a(n-k+2), you would have to put at least one number greater than a(n-k+2), but then the array won't be sorted.
•  » » » 4 months ago, # ^ |   0 does the n-k+1 elements also includes the first prefix sum element? also thankyou for replying ;)
•  » » » » 4 months ago, # ^ |   0 it includes the n-k+1 element from the original array .. not the prefix sum because given last k prefix sums you can only find last k-1 elements. So the maximum number you can place in first n-k+1 positions is the element found for position n-k+2 or k-1th element from the last because we want to keep the original array sorted.
 » 4 months ago, # | ← Rev. 2 →   +113 Here's my solution to G. I think it's somewhat similar to the editorial. It looks long and complicated, but I've tried to thoroughly explain the motivation behind it, the central idea is fairly straightforward, and I think all the steps are pretty intuitive. I hope it can help anyone confused by the main editorial. Motivation Pt. 1First, notice that the problem doesn't change if we start with an empty grid and place down cells, where we are forced to place cells in grid spots marked 0.For now, ignore the constraints on which cells need to be filled. Let's start by considering when $k = 2$. The most obvious solution would be something like this: XXXXX X.... X.... X.... X.... If you play around with it a bit, you'll notice you can "twist" the path around, and that actually, any path from the bottom left to the top right works. ...XX ...X. .XXX. .X... XX... This motivates looking at the diagonals which go from the top left to the bottom left (or more formally, sets of cells with the same value of $r - c$), since at each step going up or right, we move into a new diagonal (In this next picture, the distinct diagonals we're considering are marked with different letters). EFGHI DEFGH CDEFG BCDEF ABCDE It's easy to show that there can be at most $k-1$ cells in one diagonal (otherwise, there would be a sequence of size $k$ going down and right). Motivation Pt. 2A natural question to ask is, what's the maximum number of cells we can place down under this constraint? Well, notice that the number of cells in each diagonal is $1, 2, \ldots, n - 1, n, n - 1, \ldots, 2, 1$. So there are $2k-2$ diagonals where we can fill every cell (the diagonals with $\leq k - 1$ cells), and a remaining $2n - 1 - (2k - 2)$ diagonals where we can only place $k-1$ cells. Doing the algebra gives: $2(1 + 2 + \ldots + (k - 1)) + (2n-1-2(k-1))(k-1)$ $=k(k-1) + (2n-1-2(k-1))(k-1)$ $=(k-1)(k+2n-1-2k+2))$ $=(k-1)(2n-k+1)$ $=2n(k-1)-(k-1)^2$\$If you think of this expression as the number of cells in 2 $n \times (k-1)$ rectangles, minus one $(k-1)\times(k-1)$ square, it's not hard to see that this is precisely equal to $n^2 - (n-(k-1))^2$: the number of cells we want to remain (if you want, you can do the algebra to convince yourself). So, the maximum number of cells we can place down is exactly equal to the number of cells we need to place down!So we know that in diagonals with $\leq k - 1$ cells, every single cell needs to be filled. Example with $n = 5, k = 4$: ..XXX ...XX X...X XX... XXX.. Let's call these forced cells.What about all the other cells? Well, we know that all the other diagonals must have exactly $k-1$ cells. Let's keep playing around with the problem, and try to find solutions for different $k$.If you try to construct some solutions, after a while, you'll notice that all the solutions you find are composed of $k-1$ disjoint paths going from the lower-left forced cells to the top-right forced cells. For example, with $n=5,k = 3$: ..XFF .XX.F .X..Y FX.YY FFYY. Here, the forced cells are marked F and the paths are marked X and Y. It's also important to note that these paths never go down or left: always right or up.Let's try to show that this $k-1$ paths idea is necessary and sufficient for a solution. Proof sketchNecessaryWe can do this with a kind of induction. We start with the lower-left diagonal, and build up $k-1$ paths.Base case: let's look at the diagonal immediately adjacent to the last forced diagonal in the lower-left. Here's a picture for $n = 5, k = 4$: ..... !.... F!... FF!.. FFF!. The cells we're interested in are marked with !s. Recall that we need to have exactly $k-1$ cells in this diagonal. It's easy to see that no matter how we place these $k-1$ cells, they will form the starting points of $k-1$ disjoint paths.Induction step: if we have $k-1$ paths leading up to some diagonal, it is necessary to extend these $k-1$ paths into the next diagonal. This part's harder to make pictures for, so bear with me.Let's say we want to add a cell in the new diagonal such that it isn't adjacent to any cells in the previous diagonal. This is the only way to not extend the $k-1$ paths.Let's try and make the leftmost cell (we'll denote it $(r, c)$) in the diagonal not adjacent to any cells in the last diagonal. Then, there are 2 cases: $(r, c)$ is strictly to the left of the leftmost cell in the previous diagonal. In this case, you can form a sequence going strictly down and right using $(r, c)$ and all the cells in the previous diagonal, since $(r, c)$ must also be strictly above the leftmost cell in the previous diagonal. $(r, c)$ is strictly below the leftmost cell in the previous diagonal. In this case, you can form a sequence going strictly down and right using the leftmost cell in the previous diagonal, and all the cells in the new diagonal, since $(r, c)$ must also be strictly to the right the leftmost cell in the previous diagonal. Notice that if neither of these conditions hold, then $(r, c)$ must be adjacent to the leftmost cell in the previous diagonal.OK, so we're forced to extend the leftmost path. What about the next one (the second-to-the-left cell in the new diagonal)? Similar reasoning will show that this case is also impossible, and extending the reasoning, that we are forced to extend all of the $k-1$ paths. So, the $k-1$ paths are necessary for the solution. SufficientSuppose that we have $k-1$ paths going up and right. Let's try to construct a sequence of $k$ cells going strictly right and down.By the pigeonhole principle, there must be at least one path that has at least 2 of its cells in this sequence. But this is impossible: if there were two cells $(r_1, c_1), (r_2, c_2)$ in one of these paths such that $r_1 < r_2$ and $c_1 < c_2$, then it would be impossible to get from one of these cells to another going only right and up! So they couldn't possibly be a part of the same path.And so, if we have $k-1$ paths, it is impossible to construct a sequence of $k$ cells going strictly right and down.Alright! So now all we need is to implement it. ImplementationLet's build our paths, starting from the rightmost one. For convenience, we'll put the starting points of our paths in the last forced diagonal in the lower right corner. For example, with $k = 4$, it would look like this (F denotes a forced cell, S denotes the starting point of a path): ..... ..... S.... FS... FFS.. It's not hard to see that any placement of $k-1$ cells in the first non-forced diagonal can be achieved by starting our paths like this.Now, let's build the actual paths. This is where the constraints on which cells we must place come in. Suppose that the endpoint of our path is currently at $(r, c)$. Let's look for points where we need to place down a cell (in other words, that are marked $s[i][j] = 0$ in the input), that are in the same row as our path and to the right of it. We extend our the path to the right to cover all these cells. Then, when there are none left, we move up to row $r-1$. Note that if some cell has already been covered by a previous path, we should ignore it.This greedy idea works because the paths are disjoint ~--- if we didn't go to the right to collect these cells, then some path starting to the left of the current path would have to cross over and collect them, and avoiding this happening can't make our solution worse.In this way, we greedily build our paths. At the end, if there is any cell that needs to be covered that we haven't covered, then our answer is NO. Otherwise we can output YES and our construction. So are we done?Not quite. There's still a possibility that the paths intersect at the very top, and that we haven't actually placed down enough cells. There's an easy fix, though. Iterate through the diagonals from bottom-left to top-right. If there are less than $k-1$ cells in this diagonal, then we place down cells (iterating left to right) in empty spots that are adjacent to cells in the previous diagonal, until we get to $k-1$ cells. Notice that since there are exactly $k-1$ cells in the previous diagonal, there must be at least $k-1$ cells in this diagonal adjacent to a cell in the previous diagonal. So we can always place down enough.The proof that this preserves the paths is a bit harder to justify. Intuitively, if two paths ever intersect, then they must be mashed together into the leftmost part of the diagonal, and adding cells to the right gives the mashed together paths a new cell to move into.My AC code: 174160936
 » 4 months ago, # |   +3 Video editorial of Problem D.
 » 4 months ago, # | ← Rev. 2 →   +46 It is stupid that people so heavily downvote the editorial when their complain is not about the editorial
 » 4 months ago, # |   +15 Seems that people have complaints about FSTs, but If every solution passing pretests will also pass system tests, what is the purpose of pretests and system tests after all?
•  » » 4 months ago, # ^ |   +3 system tests is just to ensure that some heurestics which is not the actual solution doesnt pass, it doesnt mean that edge cases which really should be in the pretests get missed out. Checking logic is more important than the ability to code edge cases
•  » » » 4 months ago, # ^ |   0 Didn't know that. Thanks for replying!
 » 4 months ago, # |   0 please teach my code wrong, i am stupid.(cry)174184654
 » 4 months ago, # |   +20 I lost my GM account after the contest, for I didn't got F correct after some wrong attempts.Very sad :(
 » 4 months ago, # | ← Rev. 3 →   +9 Can someone see what went wrong in my DP solution in C? 174187246
•  » » 4 months ago, # ^ |   0 Here is an example test case: 5 1 2 3 4 5 The answer should be "Alice", not "Bob".When you initialized your DP, you set dp[2][1][1] to be false. However, if Alice picks the (only) even number, Bob has no choice but to pick an odd number, allowing Alice to take the other odd number and secure the odd sum as desired. Note: there might be other mistakes, but this is the one I found that explains the issue with this particular test case.
•  » » » 4 months ago, # ^ | ← Rev. 2 →   +4 Thank you! My code got accepted after changing dp[2][1][1] to true; that was a bit silly of me, could have gotten (back) to specialist if I AC'd that problem
•  » » » » 4 months ago, # ^ |   0 It's unfortunate that you missed it in the contest, but at least it works now!To be honest, there was no need to hardcode cases where an index is equal to 2. The furthest back that you check is with indices $i - 2$ or $j - 2$, so as long as you correctly initialize the cases where the index is $0$ or $1$, it should have been fine to build the DP from indices 2 onwards.
•  » » » » » 4 months ago, # ^ |   +1 That's fair too, but previously I had a different DP method where I would need to hardcode this. After figuring out (and implementing) the correct solution I thought that "it wouldn't hurt to just leave the base cases like this, right?", and damn I was wrong, it hurts
 » 4 months ago, # |   +4 Really funny that I went overkill and did DP on C. Well an AC is an AC, I highly appreciate the low contraints though
•  » » 4 months ago, # ^ |   +21 Thanks for understanding! That's also why I set low contraints to this problem.
•  » » 4 months ago, # ^ |   +8 Personally, I still think the DP approach is much easier than the case analysis, since I don't think it is obvious at all as to what the optimal strategy is in each of these cases, or even to figure out that considering the number of odds modulo 4 suffices to characterize most cases. Either way, I think it's a good problem, and it's nice that both approaches are accepted.
•  » » » 4 months ago, # ^ |   0 Yes, that's what I wanted, both approaches are accepted.
•  » » 4 months ago, # ^ | ← Rev. 2 →   -9 I did DP as well but I was reading the input without taking the absolute value modulo 2. Therefore I failed miserably while the DP was correct lol. Rating dropped hard this contest. I also believe DP was more straightforward than the O(1) solution, good thing they kept the input small. After the contest I sadly submitted a correct solution.174190808
•  » » » 4 months ago, # ^ |   +6 I only do this absolute value modulo trick in first several problems, aiming at pointing out something that you might not take care of.I'm more happy to see you guys really learned something from this problem, rather than getting fake and useless contributions from announcement. Best luck!
•  » » » » 4 months ago, # ^ |   +3 Yeah, I'll surely double check the constraints from now on. Many times going slow but steady is better.
•  » » » 4 months ago, # ^ |   +3 tbh, allowing the values to be negative in a problem where you need to check parity is kinda a jerk move, especially since it unfairly punishes those who happened to carelessly check if mod 2 is equal to 1, whereas those who carelessly checked if mod 2 is equal to 0 (like myself, actually) are unaffected.By the way, you probably already know this, but although it's okay to take the absolute value modulo 2, this would not work with a larger modulo, e.g., -4 mod 3 should actually be 2 (C++ gives -1, so you need to add 3), but applying the absolute value first would yield 1, which is incorrect.
•  » » » » 4 months ago, # ^ |   0 I actually didn't know that, never had to deal with problems where I didn't use modulo on some positive value so far. Appreciate the information!
 » 4 months ago, # | ← Rev. 4 →   +1 Beginner friendly dynamic programming solution using memoization for 1738C - Even Number Addicts.174190808$$solve(o, e, w, a)= \begin{cases} w & \text{if } o = 0 \ \text{&} \ e = 0\\ max \{solve(o-1, e, !w, false), \ solve(o, e-1, w, false)\}& \text{if } \text{alice's turn} \\ min \{solve(o-1, e, w, true), \ solve(o, e-1, w, true)\} & \text{if } \text{bob's turn} \end{cases}$$In the equation above, o represents the amount of odd numbers, e represents the amount of even numbers, w is the boolean variable that tells us if alice is winning and finally, a is another boolean variable tha tells us whose turn it is. Of course we are also using memoization to not re-calculate sub-problems. Complexity is $O(n^2)$, the size of the memoization table.
•  » » 4 months ago, # ^ |   +4 That's why I set low constraints. Hope you guys not only know case works but basic DP ideas.
•  » » 4 months ago, # ^ |   0 This is the clearest and most intuitive solution that I have seen.
•  » » » 4 months ago, # ^ |   0 Thank you :)It's definitely the most intuitive for my level, lol. Grandmasters' intuitions are built different.
 » 4 months ago, # |   +1 Can someone explain the dp approach to problem c?
•  » » 4 months ago, # ^ |   0
•  » » 4 months ago, # ^ | ← Rev. 4 →   +2 Let dp[i][j][k = 0/1] be whether A can always get parity k with i odds and j evens. You can see that a move is considered winning if no matter what parity B chooses in the next move, A always have a way to win. Say if A chooses an odd number, it's a winning move if A can always win even if B chooses an odd number or an even number.Consider dp[i][j][k]:Case 1: A chooses an odd number, B chooses an odd number. This is winning if A can always get an opposite parity of k with i-2 odds and j evens, hence dp[i-2][j][1-k].Case 2: A chooses an odd number, B chooses an even number. This is winning if A can always get an opposite parity of k with i-1 odds and j-1 evens, hence dp[i-1][j-1][1-k].You can see that if A chooses an odd number, it's considered winning if dp[i-2][j][1-k] and dp[i-1][j-1][1-k] are both true.Case 3: A chooses an even number, B chooses an even number. This is winning if A can always get the same parity with i odds and j-2 evens, hence dp[i][j-2][k].Case 4: A chooses an even number, B chooses an odd number. This is winning if A can always get the same parity with i-1 odds and j-1 evens, hence dp[i-1][j-1][k].Using the same logic, you can see that A choosing an even is winning if and only if dp[i][j-2][k] and dp[i-1][j-1][k] are both true.Hence we get this dp formula: dp[i][j][k] = (dp[i-2][j][1-k] && dp[i-1][j-1][1-k]) || (dp[i][j-2][k] && dp[i-1][j-1][k]), and the answer of the problem is dp[x][y][0], where x is the number of odd numbers, and y is the number of even numbers in the array.Small note: if j < 2, then we consider dp[i][j-2][k] to be true (since it's not possible to have that particular sequence of moves), and it's the same for the others.Example solution: 174190925
 » 4 months ago, # |   0 For F,I can't quite understand why we should choose an unvisited vertex u "with the largest degree" ? Can someone help me?
•  » » 4 months ago, # ^ |   +14 I think the way it tones down is that we need to find all the connected components and then we can color all the elements in one connected component in the same color. ( because then s_c <= n_c^2 will always hold)If you dont visit the vertex with the maximum degree you might not find the connected components correctly, take for example the image attached belowIf you start with lets say the smallest degree, you will find the connected components as follows If you are confused why its colored like this, then lets say you start with 1 degree vertices you color them and their adjacent 2 degree vertices. Now you pick the 2nd degree vertices (lets say you take the orange one), you look to its right and you see the 1 degree vertex which is already visited, so you will break and not discover the middle vertex ( with 4 degree)But if you explore the maximum degree vertex first, you will end up like this
•  » » » 4 months ago, # ^ |   +6 !Thank you very much for your splendid answer!!!
 » 4 months ago, # |   0 hey my personal opinion but both B and C had unnecessary negative numbers, generally when giving questions involving performing division or modulus, not giving negative numbers is a good idea due to weird stuff in compilers where -1 %2 evaluates to -1. Luckily i didnt get affected in C (did in B) but overall it shouldn't be about who remembers how compilers calculate negative values which causes the difference between a AC and WA
 » 4 months ago, # |   0 For 1738A - Glory Addicts, 174195503 using PyPy3-64 gives TLE, but 174196553 using PyPy3-64 (only difference being that the first one uses len() function and second one counts manually) barely passesand 174196785 with same code as the first one but using Python3 compiler passes with some margin. Can someone explain this sorcery to me :'( I mean firstly why does manually counting instead of using len() is faster, and secondly why is python3 way faster than PyPy3 in this case, as the CF editor says PyPy3 will usually be faster
 » 4 months ago, # | ← Rev. 3 →   +7 Some of the problems are bad.Problem B:Just a template problem.Problem C:Why just let n<=100 ???Problem H:You can find the problem which is even harder than this problem.
 » 4 months ago, # | ← Rev. 2 →   0 It was my first contest. I could only solve 1, what is the rating of the first problem?
•  » » 4 months ago, # ^ |   0 It is still unknown. Usually, few days after the contest the problems get rated.
•  » » » 4 months ago, # ^ |   0 Okay thanks
•  » » » 4 months ago, # ^ |   0 Hey Vito, could you please tell me the difficulty of Codeforces round. Are global rounds comparatively tougher than div3, div4 or equivalent to div2 or div1 ?
•  » » » » 4 months ago, # ^ |   0 Let's say $f(x)$ is difficulty of Div.$x$ contest. $f(4)$ $<$ $f(3)$ $<$ $f(2)$ $<$ $f(1)$.In Global rounds there are more problems than in normal Div.2 or Div.1 round. The first few problems are of similar difficulty as typical Div.2 round first few problems. Last few problems in the problemset are usually harder than any Div.2 problems.
•  » » » » » 4 months ago, # ^ |   0 Great!
 » 4 months ago, # |   0 Can anyone help me find what's wrong in my solution 174220349 for Problem C?
•  » » 4 months ago, # ^ |   0 Dear abhinav_99, you should replace if(a[i] % 2 == 1) with if(a[i] % 2).In C99 it is guaranteed that:if B != 0{ if A < 0 then A % B <= 0 if A > 0 then A % B >= 0 if A == 0 then A % B == 0. }After several contests, I found that when failing because of a wrong answer on test >=3, it is always some mild but annoying bugs, because you have already passed many big tests.
•  » » » 4 months ago, # ^ |   0 Thanks CristianoPenaldo
 » 4 months ago, # |   +5 POV: you are rainboy Spoiler
 » 4 months ago, # | ← Rev. 2 →   0 Weirdest thing is happening for my submission for A. I submitted my code during the contest and got TLE and left it at that, but when I optimized the code multiple times I was still getting TLE, so I went and checked the submissions of others and copied and pasted it(because I had written exactly the same code as that solution) and I am still getting TLE for some reason.My Copied SubmissionOriginal Submission by CF userWhy is this happening??edit : I am also doing the same thing as editorial.
•  » » 4 months ago, # ^ |   0 You're using PyPy 3 not Python 3. Does that make a difference? (I have no idea!)
•  » » » 4 months ago, # ^ |   0 It worked!!Wow, I didn't know that PyPy3 and Python 3 would make this much difference. Also when I changed it from PyPy3 to Python 3.x.x it said below that PyPy is usually faster.This is the first time I used Python 3 during submission as well.Thanks a lot of pointing it out!!!
•  » » » » 4 months ago, # ^ |   +10 In PyPy, input and/or output is slower. Adding the following 2 lines of code at the beginning makes your TLE code work in 280 ms: import sys input = sys.stdin.buffer.readline 
 » 4 months ago, # |   0 In problemA my approach is very similar to the editorial's. But over here I get wrong answer for 3rd test case, although I get the correct answer in other compiler.
 » 4 months ago, # | ← Rev. 4 →   0 What is wrong with my submission for problem B? (174286190) Everything seems correct to me... SpoilerIt's just 5 lines of code Codevector b(k); for(auto &x : b) cin >> x; int prev=b[0]/(n-k+1)+(!!(b[0]%(n-k+1))); bool ok=1; for(int i=1; ib[i] || prev>b[i]-b[i-1]) { ok=0; break; } prev=b[i]-b[i-1]; } cout << (ok?"YES":"NO") << '\n'; 
•  » » 4 months ago, # ^ |   0 Take a look at Ticket 16246 from CF Stress for a counter example.
 » 4 months ago, # |   0 c problem,I can't find why is not passed test3?help help me 174499898
•  » » 4 months ago, # ^ |   0 Take a look at Ticket 16236 from CF Stress for a counter example.
•  » » 4 months ago, # ^ |   0 In the testcases where x is negative and odd,x%2 will be -1 but not 1
•  » » » 4 months ago, # ^ |   0 thanks,I find this problem
 » 4 months ago, # |   0 For Proble-C, can someone point out an error in my DP solution — 174134613. Thank you.
•  » » 4 months ago, # ^ |   0 Take a look at Ticket 16245 from CF Stress for a counter example.
•  » » » 4 months ago, # ^ |   0 Thanks a lot! I made an error in writing a base case. I made sure to check the base cases like 4-5 times during the content... can't belive I did such a horrible mistake...Btw, what is this CF Stress?? It seemed interesting
•  » » » » 4 months ago, # ^ |   +1 cfstress.com is a website I've created that helps you find the smallest counter example for all upcoming problems on Codeforces. You can be as specific as you want, for example Give me a counter example, where the number of test cases is 3, array size is 7 and array values are in range [2, 9] and it'd give you back a counter example if it exists. All you need to do is provide your submission ID.But of course, it's not free to use.
 » 4 months ago, # |   0 liouzhou_101 can you please explain this statement ~~~~~ (z + y + 1) % 2 ~~~~~ from problem C
 » 4 months ago, # |   0 What an excellent problem C! It brought me back to the my junior-high school time!
 » 4 months ago, # | ← Rev. 3 →   0 c ques can be done without dp.although dp looks bit difficult to implement.just used a if statement for this. here's my code. 174767387 if(n is odd then alice have to select the last move and if odd element is present then he must take that one) so,if array have one or two odd elements then alice loose otherwise always won.and if n is even then alice losse only when 2 odds are present beacuse if he picks first odd then bob picks the other one and alice loose.
 » 4 months ago, # |   0 my solution Can anyone please help me with why my A is wrong? I run the same code in local/ code chef.com/ide and it gives correct output for the test case where it is failing(test 1 test case 1). I have taken types 0 and 1 in two separate vectors b and c; sorted them; and started iterating from end, if we can get another type in front of this, I add this element two times else just one, looks like I have not missed any edge case as well. Any help will be highly appreciated. Thanks :)
•  » » 4 months ago, # ^ |   0 for this test case : 4 0 1 1 1 1 10 100 1000 vector b ==> {1} and vector c ==> {10, 100, 1000}lets take this part of your code : code int bsize = b.size(), csize = c.size(); // so bsize == 1 && csize == 3 int i = bsize-1, j = csize-1; // so i == 0 && j == 2 for(; i >= 0&&j >= 0; ){ if(j != csize-1){ cur+=c[j+1]; } cur+=(2*b[j]); // here is the problem b[2] and bsize == 1, out of range cur+=c[j]; i--;j--; } 
•  » » » 4 months ago, # ^ |   0 Damn!! Thanks! Thanks a lot :), I read the code like 10 times and still couldn't find this typo :( I have always used ubuntu and now I have a windows machine, which I have not yet set up; so I use codechef.com/ide, it should've thrown an error :(
 » 4 months ago, # | ← Rev. 2 →   0 in problem C edututorial "b≡2(mod4) which we have already proved that Bob always loses" b≡2(mod4) which might bobs winning strategy.
 » 4 months ago, # |   0 can anyone please explain the intuition for the problem B?
 » 4 months ago, # |   0 Why we mod by 4 in problem 'C'?
 » 4 months ago, # |   0 When I run this on Codeblocks the first test cases are correct. But after submission it shows different results on first test case. Why is this so?175502330It got accepted if I initialized the bool values in solve function. If I don't initialize them it doesn't work. Please, kindly share the logic behind this. Thanks!
 » 4 months ago, # | ← Rev. 4 →   0 UPD : understood the mistake.