### awoo's blog

By awoo, history, 11 months ago, translation, 1437A - Marketing Scheme

Tutorial

1437B - Reverse Binary Strings

Tutorial

1437C - Chef Monocarp

Idea: BledDest

Tutorial
Solution 1 (pikmike)
Solution 2 (pikmike)

1437D - Minimal Height Tree

Tutorial

1437E - Make It Increasing

Idea: Neon

Tutorial
Solution (Ne0n25)

1437F - Emotional Fishermen

Idea: BledDest

Tutorial
Solution (BledDest)

1437G - Death DBMS

Idea: BledDest

Tutorial
Solution 1 (pikmike)
Solution 2 (pikmike)  Comments (118)
 » thanks for the tutorial
 » Well i'm not so sure about this but i think another more straight forward answer for b is that we use the fact below SpoilerIn the The optimal answer no indice is switched more than once and the fact that we need to make the string into a binary string and that the number of ones and zeros are equal we just calculate the number of indices that need to be reversed and if any adjacent indices need to be reversed we count them as one(as we can switch them with one move)Once again I say as i'm still a beginner i'm not fully confident of this approach tell me if it has a problem
•  » » I used a similar approach submission
•  » » I did this, but I don't have proof why this works. I saw that when there is a even length contiguous block of bad indices then one reversal fixes that block. But for odd length blocks it still works, I don't why, I was seeing that probably the odd length blocks cam be paired up with other odd blocks but couldn't conclude much. Anybody can prove this?
•  » » » I_love_Lata You are almost correct. The reason this works is that since the number of indices that have to be reversed is always even (pretty straightforward), the number of odd blocks will be even in number. Two consecutive odd blocks can be made correct in 2 moves, the first move- reverse the whole substring between the first odd block tot he next odd block(both inclusive), then in the second move reverse only the middle part which was initially correct before the first move.So overall, 2 odd blocks require 2 moves, and they are even in number, so for the sake of counting we can say that we need 1 move per odd block, and 1 move per even block is obvious. So the answer is the total number of blocks.
•  » » » » Ah, I see, I was unable to see why number of odd blocks is even, turned out it's because total bad indices are even. I think I had hard time realising this. Thank you for your comment!
•  » » a simpler approach submisson
•  » » Yup, you are right. I used this approach only. :-)
•  » » ~~~~~ Your code here... ~~~~~ 

 » For G, can someone please explain why iterating over all the terminals in each query is $O(Q*sqrtN)$?
•  » » Not exactly $q \sqrt n$ but $(sum~of~lengths~of~q) \cdot \sqrt{sum~of~lengths~of~s_i}$. No more than $\sqrt{sum~of~lengths~of~s_i}$ words can end in each position and there are $(sum~of~lengths~of~q)$ positions in total.
•  » » » why n^(1/2)? I think it should be n^(2/3).
•  » » » » 11 months ago, # ^ | ← Rev. 2 →   Why? If we want to find out the maximum number of unique lengths then let's take the smallest possible lengths $1, 2, \dots, k$, one string per each length. The sum of them is $\frac{k(k+1)}{2}$, thus $k$ is $\sqrt{2 \cdot total\_length}$.
•  » » » » » 11 months ago, # ^ | ← Rev. 3 →   Let's conside the worst case, when you query for a string T, let's says T = abcd...z, and insert every substrings into the Automaton, There are O(|T|^2) substrings, and the total length of them is n = 1*|T| + 2*(|T|-1) + .... + |T|*1 = O(|T|^3). By subsititution, we have O(n^(2/3)) in the worst case. Did I make some mistake?
•  » » » » » » I think you are mistaking what are we summing up. What we are doing with the query is the following: add the first letter, save the current vertex and look at the terminals above the vertex (their count is $O(\sqrt{dictionary\_length})$). Then go over the next letter from the saved vertex, once again jump no more than sqrt times. I don't really see how the substring count of the query is relevant here.
•  » » for each search you can also just keep a set of which words you have already matched in the current search (actually a set with each terminal of the trie that has already matched). thus, if you are visiting it again during the same search, you just ignore it. then the first solution for G becomes O((q+T) log n), T being the sum of lengths of each query of type 2.97259249
•  » » » Why do you think the complexity is like that? I don't quite see how to construct a test such that there are a lot of distinct substrings of fixed total length but I don't see $O((q + T) \log n)$ either.
•  » » » » that's true, I can't figure out a better bound for the number of matched strings in total. the O(q sqrt(n)) bound seems tight. the set i suggested just avoids going up the suffix link tree more than once per terminal.
 » 11 months ago, # | ← Rev. 2 →   Thanks
 » 11 months ago, # | ← Rev. 2 →   In E, I am not able to get why are we subtracting i from each a[i] int the following step forn(i, n + 2) a[i] -= i; in editorial solution.Can somebody help me? Thanks in advance!
