Tutorial is loading...
Authors: AleXman111, sdryapko
author's solution: 100959880
Tutorial is loading...
Author: Vladik
author's solution: 100960218
Tutorial is loading...
Author: Vladik
author's solution: 100960332
Tutorial is loading...
Author: Vladik
author's solution: 100960452
Tutorial is loading...
Authors: AleXman111, sdryapko
author's solution: 100960607
Tutorial is loading...
Author: AleXman111
author's solution: 100960771
B was so similar to this
also an easier version to problem https://codeforces.net/problemset/problem/1393/D
I think your task is a classical example of this algo: https://e-maxx.ru/algo/maximum_zero_submatrix
The contest's task is more original statement (who doesn't like Christmas trees:)))
why is it wrong?i'm so confused.... is there something wrong about "std::string"????
You output an extra space, so the checker thinks it doesn't consist of abc.
thank you so much!!!! -,- i forget my costom "#define", thank you!!!
Video Editorial Problem C: Random Events
Thankyou
Thank you
Thank you , but tbh you could have helped more by making video editorial on E and F . Most can solve C,D from editorial itself .
why is this so heavily downvoted?
I feel it's because of the color. Need to get back to CM maybe.
I have same question... He didn't do anything wrong, just posted a comment with a video link which most probably he put some efforts to make and also which might help others.
On the other hand shitty memes gets 100s of upvotes. I sometimes really wonder why is it so, what do people think before deciding to upvote or downvote.
I think it is because that he commented under someone else and his comment is totally unrelated to the original comment.
That Decision-tree you made was so helpful . Thanks !
I was thinking of another approach for E.
For x<y, what if we change the problem a bit? I mean now instead of filling on starting of day, we fill at the end of day. And now initial k=r-k+l and we swap(x,y).
Now we have a similar problem where y<=x, and the only change is that we are now filling at the end of the day. Would this work?
This won't work since you are required to subtract x at the end of each day but you may or may not add y. But if you swap x and y, you will be essentially adding the original y every day.
Oh,I see. Thanks for replying! :)
What will be the space complexity in Problem D?
$$$O(n\log n)$$$
How do we find it?
Sorry, it's $$$O(n)$$$. The time complexity is $$$O(n \log n)$$$.
thanks....any idea about the maximum size of the set or map used to store all possible sums?
Wrong thought
In the worst case when all the sum are different in the tree then it would be approximately $$$2 * n - 1$$$.
Let's think about a perfect binary tree. Let's say there are $$$n$$$ leaf nodes. Then number of node will be $$$1 + 2 + 4 + 8 + 16 + ... + n = 2*n - 1$$$.
If our tree is not a perfect binary tree then there will be more than $$$2*n - 1$$$ nodes.
When I need to define an array for non-perfect binary tree I declare the array of size $$$4*n$$$. Because there are always atleast $$$4*n$$$ nodes in the tree.
So, the space complexity is $$$O(n)$$$
Why time complexity is $$$\mathcal{O}(n \log n)$$$? I think it is $$$\mathcal{O}(n \log n \log A)$$$, because we have $$$\mathcal{O}(\log A)$$$ recursion depth, and in each level we have less than $$$n$$$ subsegments. We can get subsegment sum for $$$\mathcal{O}(1)$$$ and check it in HashMap for $$$\mathcal{O}(1)$$$. But each time we are going to next level, we need to find middle position using binary search for $$$\mathcal{O}(\log n)$$$ so it will be $$$\mathcal{O}(n \log n \log A)$$$ time, and $$$\mathcal{O}(n)$$$ space.
Yep, wrong thought, so sorry
Thanks!
Size of Q is O(N) not O(NlogN)
plz explain ..
You call recursive solution starting with segment (1,n) this adds one element to the map. Then you call recursive function twice with (1,mid) and (mid+1,n) this adds 2 elements to your map. And so on, so the size will be 1+2+4+8+...+n/2+n which is O(N)
hey can anyone check why my solution to D is failing on testcase 7...I cant seem to find the error.
https://codeforces.net/contest/1461/submission/101091924
i'm facing the same issue my submission
instead of set of int use set of long long
i have done the same way as editorial by calculating all possible sum but i am getting tle. how?? link to my code : https://codeforces.net/contest/1461/submission/126734496
There are quite a few O(n*m) solutions for problem B , not sure why O(n*m*m) is the intended solution. I will share approach i have used .
Let dp[i][j] be the number of spruces having (i,j) cell as the bottom center point of the spruce . then dp[i][j] = min(dp[i — 1][j] + 1, min(left[i][j], right[i][j])) . Here left[i][j] is the number of * to the left of (i,j) and right[i][j] is the number of * to the right of cell (i,j) . Both left and right can be easily computed in o(n*m) . The final answer is simply sum of all dp[i][j]
Link : https://codeforces.net/contest/1461/submission/100925269
Thanks brother.I search for a n^2 solution
If i am passing a new vector in each new recursive call in D then what is space complexity for it.. How should i Calculate time complexity for this??
Link- https://codeforces.net/contest/1461/submission/100962372
Since any element of your initial input array can appear in at most $$$O(log(n))$$$ different vectors , the total space complexity would be $$$O(n*log(n)).$$$ The question should now be : Why would an element be in $$$O(log(n))$$$ vectors in the worst case? Think about the number of your recursive calls. In the worst case it's $$$O(n)$$$ because we might have leaf nodes for all the $$$n$$$ elements in our recursion tree and the number of internal nodes in such tree is $$$n-1$$$, which overall concludes to $$$2n-1$$$ nodes. Coming back to the real question, in a recursive call, an element will either go to the left or to the right vector and in our recursion tree (in the worst case) this element will appear over all the corresponding $$$O(log(n))$$$ nodes (this indeed happens when all the elements in the array is some sort of permutation) and generalizing this for all elements in the input array , we need $$$O(n*log(n))$$$ space. But please note that this $$$O(n*log(n))$$$ space complexity can be further reduced to $$$O(n)$$$ with only passing indexes of your input array to your recursive function.
In problem D,What can be the maximum size of the set or map used to store all the possible sums?
nlogn
O(N)
There would be 2n-1 sums stored for an array of size n in the worst case.
can anyone tell me why this solution fails at testcase 7
I am sure many contestants wouldn't able to solve c if only one example was given. (including me :>)
I didn't even look examples but again C was not hard
What's the complexity of problem E? Where is the proof it can pass in time? Is it obvious?
It's $$$O(x)$$$, because we visit each state of $$$k\mod x$$$ from $$$0$$$ to $$$x − 1$$$ at most once.
Right!! I was thinking x could be up to 10^18 xD There were so many variables I didn't see the 10^6 constraint :(
so true lol
It takes $$$O(1)$$$ time to lower the level as much as possible then add $$$y$$$. Realize that we only add $$$y$$$ $$$a$$$ times where $$$ a \cdot y \equiv 0 \mod x $$$. After this we are back to where we were at in the beginning, so we can stop checking. This takes $$$ a = \frac{x}{gcd(x,y)} $$$ iterations. So it's $$$O(x)$$$.
Interesting solution for E.
My unnecessarily over-optimized solution to problem D for anyone interested.
It ran for almost 0.8 seconds. I don't think it was over-optimized lol.
Looks like you judge optimality of code by its runtime XD XD ...... If you know how to compute and compare complexity you'll understand hopefully.
PS: I removed fast io for readability. Check this code with fast IO.
Yeah, I was just kidding XD, my code is exactly the same.
100958922 Can someone tell me, why it is giving TLE in pretest 1, in single for loop... Though giving correct answer..
You forgot to put
return
in line 40. The code will get stck in infinit loop.C was nice
My solution for E is O(1). It's a bit complicated to explain but if interested, you can see the code here.
Edit 2: It turns out that my algorithm was incorrect at the last step after one user hacked my submission. Read this comment below.
Edit: After some people asked, I decided to give a rough idea of my solution. I strongly suggest you to draw a number line on a paper while reading my solution. $$$[x]$$$ represents $$$floor(x)$$$.
I am calculating the max number of days for which it is possible to maintain the water level between $$$l$$$ and $$$r$$$, and storing this value in $$$num$$$ which I will later compare with $$$t$$$ to output "Yes" or "No". If initially $$$k-x < l$$$ and $$$k+y > r$$$ then clearly $$$num = 0$$$ and the ans is "No".
If $$$x = y$$$, then we can maintain the water level indefinitely and the answer is "Yes", no matter what $$$t$$$ is.
If $$$x > y$$$, then the water level must decrease every day at least by an amount of $$$x-y$$$. Assuming $$$k+y \le r$$$, $$$num = \left[\frac{k-l}{x-y}\right]$$$. However if $$$k+y > r$$$, then $$$k-x$$$ must be greater than or equal to $$$l$$$ and so in the first day, we don't add $$$y$$$ and our new $$$k$$$ becomes $$$k-x$$$. Then using the same logic as above, $$$num = 1 + \left[\frac{k-l}{x-y}\right]$$$ (here $$$k$$$ is the new $$$k$$$).
The difficult case is when $$$y > x$$$. Note that if at any point, if $$$k$$$ takes a value in the interval $$$[r-y+1, l+x-1]$$$ then we cannot maintain the water level for another day. So if the interval itself does not contain any integer points, then we can safely say the answer is "Yes". This happens when $$$(r-y+1) - (l+x-1) \ge 1$$$, or equivalently, when $$$x+y \le r-l+1$$$. Otherwise, there is a "danger zone" (i.e. the interval $$$[r-y+1, l+x-1]$$$) where we don't want to step in. If we are on the right side of that danger zone, it means we cannot add $$$y$$$ and on the left side, we cannot subtract $$$x$$$. Inside the danger zone itself, we cannot do either. If we are on the right, we subtract $$$x$$$ as many times as possible which is equal to $$$\left[\frac{k-l}{x}\right]$$$. After doing this, our $$$num$$$ becomes $$$\left[\frac{k-l}{x}\right]$$$ and we reach $$$k = k - \left[\frac{k-l}{x}\right]\cdot x$$$. If we reach the danger zone (can be checked by checking if $$$k+y \le r$$$ is false), then we are done. Otherwise, we must be on the left side of that danger zone and now we need to add $$$y$$$ to our $$$k$$$. If $$$x$$$ divides $$$y$$$, then we can repeat this process indefinitely (adding $$$y$$$ and subtracting $$$x$$$ multiple times) and in that case, our answer will be "Yes" (I have set
t = -1
in the code because I am comparingnum
witht
at the end). If there is some non zero remainder, call it $$$rem$$$. After adding $$$y$$$ and subtracting $$$x$$$ $$$\left[\frac{y}{x}\right]$$$ times, we will reach at $$$k = k + rem$$$. We keep doing this until we are outside that danger zone i.e. $$$\left[\frac{r-y-k}{rem}\right]$$$ times. We add $$$\left[\frac{r-y-k}{rem}\right]\cdot\left[\frac{y}{x}\right]$$$ to $$$num$$$ and add $$$\left[\frac{r-y-k}{rem}\right]\cdot rem$$$ to $$$k$$$. After taking another step (1 step = $$$\left[\frac{y}{x}\right]$$$ days), either we can land inside the danger zone meaning we are done, or, we can cross over that danger zone. If we land in the danger zone, then we just simply add $$$\left[\frac{y}{x}\right]$$$ to $$$num$$$ and we are done. Otherwise, we reach at $$$k = k + rem$$$ which lies on the right of danger zone. Since $$$rem < x$$$, subtracting $$$x$$$ will surely take us to the left of danger zone and that too on a location which is left to our original location (before taking the step). The "decrease" in our position after $$$\left[\frac{y}{x}\right]+1$$$ days is $$$x-rem$$$. We can afford to let it decrease only $$$\left[\frac{k+rem - (l+x)}{x-rem}\right]$$$ times and that consumes another $$$\left[\frac{k+rem - (l+x)}{x-rem}\right]\cdot(\left[\frac{y}{x}\right]+1)$$$ days. After another $$$\left[\frac{y}{x}\right]$$$ days, we finally land inside the danger zone and we are done.Seeing your code, maybe I need that “complicated explanation “ lol
I have updated my comment.
Respect ++
Please explain if you could, I think $$$O(x)$$$ is not very hard to come up, but I have been trying $$$O(1)$$$ for some time now
I have updated my comment.
I think you have a typo, should be ..
Now reading and understanding rest of comment!
Yes, thanks.
Hi, Can you please explain the
if (rem)
block of your code. Thanks.PS. Those trying to understand problem E try to think of it as, given two points l and r on x-axis and a starting position k, you're allowed to move x units backward and y units forward.
I have updated my comment.
Coming up with danger zone interval was pretty neat. Thanks for the detailed explanation.
The solution is broken with this case
3 1 17 inf 8 10
The process can go on forever3 -> 13 -> 5 -> 15 -> 7 -> 17 -> 9 -> 1 -> 11 -> 3 -> ...
Thank you for pointing this out. Upon hand-tracing my algorithm on your example, I realized that the issue is at the last step. I assumed that after another $$$\left[\frac{y}{x}\right]$$$ days, we will surely land in the danger zone (last line). However, this is wrong since the value of $$$x-rem$$$ can be larger than the length of our danger zone and we may skip over it. To make the algorithm correct, we need to repeat the process all over again with new $$$x = x-rem$$$ and keep doing this until either we reach the danger zone or get $$$rem = 0$$$. In the worst case, $$$x$$$ may only decrease by $$$1$$$ in each iteration and therefore the algorithm becomes $$$O(x)$$$.
This was very unfortunate though :/
I know how can problem B be solved with Dynamic programming, but the explanation in the editorial does not explain 1 single thing about the usage of DP in the problem and why we should use DP instead of just simulating the explanation.
Very poor explanation with very low effort.
when will the rating changes will be published ???
Can someone please explain how to solve the question B ? The solution given in editorial is not clear to me. Thanks
My solution to B is a 2D DP. The total complexity is O(NM) and i guess it is easier to understand too.
Please explain these steps. Thanks.
If the symbol at (i,j) != '*' we skip as no spruce tree with that as top can be formed else we do the dp thing. The dp thing is actually we are seeing what max height trees can be formed by the (i+1,j)(i+1,j-1),(i+1,j+1) as the top and the max height tree that can formed by (i,j) will be min of them+1. You will get the intuition when you draw a few cases and we can further prove it by induction.
dp[i][j] = hieght of max height tree with (i,j) as top
The main idea here is that the height of max height tree that can be made with (i,j) as the topmost piece == no of trees that can be made with (i,j).
Finally that ans is answer to our problem that is the total no of spruce trees = sum of spruce trees with top (i,j) for all i, j.
Why is this downvoted? I just tried to help this guy. Is it ratism? If no please tell me how can I improve my comments and what is wrong in this one?
No man, don't take it in another way... It's just because you directly pasted comment(code) that makes this blog messy...Always put your codes in spoiler and enjoy green color besides your contribution. :P
Can someone please explain E? I get the initial part where y<=x.
Otherwise, let's note, when we use the rise in water level, we change the value of the expression k mod x. At each step of the algorithm, we will lower the water level as many times as we can, and then raise the level.
What does this mean? What exactly is the value k mod x. And why do we lower till the time we can and not add y litres of water somewhere in between of this process?
I think this statement can be helpful to explain the editorial of E: Statement: If some solution exists, there exists a solution in which we add water to the cooler only on days when this is necessary (otherwise, the level of water at the end of the day will be lower than l).
Proof.
Let’s consider some solution to the problem. If this solution adds water only when needed – nothing to prove. If not, consider the first day t0 when the water was added, but this was not necessary. Let’s not add water at the beginning of t0. If this did not break the solution – nothing to prove. What if it broke the solution in some day t1 > t0? The only way we could break the solution at day t1 – we don’t have enough water is the cooler at the end of day t1. Let’s note that it means that we did not pour any water in the cooler at the beginning of t1 (since y>x). Let’s just do it – pour y liters of water into the cooler at the beginning of t1. The solution is fixed on t1, but it is also fixed for all the further timestamps (because for all the t > t1, moving the action of pouring y liters of water into the cooler from t0 to t1 changes nothing). Let’s continue this process until we only pour the water into the cooler when it is necessary. The resulting sequence of actions is a solution, in which we pour the water into the cooler only when necessary.
I'm thinking another approach for E:
Determine a range [r-y+1,l+x-1], if we reach this range at the beginning of day, its impossible.
Then use extended euclidean to find the minimum number of days to reach each number in this range
in problem b to decrease the complexty you need to the bigest sprcues for every position and add to the (weidgh-1)/2 to answer and add the number of * to do that you will need to use binary search or memoistion the number of * before the curent index so the complexty will be n * m * log(n)
EDIT: My question (for F) was resolved -- My example in the previous post was wrong -- now makes sense that the case with * and — is very easy as was mentioned in the editorial.
Could somebody explain the following, please?
If the product of numbers is greater than or equal to 10^16, then it is beneficial for us to put the multiplication sign everywhere.
I discussed with some other people on discord, and I’ll give credit to Savior-of-Cross and tfg for some of the ideas.
Consider an optimal assignment of operations in the array. Let the array have length $$$n$$$. We will have that the operations in the array will be in the form
$$$(d_{11} * d_{12} * d_{13} * … ) + 1 + 1 + … 1 + (d_{21} * d_{22} * …) + 1 + 1 + … + (d_{k1} * d_{k2} * …)$$$
where the first and last element of each block of $$$*$$$ are both $$$>1$$$. For each $$$i$$$, let $$$a_i = d_{i1} * d_{i2} * …$$$. Then, we have that the product of all the elements in the array is $$$a_1a_2…a_k$$$. Let this product be $$$P$$$. Suppose $$$P \geq 2n, k \geq 2$$$.
Corrected proof (thanks Savior-of-Cross!), also changed the bound to $$$2n$$$:
We first want to show that $$$\sum_{i=1}^k a_i \leq 2 + P / 2$$$.
If $$$k = 2$$$, then we have that $$$a_1 + a_2 = a_1 + \frac{P}{a_1} \leq 2 + P / 2$$$. For $$$k > 2$$$, we have $$$\sum_{i=1}^k a_i \leq (\sum_{i=1}^{k-2}a_i) + a_{k-1}a_{k}$$$ (a sum of $$$k - 1$$$ terms with product $$$P$$$) and can thus inductively show that the cost is $$$\leq 2 + P / 2$$$.
We have that the cost of the current configuration is $$$\leq$$$ $$$\sum_{i = 1}^k a_i$$$ + (number of ones) $$$\leq 2 + P / 2 + n - k \leq P / 2 + n$$$. This is less than or equal to $$$P$$$ whenever $$$P \geq 2n$$$.
Thanks!
I thought AM-GM tells us that $$$\sum_{i=1}^{k} a_i \geq 2\sqrt{P}$$$, and not the other way around.
Thanks for pointing that out. Also i think it should be n * nth root() -- i had improperly tried to generalize from k = 2. I guess there's a hole in my proof hmm... let me think about this
Actually I messed up my some calculation, but it directly tells us that $$$\sum_{i=1}^{k} a_i \geq k \cdot P^{1/k}$$$.
See above for the fixed proof!
Let's assume a[i] <= a[i+1]. Let's say we can group those without regards to the 1's (so we can group them out of order). Group a2..ak, we get C = a1 + a2*a3*...*ak + n (n is the number of 1's in between). C is obviously an upper bound for the answer of grouping K-1 or less groups since ai >= 2. We want to prove a1*a2*...*ak >= a1 + a2*a3*...*ak + n for some condition. This actually reduces to a1*a2 >= a1 + a2 + n. The maximum a1+a2 happens when a1 = 2 and a2 = P/2 so: P >= P/2 + 2 + n, P/2 >= 2 + n, P >= 2*n+4. Here, n is the number of 1's, so we can take N = n+k where N is the actual size so P >= 2*N >= 2*N+4-2*k.
Assume u are combining 2 numbers x and y from addition to multiplication. The amount gained is x * y — x — y = (x — 1) * (y — 1) — 1. Now assume we are able to take the first some amount of numbers in segment such that they are greater than 10^16, let that be x, and y is the next number greater than 2 after some 1s in between. Obviously, (10^16 — 1) * (2 — 1) — 1 = 10^16 — 2 is the minimum amount we could gain by ignoring the 1s and multiplying the two numbers, and that is much greater than the amount gained by adding the 1s obviously. In fact, you could have product much less than 10^16 too i believe, it should only be like n probably.
Obviously not formal, but you can do an inductive proof on value of consecutive group multiplied to formalize it. My reasoning though is more intuitive and what I used in contest.
I use the formula $$$(A-1)(B-1)-1-x$$$ (where $$$x$$$ is number of 1s between the components) as the prize from merging 2 'components' too.
If $$$A$$$ reaches $$$N+1$$$, where $$$N$$$ is the length of whole sequence, merge anything with that component will always get zero or positive prize (a.k.a. benefit) because $$$x$$$ cannot exceed $$$N$$$. That means the optimal decision to do then is merging the whole sequence.
Therefore, if the optimal answer is not multiplying the whole sequence, then each separated component in the optimal answer must have the multiplication of all its number at most $$$N$$$
Using this logic,
Therefore, the maximal value you can get without multiplying the whole sequence is at most $$$N^2+N$$$
In conclusion, if multiplying the whole sequence is larger than $$$N^2+N$$$, it is already optimal, else we can solve it using DP.
The proof above has shown tighter bound that $$$2N$$$ is already enough, but I think it is more intuitive this way.
can someone please help in B(find the spruce)?
Find the maximum height of the spruce that can be obtained from each (x,y). (cell)
The maximum height of the spruce at (x,y) depends on the
1) maximum height of spruce that can be got at (x-1,y) (the cell above (x,y))
2) the number of * to the left of (x,y)
3) the number of * to the right of (x,y).
So, we can iterate over the grid, fix each cell (x,y), find the maximum height of spruce that can be obtained if (x,y) is origin (center of the spruce). Sum all the values. You get the answer.
To fit this in the time limit, you can use prefix sums, dp, binary search.
Please explain how to use binary search in that problem, I know the other two.
binary search on the maximum height at every cell.
Check out my submission 100949094
Binary search is absolutely not necessary as O(N^3) is allowed.
The simplest solution I can think of is to do a dp iterating from the bottom row up. Let dp[i][j] = the max height of a spruce. Then the answer is just the sum of all dp[i][j]. Start with all dp[i][j] = 1 if there is a '*' at i,j otherwise dp[i][j] = 0. If there is a '*' at i,j then dp[i][j] = 1 + min(dp[i+1][j-1],dp[i+1][j],dp[i+1][j+1]) otherwise dp[i][j] = 0. This dp relation can be seen easiest via a picture.
Can anyone help me in figuring out on which test case my code for problem B failed on system test.
My code
Problem link
Consider case-
AC Submission
Changed $$$a[i+1][1]$$$++ to $$$a[i+1][1]=1$$$ and got $$$AC$$$
Ah $$$!$$$ That hurts.
$$$Thanks$$$ $$$:)$$$
Please tell me how to solve E if $$$x \le 10^{18}$$$? The problem is that when $$$y > x$$$ then the lowest point may or may not be able to form full cycle (modulo $$$x$$$), how to check this for big $$$x$$$
.
2 1
2 1
2 0.958222
Is where your code fails not the one you mentioned. Check again
Thank you, I noticed it. Unfortunately no option to delete the comment now
My answer to B was way simpler than the editorial. I just did this 2D DP
where
block[i][j]==1 if (i,j)=='*' else 0
The complexity of this solution is O(NM) so it is the best complexity you can get.Can anyone xplain the question A(string generation)??
Problem: Given N and K. Generate a string of length N consisting of only 'a','b' and 'c' and the length of the longest palindromic substring(LPS) from the given string should be <=K. Ex aabc here length of LPS is 2 ("aa").
Solution: As the length of LPS can be <=k. We can keep it 1 length as well. Now, How we can achieve that? obviously if we keep on repeting the pattern abcabcabcabcabc.... the LPS which we will get is of length 1 which is for <=k.
why is it wrong,i'm so confused.... is there something about "std::string"????
No, it's not wrong. Implementation
thank you so much!!!! -,- i forget my costom "#define", thank you!!!
https://codeforces.net/contest/1461/submission/100931552 Isn't this a simpler approach to B? Also I think it has O(n*m) complexity too.
Hey, your solution is much easier to understand, but could you please explain what algorithm or what the logic behind it is?
I observed that all the smaller spruces combine to form a bigger spruce . So in order to have a height h spruce the bottom 3 element must have all at min. h-1 spruce.
In editorial of problem E , for y>x , how to justify that doing "we will lower the water level as many times as we can, and then raise the level" will be optimal ?
Because when you are not able to decrease any more, you can always save yourself with only one y and proceed, therefore there is no reason that you put water in other than that case.
I found more concrete proof . For simplification consider $$$l = 0$$$.
Suppose water level is at $$$p$$$ and $$$p<x$$$ i.e water level will go below 0 on that day .suppose $$$y+p>r$$$ then we cannot add water in morning. Thus $$$x+y>r$$$ , hence if we would have added extra water sometime before even when level was $$$p_1⩾x$$$ then $$$p_1+y>r$$$ and thus not possible to add. Hence we should lower the water as much as we can.
Thanks a lot, that's a nice proof! rananjay23
Proof for Case 3 when $$$x<y$$$:
Let's say we are on $$$k \mod x = x_1$$$, and we have $$$m$$$ remaining days that we can go but instead we increase by $$$y$$$ (we assume that it is possible without exceeding $$$r$$$). So, we have two cases, first when we utilize current $$$k$$$ fully i.e. we decrease until we reach $$$x_1$$$, second when we increase it by $$$y$$$ now. Here $$$k+y \mod x = x_2$$$.
$$$ans_1 = \dfrac{k-x_1}{x} + \dfrac{x_1 + y - x_2}{x}$$$, where $$$(k-x_1) \mod x = 0$$$ and $$$(x_1 + y - x_2) \mod x = 0$$$.
$$$ans_2 = \dfrac{k+y-x_2}{x}$$$, where $$$(k+y-x_2) \mod x =0$$$.
Now, we can see that $$$ans_1=ans_2$$$. So it doesn't matter if we increase by $$$y$$$ now or later. Hence our algorithm is correct.
In problem C, why do we make ans = 0.0, when last correct position is -1?
Why is this solution not correct? https://codeforces.net/contest/1461/submission/101000199
what's wrong with my code problem A i'm so confused......
You cout ' ' and I think system checks with getline so you get wa if you cout without ' ' you will get ac
thank you so much!!!! -,- i forget my costom "#define", thank you!!!
Hey guys, so I made this screencast and solutions from A-D, hope you find it useful and am looking forward to honest feedback: https://www.youtube.com/watch?v=JsHC-GW8UVc
So i had a terrible contest. (yet again!) My submission to C gets Wrong Answer on Test case 8 submission
However i can't see any flaw in my approach, guide me. I am maintaining a DP sort[i] if all elements till i are sorted, not_sort[i] otherwise. My recursion is this: where mp[i] is the probability of sorting operation.
I am getting wrong answer on 10th test case of B. can any body explain the error. https://codeforces.net/contest/1461/submission/100931109
This codeing style is hard to read.
while(t+i<n&&j+1+t<=m&&j>=t&&a[i+t][j+1+t]-a[i+t][j-t]==2*t+1)
Thanks for replying! I am looping through each and every n*m cells assuming them as origin of the spruce (obviously if it exist). t denotes height of the a spruce having origin as i,j. so t=0 means assuming height as 0 initially. Explanation of conditions :
condition 1 : a[i+t][j+1+t]-a[i+t][j-t]==2*t+1 this checks if all cells ranging from j-t to j+1+t ( in total 2*t+1) at level t w.r.t. i,j are all * . because my array is prefix sum on every row.
condition 2 & 3 : remaining condition they just take care if my while loop is not cheking any cell which is not possible. as a[i+t][j+1+t] -> i+t can be max n and j+1+t can me atmost m.
Hope you understand.
There should be a check in the while loop condition that $$$j-t>=0$$$, or is this somehow included in the other conditions?
It is there. j>=t (condition 3)
i wasted a lot of time in B then implemented using dp approach.it was so simple with dp;
I don't know if my method to solve F. Mathematical Expression was smart or dumb. If we observe that it is beneficial to multiply if the product is greater that size of the array. After that we can simply use bitmasking to solve and the complexity would be linear with respect to n.
How to solve C using dynamic programming??
I genuinely want to know where do you see optimal substructure and overlapping subproblems?
The overlapping is that a spruce of hight h is made of 3 spruces of hight h-1 plus the top cell.
The optimal substructe is that we can iterate the rows bottom up to check the hights of the 3 spruces just below the current cell.
That's problem B, not C.
Ups, sorry.
While solving problem E during the contest , what was train of thought which brought you to the conclusion that when y>x we need to decrease water level as much we can and then raise the level ? Was it simply guessing and then proving or there was some steps of thoughts after which you arrived to that conclusion .
can someone explain Problem E i didn't get what editorial wants to say
So i don't know why but my F doesn't work. My solution is this[submission:101137585].So the idea is pretty much the same except for the last part.We can watch every 2 subsequences of array which have 0's in between them. So the solution for subsequence is next: first because 1's on the start and end of subsequence cant help with product but can with addition we know that they must have a '+' sign in between them. So lets watch now for subsequence that starts and ends with numbers that are not 0 or 1. There are 2 cases:if me multiply every number in this subsequence or to multiply every subsequence of this subsequence that consists of numbers greater than 1 and to add the 1's(there is no point in adding any numbers but 1's (because if x, y != 1 then x + y < x * y so it would always be more beneficial to just multiply)).Example: for this subsequence 2 2 1 1 1 2 if we multiply every number we would get 8 but if we multiply the first 2 then add that to the ones and then add the 2 at the end we would get 9 which, indeed, is the best solution. So we are basicly looking for max(product of all elements, number of 1's + sum of products of all subsequences which dont have 1's). So if you can help me i would really appriciate it.
101137585
Can someone explain F? Its too difficult to understand from the editorial.
Vladik Can you please tell why I need to handle the separate case of $$$y<=x$$$? means where/how the remainder method will fail.
E: water level input: 7 1 10000 4 2 1 answer: YES
was this test case considered by the jury?
void solution(vector<vector>& v, ll i, ll j, ll height) { if (i >= v.size() || i < 0 || j < 0 || j >= v[0].size()) { return; }
}
void solve(){ ll n, m; cin>>n>>m; vector<vector>v(n,vector(m)); for(ll i=0; i<n; i++){ for(ll j=0; j<m; j++){ cin>>v[i][j]; } } ll result=0; for(ll i=0; i<n; i++){ for(ll j=0; j<m; j++){ if(v[i][j]=='*'){ ans=0; solution(v,i, j,1); result+=ans; } } } cout<<result<<endl; }
int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; cin>>t; while(t--){ solve(); } return 0; }
How to convert this recursive solution into dp solution ??