Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

### jdurie's blog

By jdurie, history, 17 months ago, Solution
Solution
Solution
Solution
Solution
Solution  Comments (105)
| Write comment?
 » С sucks
•  » » You sucks
•  » » » Your grammar sucks.
•  » » 17 months ago, # ^ | ← Rev. 3 →   Yes, example test case for B sucks so I got many WA on pretest 2 and problem С also sucks. And I am trying to hit -100 contribution, So downvote me.
•  » » » Why do I get upvotes, I need downvote!!!.
•  » » » » Alright let's click the upvote...
•  » » » Good reverse psychology lol
•  » » » no it's actually helpful
 » Thanks for the fast editorial!
 » Fix picture for C please
 » Statement of Problem A was incomprehensible. These days, I feel that in cf contests it's not the problem that's hard but the way it's written which makes it harder to understand :/ Had to blindly solve it using the given test cases
•  » » 17 months ago, # ^ | ← Rev. 2 →   I agree. I anticipate the description of A as short as AtCoder problems.
•  » » 17 months ago, # ^ | ← Rev. 2 →   NarrationForces
•  » » » Unclear Ad-Hoc based GridForces
•  » » same as problem D
 » 17 months ago, # | ← Rev. 4 →   Let $f[i][j][k]$ be whether there is a path whose end point is $(i, j)$ and the sum on the path is $k$.We can use bitset to calculate this DP with a $O\left(\dfrac{nm(n + m)}{64}\right)$ complexity.Submission: 161091846
•  » » I am not able to get why is the time complexity divided by 64. Would be great if you could explain it.
•  » » » 17 months ago, # ^ | ← Rev. 2 →   It's because of use of the bitset. I am not sure but I think each position contributes only 1 bit to the space complexity and not the space required for storing 64-bit integer (size of long long)
•  » » » The size of the bitset is roughly $\dfrac{(m+n-1)}2$. On 32 bit systems, bit operations on bitsets take $O(\dfrac{n}{32})$, where $n$ is the size of the bitset. This means each operation takes about $O(\dfrac{m+n}{64})$. Then, the loop makes it $O(\dfrac{nm(n+m)}{64})$.
•  » » what is this line doing ? f[i][j] = (f[i - 1][j] | f[i][j - 1]) << a[i][j]; 
•  » » Same way with me and my submission is 161081121
 » 17 months ago, # | ← Rev. 2 →   If the editorial for D doesn't make sense, you can check out this one over here on page 17.
•  » » Thanks. Appreciated (I mistakenly downvoted you and now I can't change it.)
 » 17 months ago, # | ← Rev. 2 →   [submission:161072909]Why is it wrong?
•  » » because k is initialized by a[x][y], that means k equals 0 ( k=a[x][y]; )but when all the values in the grid are negative the result will be incorrect.
•  » » » Thanks
 » There is a solution that does not require any observation in D2 from D1 — just re-root the tree.|First, calculate the answer for any root.If you want to change u to v, push all changes into a stack, recalc dp[u] to what it was before you added v, then recalc v as if it is the root. Then, do it recursively for all children son such that son != u. After you finished, roll back the changes to dp. It takes O(1) to roll back, O(1) to recalc, so it is still O(n)
•  » » I solved it in the same way (sadly after contest).
 » If you are/were getting a WA/RE verdict on problems from this contest, you can get the smallest possible counter example for your submission on cfstress.com. To do that, click on the relevant problem's link below, add your submission ID, and edit the table (or edit compressed parameters) to increase/decrease the constraints.Divison 2 If you are not able to find a counter example even after changing the parameters, reply to this thread (only till the next 7 days), with links to your submission and ticket(s).
•  » » Hello, it generated a wrong testcase for problem C. Ticket We can only have 1 or -1 in the grid, but it showing a testcase with 2.
•  » » » Hi, the generated testcase is actually correct. The input format is the same as the problem, so 1 1 2 -1 1 means that you have one testcase, where the number of rows is 1, number of columns is 2, and the elements are -1 1. (Here, 2 is the dimension of the matrix, not the matrix element).
•  » » » » Oh ok. Sorry!
 » 17 months ago, # | ← Rev. 2 →   Can anyone tell the answer to the test case: 8 2 1 3 4 3 6 2 8 according to cf stress test it should be joe but according to me winner will be mike as in 2nd iteration 2nd element will be already 0 that was joes pile.
•  » » Here the winner is Mike as you said
•  » » » I am really dumb there was a typo in finding the minimum value in my code and due to that i was not able to get ac on b and wasted a lot of time and i suddenly noticed it now.
 » If anyone was curious, you can solve C with the bitset dp: https://codeforces.com/contest/1695/submission/161121586
•  » » what a ironical name!
 » I have some doubt about the solution to problem A. Consider following case: 1 4 4 0 -14 8 9 17 -26 20 30 21 5 -24 -19 -7 -13 14 -4 Based on the solution, the answer is 12. But if we take a subrectangle with $(h, w) = (4, 3)$ and upper-left corner placing on $(1, 1)$, the maximum value inside is 21, which is not the global maximum value(30). So if Joe chooses this subrectangle, Michael will lose.I think the answer should be 16. Am I missing something?
•  » » Michael can instead take a subrectangle with $(h,w) = (3,4)$. this includes the global maximum wherever the subrectangle is.
•  » » » Oh, I completely forgot Michael was the one who chooses h, w values... Thanks!
 » I'm not gonna argue about the quality of the problems (there's already much argument going on), however I think the pretests for A was pretty much garbage 161068768 a solution which scans from -32768 got through pretests. this means that pretests didnt even have a testcase with less than -32768 as minimum 161081758 a weird solution, and I dont understand how it even went through the pretests 161078731 what even is this