•  » » Same Here. I didn't get that part For eg. if the seq is [2,5,3,1] then we can't choose LIS as [2,3] but by taking distance consideration and by making a[i]-=i how we are able to do that.
•  » » » This explanation was good for problem E.
•  » » 11 months ago, # ^ | ← Rev. 2 →   If you subtract i, it leaves room for you to make the other elements in strictly increasing order.For example, if we have the array1, 1, 2, 3And we subtract i from each,1, 0, 0, 0You can see if we make these elements all equal, then the original array will be increasing. If we change everything to one and then add i again, we have1, 1, 1, 1 -> 1, 2, 3, 4
•  » » » Okay, I got it. Thanks for explaining kevin!
 » 11 months ago, # | ← Rev. 2 →   Hi which programming language is this: (answer to A) ? (I'm asking cuz it looks sexy) fun main() { repeat(readLine()!!.toInt()) { val (l, r) = readLine()!!.split(' ').map { it.toInt() } println(if (2 * l > r) "YES" else "NO"); } } 
•  » » I guess it's kotlin.
 » 11 months ago, # | ← Rev. 4 →   adedalic "The longer the segment [a2,a) is the better and the maximum we can take is a=2l." Can you please explain this statement a bit more? maximum a possible is 2*l but there can be a such that a
•  » » It has been shown that $\left\lfloor\frac{l}{a}\right\rfloor = \left\lfloor\frac{r}{a}\right\rfloor$. And also that, we must find $a$ such that $\frac{a}{2} \leq l$ $mod$ $a \leq r$ $mod$ $a < a$. So, it must be obvious that $r - l < \frac{a}{2}$, if such an $a$ exists Case I: $r \geq 2l$ Here we must have $l \leq r - l < \frac{a}{2}$. That is $a > 2l$. But this wont work as $l$ $mod$ $a$ would be less than $\frac{a}{2}$ Case II: $r < 2l$ We can always choose $a = 2l$
•  » » » 11 months ago, # ^ | ← Rev. 3 →   I got what you said, it becomes easy as soon as you have the two cases(r>=2*l and other one) to analyze.but what if we want to analyze cases as below 0) obviously a should not belong to [l,r] 1) if it is possible to have required a such that a>r, this gave me 2*l>r 2) then a
•  » » » » i thought the same way beacause between l and r you will get mod 0 and hence it cant be an answer so you need atleast something larger than r . then i tried a= r+1 and it worked and also r+1 should be less than l because l%a >=a/2 .
•  » » » » According to 0), either a > r or a < l. if a > r, obviously that a == r+1 is the best. Now we want to prove if a == r+1 is not satisfied, a < l will be not satisfied too.If a == r+1 is not satisfied, it means l*2 < r+1, right? Because Only in the situation that l mod (r+1) = l < (r+1)/2, the restriction is not satisafied.Next, l*2 < r+1 means l*2 <= r. If a < l, you will easily become aware of that for every a, exist a k that satisfy l <= k*a <= r. Because l*2 <= r.If l <= k*a <= r, the restriction is not satisfied. So we finally find if a == r+1 is not satisfied, a < l will be not satisfied too.
•  » » » It can be done using simple fact that for any no x ,it will have maximum possible mod when we take its mod by any no grater than xProof for x% y :case1 — when y > x in this case ans is always x .case 2 — when y =x int this case ans is 0 always.case3 -when y < x in this case x % y < y and y < x so maximum value of mod occurs in first case.so for any l to r just think of taking a = r+1 ;
 » For Problem F, I've got a $O(n log n log max a_i)$ solution. CodeBasically $dp[i][j]$ means the number of ways to make fisherman $[1,i]$ emotional while $j$ of them are happy, considering the sequence in descending order.Then use a segment tree with range multiply update and range sum query to speed up the solution.
 » 11 months ago, # | ← Rev. 2 →   G can also be solved with suffix tree. I dont know about Aho Corasick , but i heard that algorithm use in Aho-Corasick is similar .
 » 11 months ago, # | ← Rev. 3 →   It may be completely irrelevant but I just want to ask one doubt. Should I upsolve the problem which are rated at something greater than 2600 (For example F,G in this contest) or should I upsolve only problem which I currently feel comfortable with? (From experience, I can say I am able to understand 2400 rated problem if given some hours)
