Thanks for the participation!
1313A - Fast Food Restaurant was authored by Endagorion and prepared by ch_egor
1313B - Different Rules was authored by meshanya and prepared by DebNatkh
1313C2 - Skyscrapers (hard version) was authored by meshanya and prepared by Sehnsucht
1313D - Happy New Year was authored and prepared by voidmax
1313E - Concatenation with intersection was authored and prepared by isaf27
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
How Can I read Such a big editorial for B. havoc editorial to read...
In the B problem, I think we just need to compare a + a with b + c
Solution for B looks like some neural network ;)
can you explain how to do the problem i am not able to understand the tutorial
Contest has become mathforces with B
All comments are related to B :D
Is there any easier understanding of problem B?
For a number $$$a$$$ in round 1 and another number $$$b$$$ in round 2, we should make sure $$$a + b \geq x + y + 1$$$. $$$1$$$ should find $$$x + y$$$, $$$2$$$ should find $$$x + y - 1$$$... And if $$$x + y > n$$$, $$$1$$$ should find the smallest number in round 2 which can be selected.
So, we can see that the final answer will be $$$x + y - n + 1$$$, cause we can not arrange number $$$[1, x + y - n]$$$ to make them greater than $$$x + y$$$, and don't forget the ans must in $$$[1,n]$$$.
In round 1, for each number $$$a$$$ between $$$1$$$ and $$$x-1$$$(it can be empty), we can find a number $$$b$$$ in round 2, and make $$$a + b \leq x + y$$$.
In round 2, for each number $$$b$$$ between $$$1$$$ and $$$y-1$$$(it can be empty), we can find a number $$$a$$$ in round 1, and make $$$a + b \leq x + y$$$.
We cannot find any other pairs to make $$$a + b \leq x + y$$$. Thus, the answer is $$$x - 1 + y - 1 + 1$$$, and don't forget the ans must in $$$[1,n]$$$.
Let $$$a = x - 1, b = y + 1$$$, while $$$a$$$ is decreased by $$$1$$$, we can add $$$1$$$ for $$$b$$$. If $$$b$$$ is greater than $$$n$$$, we can select any number remaining, cause at this time, $$$a + b < x + y$$$. To proof the second one, we can just swap the round 1 and round 2.
It seems like it's a kind of greedy, but the editorial can proof the answer strictly. And also sorry for my poor English. Wish it can help you. :D
UPD: swap((a,b),(x,y)), sorry for the mistake.
what is a & b ?
thanks
thx, it helped alot
For the first case (minimum possible overall place) did you find a+b>=x+y+1 because it will give the count of all the combinations of final ranks which are more than x+y sum's rank? Also, I don't understand what you are saying in the next line ( the x+y>n condition)
Of course, we can't make sure we'll always find such $$$a$$$ and $$$b$$$ that $$$a + b \geq x + y + 1$$$. And at that time, the answer will be increased by 1, because whichever we select the $$$b$$$, we can't make $$$a + b >= x + y + 1$$$, in other world, the person who get the $$$a$$$ place in round 1 will finally placed before Nikolay.
Not sure if it is easier, but I solved it with the following intuition: let's rephrase this task in a different way: you have a square grid of size
n
. Each participant with scores(xi, yi)
will mark one cell with same coordinates. There should be only 1 marked cell on each vertical and horizontal line. Total score of a participant isxi + yi
and participants with a same score share same diagonal (from top-right to bottom-down), participants with lower score are on diagonals on the left, and participants with higher score are on diagonals on the right.Now the task is the following — having Nikolay's mark on
(x, y)
how can we put other marks in order to have max or min number of marks on a right side from Nikolay's diagonal. You can check three cases depending on a position of Nikolay's diagonal from main diagonalx + y ? n + 1
, where ? on of<,=,>
awesome.
Awesome. Thanks for sharing your intuition.
Fire woman is beautiful!~
can anyone write the bruteforce solution for problem A.
You can use Greedy method
Notice that you should take (a-b) and (a-c) before take (b-c) else you will get
WA
B looks so hard in the editorial:)
I would like to understand D. Why we use bit mask of size k?
What does it mean if "we can find free bit and create match to this segment" to create the dp?
Let’s imagine this situation. You gave every segment some number 1..k, such there are no intersecting segments with the same number. In this case you can easy maintain dp.
When we try to add new segment, we take some remaining color to match it to this segment (you have mask of free colors). So we came back to previous situation.
Why no need to consider the combination of segments with same number after numbered them according to your method? According to 71735081 from gigabuffoon, numbers for each segment are determined before doing dp.
When leaving color 3 available and color 4 taken, why can't another later segment with number 4 take the available color 3?
Suppose we sort and process a start/end event queue over the segments. At each start event, we will assign that segment the smallest integer not in use by another active segment. Since there is no index with more than $$$K$$$ segments covering it, during this process we will assign segments at most $$$K$$$ unique values.
These values correspond to an index of a bit in a bitmask; this bit is on if we currently have this segment selected. Because each index is covered by at most $$$K$$$ segments, a bitmask $$$[0, 2^K)$$$ can uniquely identify any subset of segments which are crossing some index.
Then, you can write a take/leave DP on the segments, and score ranges you cover based on the parity of your bitmask. code
thanks a lot
Thanks for the hint and code.
A side question: no need to worry about stack overflow for recursive dp implementation since N <= 1e5?
What happens when a certain bit / color is on and you arrive at a the start of a segment of the same color? Wouldn't it mean that two segments that the parity flips because you now have two segments that don't intersect? I see that you are adding them instead of XOR
Ah, it seems that this cannot happen because if they are of the same color, then they don't intersect, meaning eventA.end < eventB.start. Then therefore, the color / bit would have been switched off before arriving at the start of the new segment.
Can someone please help me find error in my logic with question B.
my submission
For minimum case, I started making pairs such that their score is one greater than our score. Then after that, some scores in first contest remain which are greater than a, and some in second contest which are greater than b. we take min of these to give additional people with score greater than us.
sg1 = min(n-1-x, y-1)
sg2 = min(n-y-1, x-1)
og = min(n-x-sg1, n-y-sg2)
mn = n - sg1 -sg2 - og
Similarly I do for max, but for this case, we search for equal score. After that we search for scores less than a in first contest, and scores less than b in second contest. We add min of these to give additional people that may come at or before us in position.
gr1 = min(n-x, y-1)
gr2 = min(n-y, x-1)
ol = min(x-1-gr2, y-1-gr1)
mx = gr1+gr2+ol+1
The answer should in range [1,n].
my god I feel silly now. I only had to change sg1 and sg2 so that they dont go negative! Thanks mate. The problem was that when a, b were last then sg1 and sg2 should have been zero, but I overlooked that issue!
Problem B
For worst rank:
Round 1 ......x........
Round 2 ..........y....
The total score needs to be made as greater as possible.
So,
Step 1 — We can couple entries like (x+1,y-1), (x+2,y-2)..... and (x-1,y+1), (x-2,y+2).....
Step 2 — After this step we will be left with some left out numbers is the first few ranks of few rounds, so we'll couple them together. The reason we are going only for the starting ranks is that coupling of these elements will result in score less than (x+y).
For example:
Round 1: 1 2 3 4 5(x) 6 7 8 9 10
Round 2: 1 2 3 4 5 6 7 8(y) 9 10
In step 1, we'll couple (6,7),(7,6),(8,5),(9,4),(10,3) and (4,9), (3,10)
After step 1, we'll look for the left out ranks among the starting ranks of rounds 1 and 2 --> 1,2 (round 1) and 1,2 (round 2). So, we'll couple them.
Answer = 1(including itself) + 5(same score part 1) + 2(same score part 2) + 2(score less than x+y)
For best rank:
Similar to the worst rank case. Here we will try to make score greater than (x+y). Sum = (x+y+1) will yield the most number of possible results, so we'll aim for sum to be x+y+1 (similar to what we did for sum=x+y in the previous case).
Edge case: min(x-1,n-x-1) may produce negative numbers, so we'll need to take max with 0.
This will give us the maximum possible number of scores that are greater than (x+y). So we'll subtract this from N.
Code for reference:
// worst rank
part1 = min(x-1,n-y)
part2 = min(y-1,n-x)
w = part1 + part2 + min(x-1-part1,y-1-part2)
worst = 1 + w
// best rank
part1=min(y-1,n-x-1)
part2=min(x-1,n-y-1)
part1=max(part1,0)
part2=max(part2,0)
best = N — (part1 + part2 + min(n-x-part1,n-y-part2)
I did exactly same as this, but stupidly I didnt do the
part1 = max(part1, 0)
for best rank. Bye bye expert, on the bright side I can give div 3 ;-)Problem B. I forgot about the borders and place
max(1,min(n,x+y-n+1))
wrotemax(1,x+y-n+1)
. and finally I didn't solve the problem during the time of the ContestWhy cant I view other people's code ? :/
What is the approach of finding "nearest" numbers to the right and to the left that are smaller than the current one?
Maintain a monotonic stack
How to search the "nearest" in c2 (second solution)?
Pls, check mine, there are comments: https://codeforces.net/contest/1313/submission/71813248
stack approach from here helped me: Next smaller element
Thanks man
This helped me a lot. I never needed nor thought about how to precompute this information. Thank you!
Problem C. in editorial(second solution). please help. can you this code
O(n*n)
optimize toO(n log n)
orO(n)
. I could not findj
faster.Use a strictly ascending stack (one for L and one for R) — O(n) as every element will only be popped once.
What if in C2 we have more than one min element??
It does not matter, in case it has more than 1 min elements, choose the first one and divide the array into two parts — one that is left to it and one that is right to it. Calculate ans recursively for them
Please explain solution of problem 'D' in detail
Well detailed and explained solution of problem 'D' Dp states explained
71761226 Hope it helps :)
good job bro
How can we memoize using dp[id][mask] ? The same mask can denote a different set of segments right ?
A mask can represent different set of segments but a mask at a particular 'id' represents a unique set of segments.(This is due to the fact that atmost only 8 segments can be active)
Is the fact that only 8 segments can be active the only reason why for an id, a mask corresponds to a unique segment set ? I didn't understand how the code is ensuring that a mask for an id is a unique set of segments.
for a segment, why is l, r+1 added and not l, r ?
Woah
I am getting Runtime error "Exit code is -1073741571" on problem C2 with C++14
71730114
while the same code is ACCEPTED with C++17
71727975
What could be causing the difference?
I looked further into this and surprised to know that stack size limitation is the culprit. Default "8192" KB (in linux) is too low.
Setting it to "100000" worked for me, and looks as if a higher value is set for c++17. Not sure what could be the reason for setting this as low for c++14
Errichto MikeMirzayanov ch_egor
https://codeforces.net/contest/1313/submission/81457914
took me 2 hrs to understand ur code but i did it ..... thank you very much for posting ur solution , learned a lot about Segment Trees.
In the recursive approach for C2 how to make choice after finding minimum i.e, if we have to recurse on the right or left part?
You have to recurse on both the parts and calculate their respective scores. Depending upon which side has better score, you need to set the opposite side to the min value.
thanks, but what if there are multiple minima ? do we have to consider any one of those or
Yes just consider the first such minima (from left) and break the array at the point.
thanks! so for finding minima(RMQ), we have to use segment tree/ sqrt decomposition. What is the O(N) approach?
deleted
sparse table supports query operation in O(1) time but O(N Log N) preprocessing time.
deleted
Segment Tree construction is O(N), because there are ~2*N nodes in the tree and each node needs constant time.Query/Update takes O(Log N) time
deleted
deleted
Hi in (C1)easy version problem i have found the minimum element and then found the sum of elements on the left side of minimum and similarly on the right hand side . If the sum on the right side is less than that of left side then then i have assigned minimum value to all elements to the right side and vice versa. can u pls tell why this approach is not working
deleted
Thanks for the tutorial <3
what stand between me and the rating add,is the math
I wrote a segment tree & divide and conquer solution for C2, based on the first idea in this tutorial of problem C2. But I get TLE here: https://codeforces.net/contest/1313/submission/71738916. I tried many times at my local machine, using a variant to count the time complex and found the divide and conquer procedure is O(N), and my segment tree is O(17) = O(logN).
So I used ST table to reduce complex from O(N logN) to O(N) here: https://codeforces.net/contest/1313/submission/71741474, get Accepted.
What is ST Table ?
Sparse Table
.
Can anyone just explain D more clearly ? I'm too stupid to understand the given editorial.
How to solve the problem C2 recursively in first solution,isn't it got a comlexity 2^n?
as every number can behave as minima of a region exactly once so its complaxity not exceed 2*n in iteration
Help needed in problem c2.
My approach: for selecting the pick index I calculated the following for each index.
sum=dpl[i]+dpr[i]-a[i] here dpl[i] is sum from 1..i making non decreasing segment and dpr[i] is same from n..i
for which index I get the maximum sum I selected that index to be the peak index. And then just calculated the final result.
Can someone please tell me why this is wrong or any test case? my code: 71701033
In (B) some proofs may become much more symmetrical if you notice that you can make the substitutions
Also an observation is that you don't have to solve a completely new problem for the minimum after solving the maximum problem; specifically if $$$x+y = c$$$, then the worst possible for $$$x,y$$$ corresponds to the best possible for some imaginary opponent with $$$x'+y' = c+1$$$; one just needs to subtract the number of "ties" in the $$$x',y'$$$ case, and add in a $$$+1$$$ because $$$x,y$$$ beats itself.
(https://codeforces.net/contest/1313/submission/71990013)
In C2 editorial second soln, what's an optimal way to find "nearest" numbers to the right and to the left that are smaller than the current one.
got it. https://www.geeksforgeeks.org/find-the-nearest-smaller-numbers-on-left-side-in-an-array/
Can anyone help me with problem Skyscrapers (Hard Version), I am getting memory limit exceeded for my solution I used Sparse Table for finding range minimum query and did a recursive method My submission
I struggled a lot with problem C / C2 (skyscrapers). Some comments said to use a monotone stack, which was a big hint, however I have only done a few problems with this approach and I didn't know what the stack actually represents. Finally I came up with it:
For now we'll focus our attention on nondecreasing skylines. For every index $$$i$$$, we will identify the optimal skyline from index $$$1$$$ to $$$i$$$ inclusive. For $$$i=1$$$, clearly we use $$$a_1=m_1$$$. More generally, for $$$i=1$$$ to $$$n$$$, we push $$$i$$$ onto the stack $$$s$$$ after popping all elements $$$j$$$ such that $$$m[j]>m[i]$$$. Finally we push $$$i$$$. The stack right now describes the optimal skyline ending at $$$i$$$; Specifically, if $$$s$$$ contains $$$i=i_k > i_{k-1} > \cdots > i_1$$$, then we make assignments as follows: indices $$$(i_{k-1},i_k]$$$ get height $$$m[i_k]$$$, indices $$$(i_{k-2},i_{k-1}]$$$ get height $$$m[i_{k-1}]$$$, $$$\ldots$$$, and indices $$$(0,i_1]$$$ get height $$$m[i_1]$$$. It is a little exercise to see that this is a valid assignment and optimal.
Let's call $$$maxIncreasingSkyline[i]$$$ the sum of the heights of this optimal skyline. It can be computed in amortized $$$O(1)$$$ at each step while doing the popping/pushing.
One can do the same procedure for nonincreasing skylines, by iterating from $$$n$$$ to $$$1$$$, and similarly obtain $$$maxDecreasingSkyline[i]$$$, which represents the optimal skyline that "begins" at index $$$n$$$ and ends at $$$i$$$.
Now the actual optimal skyline will be one of 3 possibilities: a nondecreasing skyline (whose value is $$$maxIncreasingSkyline[n]$$$), a nonincreasing skyline (whose value is $$$maxDecreasingSkyline[1]$$$), or a combination of the two, in other words, there is some $$$j$$$ such that $$$maxIncreasingSkyline[j]+maxDecreasingSkyline[j+1]$$$ is maximal. The latter can be computed in $$$O(n)$$$ time since we have already prepared the two arrays. Then one does another linear scan to reconstruct the assignments $$$a[\cdot]$$$ from the stack.
My submission is here but hopefully the explanation above is adequate.
Hardest Div2B ever. Were past Div2B's always this hard? :(
In problem C2, what is the neatest way to implement the recursive approach? Recursive approach is prone to exceed the default stack size limit.
Are there any restrictions for i = 1 and i = n? see 79077157 for more details. Please someone tell me where my code is not correct. Thanks in advance.
for problem C1 of div2
I think that my solution is correct
Solution for C2 using the divide and conquer approach: 85811938
EDITORIAL OF C2
OBJECTIVE: Find the Optimal Peak by both Methods:
Method 1: Create a Segment Tree which returns the index of the smallest element in [l,r].
Now , we search for the smallest element in [l,r]. Suppose it is at minindex. Then we make 2 recusrive calls,
-> First we assume that all a[l....minindex] have been changed to a[minindex] , so we have a value of a[minindex]*(minindex-l+1) . Then we make the recursive call on [minindex+1,r].
-> Second is when we change all a[minindex+1....r] to a[minindex]. Then we have a value of a[minindex]*(r-minindex +1 ). Then we make a Recursive call on [l,minindex-1].
In our base case, (l==r) , we have the value i.e. final sum of the array if l(or r) were the Peak. We simply find the Optimal Peak, i.e. the one with max. value. (Have a look at the code, I have made 2 special checks in case [l>r].See just below the recursive calls)
Once we have the optimal Peak, we can easily calculate the Array.
Method 1 Code
Method 2: This Method is nicely explained in the editorial. I referred to this to make 2 arrays pre and suf to find the smallest element on the left and right of a[i] with overall O(n).
Then I calculate value for each a[i] in left[] and right[] as per described Method in Editorial and found out the Optimal Peak. Then answer can again be calculated easily.
Method 2 Code
Can anyone help me in C1 . I m getting wrong answer at test case 9 . I m using divide and conquer technique .Provide me some test cases or check my soln . 1313C1 - Skyscrapers (easy version)
include<bits/stdc++.h>
using namespace std;
define ll long long
define pb push_back
int main() { ll n,i,j,k,l; cin>>n; ll a[n+1]; for(i=1;i<=n;i++){ cin>>a[i]; }
i=1;j=n; while(i<=j){
}
}
Can anyone tell me the problem in my code for problem C2. I used Seg tree here.