•  » » deepmind practicing
 » Is there any way of using dfs in problem C?i can make a graph for possible path from a cell and apply dfs to check whether the sum is zero or not?
•  » » In the worst case(when n,m = 1000), there would be 1998C999(1998 Choose 999) different paths from 1,1 to n,m. (This because we need to make 999 right turns and 999 down turns, so total 1998 turns. Just need to select 999 out of these for a right turn)Which would exceed the time limit easily.
•  » » » Thanks for your nice explanation. Got it!
 » I have done this contest with a really bad result and I think I need to say something. As a habbit, I always initialize the max = -999999999 and min = 999999999, my brain thinks 999999999 > 1e9 and actually I don't understand why I has not had any problems with this before. As you can easily guess, in this contest, I had problems with A.Subrectangle Guess and B.Circle Game. I should have solved A and B in under 15 minutes; I passed pre-tests of problem A, and I never think it will be terrible for me, until now. If I recieved WA in problem A, I would soon find the mistake(max = -999999999), because the idea is clear, I would not have to check the idea, I could concentrate on the code and find my mistake, I just lost 50 points, and next I would easily solve problem B and have over 1h30m to think promblem C or D1. But no, I recieved WA on test 3 in problem B, and although I could prove my idea is correct, I still spent the whole time to find the mistkae in my idea, until some last mintues, I found the mistake in my code, and I passed B on exactly the last minute, and did not have enough time to submit A. As a result, I did not pass the main test of problem A. Anyway, thank you for this contest, if I don't fail in this contest, maybe I will fail in the contests which are more important to me because of this mistake.
•  » » 17 months ago, # ^ | ← Rev. 2 →   instead initialize max = INT_MIN, min = INT_MAX no issue of thinking numbers
•  » » » based opinion
 » The tutorial doesn't have any hints and I don't want to see the solutions directlyCan someone give me some hints for problem E?
•  » » hint1In what situtaion, the answer is -1? hint2For hint 1, the answer is "when there is a connected block with only 1 edge".Otherwise, the puzzle grid alaways could be constructed with a 2*n martix. hint3dfs/eulerian.
 » In problem D, you make your queries first and then you get the distance values, altogether... I'm writing this because I somehow missed/overlooked that crucial point while reading the problem. I guess some others may have unknowingly made the same mistake.
 » 0-1 BFS also works for C with a deque.
•  » » Can you share your implementation?
•  » » » You can see in his submission. here
•  » » » » can u pls explain the implementation using 0-1 BFS
 » 17 months ago, # | ← Rev. 2 →   Can someone please explain why 161082582 got accepted but 161062638 did not in prob A UPD: got it
 » 17 months ago, # | ← Rev. 2 →   Hey jdurie! Feedback as a low-ranking coder:Personally, I feel that the problem statements were a bit unnecessarily lengthy, and thus difficult to understand (especially A). Not much problem with sample test cases (just speaking for myself).Overall, I really liked the contest though, and thank you for the fast editorial. Trying to upsolve a few questions now!
•  » » I don't agree at all, problem A was very clear and sample test cases for B were just horrible.
 » Problem B sample tests were horrible. It was very easy to get them correct. For example, I submited 8 times before getting AC.
 » nice solutions
 » Can someone tell the proof written for Problem C in the editorial in simpler words stating that we can get the sum as 0 if the maximum and minimum path sum ending at (n,m) cover's 0 if the max_sum >= 0 >= min_sum