•  » » If you just want to be high expert or low cm, you should probably just do easier tasks(<=1900). Though in theory, doing hard problems is a lot better but doing too hard tasks can make you demotivated, make you look at editorial too quick etc.I would suggest upsolve upto 200-400 rating points above you, depending on how hard they feel to you.Contests are about struggling with problems so also make sure you struggle before reading editorial.
 » why would greedy fail for C?
•  » » what greedy?
•  » » » 11 months ago, # ^ | ← Rev. 2 →   Like we use T for every distinct ti equal to it so answer will be 0 till then and for every duplicate ti we will use the smallest absolute difference to it's left(greater than 0) or right which has not been used and add it to the answer
•  » » » » Any reason why this should work?
•  » » » » » we are trying to minimize the answer but now that it's known that greedy doesn't work, I just want to know why
•  » » » » » » How will you choose which side you should take
•  » » » » » » » 11 months ago, # ^ | ← Rev. 2 →   I will sort the initial array first and then if both sides have equal absolute difference then I will choose the left side as the right side may come in handy for further values of the array
•  » » » » » » » » Just take this example: 1 3 4 4 6 7 7 8 9 10 11 12According to your logic, you'd take 5 for the repeated 4 because 2 is farther, however, taking 2 would be more beneficial because then you'd have 5 available for the repeated 7 later. I hope it's clear.
•  » » » » » » » » » Thank you. This is the test case I was missing.
•  » » » » » » » » » 11 months ago, # ^ | ← Rev. 2 →   And we can always reach this lower bound, by pairing 00 with 11 or with left/right border of s What Does This Mean Can Someone please elaborateIn Problem B
•  » » » » » » » » » I think it is because of only look of situation when we have pairs of one type is like: 010101101010.Generic look: ...xyxyxyxxyxyxyx... here you can just reverse a half.
•  » » » » » » And even your algorithm will not follow this: " I claim that there is an optimal answer such that the times T for each dish go in the increasing order. "
•  » » » » » » » 11 months ago, # ^ | ← Rev. 2 →   Can you please explain the claim?
•  » » Even I was doing same during contest but cant find why it is wrong https://codeforces.com/contest/1437/submission/96912740 .Help if anyone got it?
•  » » » look at the replies in my comment, it is clear now
•  » » » » yes!!
 » https://codeforces.com/contest/1437/submission/96882318 can anyone please explain what is wrong with my code Problem A
•  » » Floating point precision maybe. Just change the condition in the other form l*2>=r+1
•  » » » But on some Ide's it is actually giving ans Yes at 7th case and also when I use double instead of float it got accepted.
•  » » » » I have similar experience when i used to use ceil function instead of the formula a+b-1//b precision can be pain. I have read double is more precise than float though.
 » Can anyone please explain the implementation of problem C first approach. I understood the tutorial, but didn't understand implementation. why do we use dp[i][j + 1] = min(dp[i][j + 1], dp[i][j]); ?
•  » » I will try to present this in an easy manner here i means ith dish and j signifies the time when it was picked so this transition (dp[i][j + 1] = min(dp[i][j + 1] , dp[i][j]) implies that either the current time is the most appropriate time to pick the ith dish or you can simply check for j + 1 — 1 th minute which is nothing but jth minute and this works because we are calculating the answer for each minute from 1 to MAX limit so we just need to pick the best one which we will assign to the current dish you can have a look at my submission for reference . Hope it helps :) sorry for my bad english :(
•  » » Let's see what we are doing while iterating the for loop in the first approach-We are updating two values dp[i+1][j+1] and dp[i][j+1].We are updating dp[i+1][j+1] when it has larger value than dp[i][j] + abs(t[i] — j), which is in fact always (so you can try changing min(dp[i + 1][j + 1], dp[i][j] + abs(t[i] - j) to dp[i][j] + abs(t[i] - j)).Now to the real question- We are updating dp[i][j+1] when it has larger value than dp[i][j], as dp[i][j+1] currently stores dp[i-1][j] + abs(t[i-1] - j)(accept for i=0), if its more than dp[i][j] we update it.In simpler terms, we are just comparing dp[i][j] + abs(t[i] - j) and dp[i+1][j] and assigning the minimum of the two to dp[i+1][j+1].Try making dp matrix and dry run the code against some sample test cases, you will surely get it. Correct me if I'm wrong :P
•  » » Here is an easy to understand approach :- Let dp[i][T] denote the minimum cost of processing the first i dishes (Remember we first need to sort them in the increasing order first), such that the $i^{th}$ dish is processed at time T.We can simply calculate answer for each state in O(n) hence the total time complexity will be O(n^3) Initial coderep(i,0,2*n){ dp[i] = abs(i - arr); } rep(i,1,n){ rep(j,1,2*n){ rep(k,0,j){ if(dp[i-1][k] < INF){ dp[i][j] = min(dp[i][j], dp[i-1][k] + abs(j - arr[i])); } } } } Note that we can move the abs(j - arr[i]) part out of the min() function as it is not dependent on k, hence our code now becomes :- coderep(i,0,2*n){ dp[i] = abs(i - arr); } rep(i,1,n){ rep(j,1,2*n){ rep(k,0,j){ if(dp[i-1][k] < INF) dp[i][j] = min(dp[i][j], dp[i-1][k]); } if(dp[i][j] < INF) dp[i][j] += abs(j - arr[i]); } } So finally we can optimise it to O(n^2) by observing that the innermost loop only calculates the prefix minimum of dp[i-1] (even O(n^3) easily passes the time limit though). final coderep(i,0,2*n){ dp[i] = abs(i - arr); } rep(i,1,n){ LL prefix_min = dp[i-1]; rep(j,1,2*n){ dp[i][j] = min(dp[i][j], prefix_min); if(dp[i][j] < INF) dp[i][j] += abs(j - arr[i]); prefix_min = min(prefix_min, dp[i-1][j]); } } LL ans = INF; rep(i,0,2*n){ ans = min(ans, dp[n-1][i]); } 
 » In the Hungarian implementation of Problem C, can anyone provide a general code that also includes finding which employee was allotted which job ? ( or in this case what was the final T for all input values) Thanks.
 » for B i used so simple approach and i have no proof of it but somehow it worked . if someone knows please tell me too. just counted max(adjacent count of(0) , adjacent count of (1)) = answer... thats it.. May be we need to do work when something is consecutive , this what i thought while solving
•  » » I think in the editorial it is proved. Actually adjacent count of(0)==adjacent count of (1) if you link the a with a[n-1]. So the answer is sum(s[i] == s[(i+1)%n]) / 2
•  » » » thanks buddy. i got it now
 » Can anyone explain the second proposal in B's editorial which says there will be equal pair of 0's and 1's if we add 1 to the left and 0 to the right
•  » » Either the no. of consecutive 0 pair and 1 pair are equal already or in the other case if no. of 1 pair is more than it could be only one more than the no. of 0 pairs and both ends will have 0 same for more 0 pairs than 1 pairs (I am getting this intuition) . I think this can be proved by using the fact that no. of 0s and 1s are equal, but didn't tried to prove yet. Sorry for bad english.
•  » » » Thanks!
•  » » » » welcome
 » There's a simpler dp for problem F that doesn't require any optimization. The state is just $dp[i]$, which is just the number of ways in which we can choose fisherman $i$ to be happy (again, we have the fishermen sorted in non-decreasing order). The overall running time is $O(n^2)$.If you've got any doubts about what's going on here, feel free to ask :)