•  » » Basically they are saying that we can change the path that gave minimum sum to the path that gave maximum sum and on each step of changing that , sum will change by 2,0,-2 . Since we know that number of cells in any path = n+m-1 (which is even) Hence both min_sum and max_sum is also even ,and we are changing min_sum to max_sum by +-2 , so we will definitely hit 0 given that 0 is in the range of [min_sum,max_sum]
 » I guess in problem C's solution the condition for no path should be 0>minnm or maxnm<0 and not 0
•  » » it's correct only, 0
 » Very understandable and clear editorial. Thank you! :)
 » I have not yet learned much, but i keep seeing dp in the discussions C and D. Can anyone explain what is dp ?
 » Can anyone explain the solution for E? Don't really understand why the order of dfs matters.
•  » » I would explain it in a slightly different way, which is similar to the editorial's solution.For any dominoes $(a,\,b)$ we have, add an edge between node $a$ and $b$. Merging the two solutions, we found out that these dominoes form cycles. Therefore we may think about finding the Eulerian Cycle of the graph obtained by two copies of the dominoes. If we successfully found it, the solution is the odd edges and the even ones of the Eulerian Cycles. However, these cycles we found may not guarantee that two dominoes from the different copies have different parity. It can be solved by letting the same dominoes from the different copies be adjacent in the adjacent lists and all the adjacent lists have the same order of edges. Note this is the same we do in the editorial's solution. For each cycle, if the length is $2$ then there must be no solution, otherwise, we arrange them in a $2 \times \frac{\text{length}}{2}$ grid. It is always possible since each connected component contains an even number of edges.
 » The proof part of problem C is brilliant and that's what made the problem a bit hard in my opinion.I was unsure before submitting my solution and got gently surprised when it got accepted.
 » Come on, I didn't even try to implement O(n^3) solution, since I thought it would be too slow. Turns out that it is under 1s even in java..
 » Why not just set $\sum n\times m \leq 10^7$ or else everyone will pass with $O(\dfrac{nm(n+m)}{w})$???I thought of this kind of way to solve in square time complexity, but i still decided to write the code above. However, you can just make the constraints much bigger, and maybe there will be most people coding the tutorial's solution.
•  » » I don't think this solution should be hacked. At least it showed an important way to improve the time complexity($O(\frac{1}{\omega})$). In my point of view, various solutions are also important and inspiring.
 » For problem D1 Div2, in the editorial, can anyone explain the benefit of having this condition: we can assume that either v or some vertex not in the subtree of v has already been queried while calculating the answer.Also D2 Div2, why is this statement true: The way we compute values in the DFS ensures that at least degree[root]−1≥2 subtrees of the root will have at least one query.
•  » » Take an edge and hang the tree with that edge, we get subtree on each endpoint of edge, if we can make each endpoint as Query.Then either of these conditions will be true $dist(u,x) = dist(v,x)+1$ or $dist(v,x) = dist(u,x)+1$. Based on this we can eliminate one subtree If we make root as query node then it is enough to query on all children except last one we can make the above constraint a bit looser, it is enough to have one query node in parent direction
 » 17 months ago, # | ← Rev. 2 →   I think Problem A has a subtle detail and would like to share it here.We are not choosing a minimum area such that any rectangle >= that area covers the maxval. Instead, we are choosing a minimum pair (h, w) such that any h*w rectangle covers the maxval, and by saying "minimum pair" I mean the pairs are ordered by the areas that they form.Those two are different! For example, consider the following case. 1 2 3 4 5 6 7 8 9 10 11 12 99 14 15 16 17 18 19 20 21 22 23 24 25 If we follow the standard answer we get the pair (h, w) = (3, 3) such that any 3*3 rectangle covers 99. But it is not "a minimum area such that any area >= 9 covers 99", for example you can consider the following area. 1 2 3 4 5 6 7 8 9 10 It has an area 10 >= 9 but does not cover 99.
•  » » 17 months ago, # ^ | ← Rev. 2 →   It's not minimum area, it's minimum value for h and minimum value for w.
 » Though my rating dropped after the contest, I think it's a pretty good round.Because it made me realize my poor ability in dp on tree :)