•  » » I don't understand anything in there, can you explain it?
•  » » » Yeah, my bad. I should've realized that code is hard to read. I'll try my best to explain it.Firstly, the fishermen are sorted in non-decreasing order.Secondly, $smaller[i]$ is the number of $j < i$ such that $2 * a[j] > a[i]$. These are fisherman that we cannot place to the left of $i$ should we choose $i$ to be a happy fisherman. When I say some $j$ belongs to $smaller[i]$, it just means that $j < i$ and $2 * a[j] > a[i]$.Thirdly, when we have reached any state $i$, we have already placed all elements $j$ such that $j \le i$ excluding the elements which belong to $smaller[i]$, because as stated before, these are elements that we cannot place in front of $i$, so we must decide their positions later on after choosing some other happy fisherman (after changing maximums i.e. transitioning to another state).From here, I will describe the transitions in the dp ($dp[i]$ is the number of ways in which we can make fisherman $i$ happy). $i$ will denote the current state we are in, and $j$ the state we want to transition to. ($i < j$).When we make this transition, we place element $j$ in the first unoccupied position, and so there is only one way to place element $j$. The reason the first unoccupied position is always given to $j$ is because all the other elements that we are yet to be placed can only come after $j$, which in turn is because we cannot place it directly to the right of $i$ (either because they belong to $smaller[i]$ in which case we will get a content fisherman, or because they are just larger than $i$ and would result in a change in maximum).The other elements that we can place (and have to place) when making a transition from $i$ to $j$ are those belonging to $smaller[i]$ and those strictly between $i$ and $j$ but not belonging to $smaller[j]$. Let the number of such elements be $count$. Now we have some number of free positions that hasn't been occupied yet. This number is exactly $n - i + smaller[i] - 1$ (because we have already placed all elements before $i$ except for those belonging to $smaller[i]$, and have also placed $j$). We can freely place all $count$ elements in any order we wish in these positions. The variable $factor$ basically gives the number of ways to do this. We pick exactly $count$ of the free positions, and then we can freely permute the order of the fishermen in those positions, thereby giving what you see in the code. Then we just sum over all valid $j$ (i.e for all $j$ such that $a[j] \ge 2 * a[i]$) for each $dp[i]$ thereby giving an $O(n^2)$ solution. The answer is just $dp$ (according to how I've implemented it), and by default I take $a = 0$.
•  » » » » 10 months ago, # ^ | ← Rev. 2 →   There's also an O(sort fishermen) + O(n) solution which relies on mod being prime.Explanation is commented: https://codeforces.com/contest/1437/submission/99661882
 » Problem F is solvable in nlogn. Here is the code link https://codeforces.com/contest/1437/submission/96998991.
•  » » 11 months ago, # ^ | ← Rev. 2 →   You just need to see the minimum requirement of the customer. Your main motive is to sell more cans to the customer.For that you just need to check if(r
 » I would really appreciate an explanation for Problem A. I don't understand the one provided.
 » I can not understand the editorial of problem A,someone please explain in more detailed way.
•  » » For a continuous interval modulo operation, the result is continuous.So just let a = r + 1 and check if l >= a / 2
•  » » 11 months ago, # ^ | ← Rev. 3 →   You just need to see the minimum requirement of the customer. Your main motive is to sell more cans to the customer.For that you just need to check if (r
•  » » 11 months ago, # ^ | ← Rev. 3 →   Our main task is to find a number(a), such that, (l)mod(a),(l+1)mod(a),...(r-1)mod(a),(r)mod(a) is >= a/2. You want to maximize the result of mod operation, now a mathematical fact, if you have a number, say 8 and you want to choose another number (a) for which (8)mod(a) is max...what will you choose?? You will get max value when a is strictly greater than 8, for example (8)mod(9)=8, (8)mod(10)=8...so on. Imagine if you get a range (l,r)... Eg: (6,9) and you applied the above idea and chose a=10, (6)mod(10) = 6, and 10/2 =5, clearly (6)mod(10) is greater than 10/2, so the answer in this particular case is "YES". But what if you get a range (6,12) you applied above idea again and chose a=13, (6)mod(13)=6, and 13/2 is 6.5, which makes (6)mod(13)<13/2, so the answer in this case is "NO". Now to sum it up, the min value of (a) you can choose is such that the half of (a) is exactly equal to l, l=a/2 or a=2*l. also this (a) must be strictly greater than r, so that you get max value of (r)mod(a)... therefore we come up with this condition, r
 » Solution for A in C++ C++ codeSoltuion for B in C++( Much simpler solution than in editorial) Code of B
 » Hey guys, If it's okay with you guys I have created a telegram group to upsolve problems faster and get experience from other coders to learn things really fast. In the end, it's all about new techniques and different perspectives to solve the same problem so why not make a small group of friends to discuss problems after contests and random problems too. I think this way we would learn really fast and would able to improve our rating faster. Join this group if you think it's worth it. Join Group Here
 » in problem E's editorial i could not understand the part a[i] -= i; can anyone explain it to me ? thanks in advance !!!
 » check out for the simple approach for B submission
•  » » Can you please explain the idea behind that code? Thanks in advance.
 » 11 months ago, # | ← Rev. 3 →   In D, what we exactly needed to do? My approach:- Spoilerint c=1; for(int i=2;i
•  » » We will not increase depth every time when v[i]
•  » » » Thank you so much bro
•  » » 11 months ago, # ^ | ← Rev. 4 →   You can attach multiple nodes under a single node. You can also use multiple nodes of a single level to this. For example, 1 5 6 7 2 4 3 Here, we can do this, Level 0 = 1 Level 1 = 5 6 7(3 of them joined to node 1) Level 2 = 2 4 3 (2 joined to 5, 4 joined to 5, 3 joined to 6) But your code does this wrong. It will increment c when 2 < 7, so current c = 2.Then when it sees 3 < 4, again it will increment c and c becomes 3. But we could have attached this 3 to a node from the level 1 as we had more nodes in it.
•  » » » Yaa bro got it, Thank you so much
 » Problem A was rated 800. Is it a joke? I know it's a one liner, but just look at the number of people solved it during the contest. This problem is at least 1000. Probably 1100. People got angry because of the div. 4 rating inflation and now they increased the difficulty of all div 2 contests leaving the problem rating as before. Amazing job, guys.
 » Can someone explain the "slope trick" mentioned in the editorial of problem c. I do get the o(n^2) solution. The o(nlogn) solution seems to be interesting
•  » » I've implemented it here: https://codeforces.com/contest/1437/submission/97535399
 » Problem B was too tricky for div2 B..
 » Hello Everyone Can someone help me in understanding the hungarian implementation used in problem C I understood the algo but the code is just not striking if someone could help me with this code because this one is very smaller than any other available by google search.Can someone define the use of all the arrays/ vectors used in the hungarian function Thank You
 » Any idea how to solve G using Suffix array?
 » Why we are sorting in Chef MonoCarp Problem?
»
11 months ago, # |
Rev. 2

# include <bits/stdc++.h>

using namespace std;

int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); // your code goes here long long int t,n,a,i,j,c,x,l,r; cin>>t; for(j=0;j<t;j++) { cin>>n; for(i=0;i<n;i++) cin>>a[i]; dequeq; dequep; c=0; p.push_back(a); q.push_back(a); a[n]=LLONG_MIN; for(i=1;i<=n;i++) {

if(a[i]>a[i-1])
{
q.push_back(a[i]);

}
else
{
q.pop_front();
p.pop_front();

}
if(p.size()==0)
{
if(i!=n)
{
for(x=0;x<q.size();x++)
{
p.push_back(q[x]);
}
}
q.push_back(a[i]);
c++;
}

}
if(p.size()!=0)
cout<<c+1<<"\n";
else
cout<<c<<"\n";
}
return 0;

} Can someone help, why my code for problem D failing?please provide a test case where it fails?

 » What is the time complexity of Hungarian algorithm?
•  » » its O(n^3)
•  » » O(n^2*m)
 » Can somebody give a neat, clear, and detailed explanation for the B problem (understandable for beginners)