•  » » That's a good example of positive mindset. I see a lotta people perform bad and start trolling the authors, problems or Codeforces itself.
 » Why the timelimit of E is 8s?
 » 17 months ago, # | ← Rev. 2 →   Hey everyone! Can someone please explain the solution for problem C? I do not see how does this proof in the editorial conclude there exists a zero path. Thanks in advance!Statement from editorial I am confused about: Therefore, because both minnm and maxnm are even, and minnm≤0≤maxnm, at some point in this sequence of operations, the sum of the path must be zero. 
•  » » It's just like the intermediate value theorem.
 » got an FST on div2 A :(
 » In D2, how does querying any vertex with degree >= 3 will ensure the answer to be minimum. Why don't we need to do it for all vertices with degree >= 3?
•  » » 17 months ago, # ^ | ← Rev. 2 →   The $ans[v]$ can be understood as "the answer of an subtree with root $v$ if there are at least one query vertex not in this subtree." The condition that the degree of top most vertex $\ge 3$ is to guarantee for all the subtree, there are a least one query point not in it.
 » The Solution Given for "C", does this kind of algo have some name(belong some where like div & conq etc) or just plain problem solving
•  » » 17 months ago, # ^ | ← Rev. 2 →   Intermediate Value TheoremOther IMVT problem: https://www.codechef.com/problems/B01T
•  » » » Thanks bhai!
•  » » » I thought the intermediate value theorem was for continuous valued functions only? This is why I'm still not convinced with the proof for C. How can we say that just because $min_{nm} < 0 \text{ and } max_{nm} > 0 \Rightarrow \exists \text{ a path } p \text{ such that } \Sigma p = 0?$Can we prove that for every value between min and max there exists a path whose sum equals that value?
•  » » » » 17 months ago, # ^ | ← Rev. 2 →   The conditions which you're assuming is not sufficient. You are missing the change at every steps. Change at every step is 2, -2, 0. Previously it was negative even number and later positive even number and everytime there is a change of 2, -2, 0. So you'll get 0 for sure.
•  » » » » » Don't we have to prove that it's always possible to find a path with change +2 or -2?
•  » » » » » » Its already proven in the editorial bro.
•  » » » » The idea is that suppose you already have a path to achieve the max sum, and a path to achieve a minimum sum; the claim is that any sum (of the same parity) in between the max and min are achievable.Now, why should this hold true? The intuition here is that because paths only go right and down, you can convert the two paths between each other by a multiple operations of flipping a "turn" (i.e. if a turn goes right then down, make it go down then right). In particular, we note that each time we flip a turn, it can change the sum by at most absolute value 2. This happens when we step on a -1 instead of a 1 (or vice versa) as a result of flipping. (Note that a single flipping operation changes exactly one cell on the path.)
 » did anyone know why D2 only dfs a vertex more than two sub is ok, but not dfs all vertex which has more than two sub. i think the answer didn't say clearly
 » Problem C was a good one.
 » For problem D, what is the smallest k(the worst situation in any possible hidden-X with an optimal strategy) if we are able to use online query, instead of quering all the node at once?Are they the same?If not the same，what is the best optimal strategy？
•  » » Let's try the following reasoning — root the tree at u — that is our first ask. For every ask concerning two sons, we can answer no as long as there are some other possibilities. This is very similar to our offline dp — we cannot toss out more than all sons by one query. So I think that the answer is the same.
•  » » » thanks！
 » For Problem-D2, why is it that performing our greedy DFS for any of the nodes with degree greater than 3 works?
 » Problem D is a known concept in graph theory called Metric dimension: https://en.wikipedia.org/wiki/Metric_dimension_(graph_theory)#Trees
 » D has appeared in previous competitions: https://atcoder.jp/contests/apc001/tasks/apc001_e
 » 6 months ago, # | ← Rev. 2 →   can anyone help me with memoising C Recursive Codebool check(vector>&vec, ll n, ll m, ll prev, ll i, ll j) { if (i == n && j == m) { if (prev + vec[i][j] == 0) { return true; } return false; } if (j > m || i > n)return false; ll ans = prev; ans += vec[i][j]; bool ok1 = check(vec, n, m, ans, i, j + 1); bool ok2 = check(vec, n, m, ans, i + 1, j); return ok1 || ok2; } void _segfault_() { ll n, m; cin >> n >> m; // vector>dp(n + 1, vector(m + 1, -1)); vector>vec(n + 1, vector(m + 1, 0)); for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= m; j++) { cin >> vec[i][j]; } } bool ans = check(vec, n, m, 0, 1, 1); cout << (ans ? "YES" : "NO") << endl; } 
•  » » BTW i think it can't be
 » HOW TO SOLVE C USING BFS
 » Nice proof of Problem C