•  » » See the problem in a string is 00 or 11 coming somewhere. Now if you choose the substring like it has one 0 at one end and 1 at another. Like in the string if you choose and substring with end like above. 1110100100 like here 11/1010/0100 the substring 1010 if rotated will give 1101010100 . If we choose the substring with ends like these we can reduce the count of 11s and 00s with 2.And see after rotating the substring the count of 11s and 00s is reduced exactly by 2. So till after rotation we still have some 11s and 00s left we can again use a substring with such ends and rotate which will again reduce conut by 2. Just in case we are left with something like 0110 we will need one more switch and so the given algo works
 » And we can always reach this lower bound, by pairing 00 with 11 or with left/right border of s What Does This Mean Can Someone please elaborate
•  » » See the problem in a string is 00 or 11 coming somewhere. Now if you choose the substring like it has one 0 at one end and 1 at another. Like in the string if you choose and substring with end like above. 1110100100 like here 11/1010/0100 the substring 1010 if rotated will give 1101010100 . If we choose the substring with ends like these we can reduce the count of 11s and 00s with 2.And see after rotating the substring the count of 11s and 00s is reduced exactly by 2. So till after rotation we still have some 11s and 00s left we can again use a substring with such ends and rotate which will again reduce conut by 2. Just in case we are left with something like 0110 we will need one more switch and so the given algo works
 » For Problem C, i used greedy approach by looking at the tag which is on the problem statement. But greedy actually don't work. In that case, are people above 1900 misleading with tags or is there actually a greedy solution for problem C ? Please let me know. Thanks.
•  » » I suppose that it's rather difficult to write a right greedy solution for this problem. I tried to write some of them but with different ideas, also i tried to combine but it didnt work, tooSo after that I hoped to find greedy solution in top50 of the round but nobody did it. only dp and graph. So I suppose there is no greedy solution
 » 11 months ago, # | ← Rev. 4 →   Hello! Can somebody send the classical venger algorithm code? P.s I saw the venger algorithm in solution, but, to tell the truth, it was difficult to understand it for me. Thanks!
 » The B solution is so precise.I learn a lot from it!
•  » » What if we want to print those steps of converting the given string into an alternating string. Can you please help me with it's precise algorithm.?
 » Can anyone explain the first approach of B. Please.
 » Can someone please explain the logic of DP in C problem (beginner friendly). Not able to understand the editorial properly . Thanks !
•  » » You can have a look at my submission..It is commented and easy to understand I guess. C//Think simple yet elegant. #include using namespace std; #define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ll long long #define all(v) v.begin(),v.end() #define ff first #define ss second #define pb push_back #define mp make_pair #define pi pair #define REP(i,n) for(int i=0;i> q; while(q--){ cin >> n; vector t(n); for(i=0;i> t[i]; //dp[i][j] -> min cost of taking out a total of i dishes uptil j minutes. //We take out the current dish i at time j -> dp[i][j] = min(dp[i][j],dp[i-1][j-1]+cost); //The "cost" should also be optimal as such. Else there is no point in doing dp knowing that some better cost exists. //In this case such an optimal cost exists when we sort t. //We dont take out the current dish i at time j..so we have processed i-1 uptill time j AND j-1. -> dp[i-1][j] = min(dp[i-1][j],dp[i-1][j-1]); //ans would be min(dp[n][x]) sort(all(t)); vector> dp(n+1,vector(2*n+1,1e9)); dp = 0; for(i=1;i<=n;i++){ for(j=1;j<=2*n;++j){ dp[i][j] = min(dp[i][j],dp[i-1][j-1] + abs(t[i-1]-j)); dp[i-1][j] = min(dp[i-1][j],dp[i-1][j-1]); } } int ans = 1e9; for(j=1;j<=2*n;++j)ans = min(ans,dp[n][j]); cout<
 » Can Someone help me to find what is wrong with this solution 97843215 for problem E
 » G let me review the AC automaton... but TLE on test 70 :(, maybe cuz the pointers in my ugly code spend too much time...
•  » » Passed! I used a heuristic method: maintain a pointer "nex", which is the closest node in the fail tree that is the end of some $s_i$. When initing the fail pointer of AC, the nex pointer can be inited by the way. Then, we can jump faster. I don't know whether the worst complexity is still $O(q\sqrt n)$, but it works.
 » For problem G, why is "Thus, that's bounded by the square root of the total length." true? (Actually, I'm also confused by what's meant by "that").
 » Problem B. Can someone explain why is the answer number of consecutive pairs divided by 2?