### Ari's blog

By Ari, history, 4 weeks ago,

Note: I can't figure out how to place the tutorials inside spoilers. If someone is familiar with how CF spoilers work and can help I would really appreciate it. For now, be warned that the tutorials are visible by default (but everything else isn't)

UPD: I figured out how to use spoilers! Also added implementations for all problems.

Thanks for participating in our contest!

1509A - Средняя высота

Author: Kuroni
First solve: cfg0d at 00:01:02

Hint
Tutorial

Implementation

1509B - ТМТ документ

Author: Ari
First solve: blue5002 at 00:04:51

Hint
Tutorial

Implementation

1509C - Спортивный фестиваль

Author: Ari
First solve: Dukkha at 00:05:32

Hint 1
Hint 2
Tutorial

Implementation

1508A - Бинарная литература

Author: Ari
First solve (Div. 2): traxex2 at 00:22:48
First solve (Div. 1): tourist at 00:05:04

Hint 1
Hint 2
Hint 3
Tutorial

Implementation

1508B - Почти отсортированные

Author: Both of us!
First solve (Div. 2): shenmadongdong.qaq at 00:29:52
First solve (Div. 1): tourist at 00:08:19

Hint 1
Hint 2
Hint 3
Tutorial

Implementation

1508C - Завершите MST

Author: Kuroni
First solve (Div. 2): deepspacewaifu at 01:42:36
First solve (Div. 1): maroonrk at 00:33:17

Hint 1
Hint 2
Hint 3
Hint 4
Tutorial

Implementation

1508D - Обмены на ребрах

Author: Ari
First solve: ksun48 at 00:57:13

Hint 1
Hint 2
Hint 3
Hint 4
Tutorial

Implementation

1508E - Дерево-календарь

Author: Kuroni
First solve: ecnerwala at 01:02:26

Hint 1
Hint 2
Hint 3
Hint 4
Tutorial

Implementation

1508F - Оптимальное кодирование

Author: Kuroni
First solve: ecnerwala at 01:52:02 (The only contestant who solved this problem!)

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Hint 6
Tutorial

Implementation

Finally, here's some funny moments that happened while we were working on the round :)

Memes

• +431

 » 4 weeks ago, # | ← Rev. 2 →   +9 Longest editorial I ever had the chance to read. Anyways thanks, it's awesome <3
•  » » 4 weeks ago, # ^ | ← Rev. 2 →   +23 Sorry, as we stated the spoilers were not working to our favor :(
•  » » » 4 weeks ago, # ^ | ← Rev. 2 →   +7 It's pretty well documented editorial, thank you once again!!
 » 4 weeks ago, # |   0 For Div 2 D, can somebody find a case where it fails? submission, I think on one case it doesn't print any string because my answer gets over n*3.
•  » » 4 weeks ago, # ^ |   0 If you find any mistake or corner case please reply me, my solution fails in the same testcase with the same error (In the test 2 case 43)Thanks!
•  » » » 4 weeks ago, # ^ |   0 I found one: 1 3 001111 110110 000000
•  » » » » 4 weeks ago, # ^ |   0 https://codeforces.com/contest/1509/submission/113292585 i did according to the tutorial but my submission gives WA on test 9. Where am I going wrong? please someone help
•  » » » » » 4 weeks ago, # ^ |   +3 Your mistake, is a common mistake, since a I had the same mistake use a variable ans and print it just once, your mistake is in the fisrt if, you forgget the endl before the return.skipping this you code have a good implementation good look bro!PSD: I submmit your code for be sure that im right and sorry for my bad english
•  » » » » » » 4 weeks ago, # ^ |   0 Thanks a lot
 » 4 weeks ago, # |   -7 gg
 » 4 weeks ago, # |   +11 i think problem E div1 can be done with HLD & treap by reversing the operations :thinking:if any one solved the problem with the similar solution please share it : )
 » 4 weeks ago, # |   0 This contest was amazing and specially it's editorial after all Thanks for preparing such contests and we're waiting for more contests from you! :D
 » 4 weeks ago, # |   0 Thank you!
 » 4 weeks ago, # |   +4 I don't understand why my B solution is wrong, pls help 113225551
•  » » 4 weeks ago, # ^ |   +3 you go through forward and taking the count of Ts and Ms do the same thing for backward it can't be less Ts from Ms from froward and from backward
 » 4 weeks ago, # |   0 Can someone explain c
•  » » 4 weeks ago, # ^ |   0 Can someone please explain recurrence relation in C. Not able to understand.
 » 4 weeks ago, # |   +59 My solution to F runs in $O(n^2)$, and is quite simple. Here's my simple condition for an edge being in the graph:For any vertex $v$, there are 4 candidates for edges involving this vertex: consider the intervals containing $v$ which extend the furthest left and the furthest right; let those bounds be $l_v$ and $r_v$. Then, $v$ can have edges to its predecessor in $[l_v, v]$, its successor in $[l_v, v]$, its predecessor in $[v, r_v]$, and its successor in $[v, r_v]$. Furthermore, an edge exists iff it is a valid candidate for both endpoints.Then, we can just maintain these candidates and the counts in $O(n^2)$: starting from the initial state, $l_v$ decrements and $r_v$ increments at most $O(n^2)$ times in total. The code is very short, and runs in less than 1 second.
•  » » 4 weeks ago, # ^ | ← Rev. 2 →   +26 I'm actually surprised about your solution; I had an $O(nq)$ solution but I didn't know that $O(n^2)$ exists (probably the reason is because I abruptly changed the constraint from $n = q = 50000$ to the current one like 1 day before the round).Anyway, this solution is very clean and also interesting, thanks.
 » 4 weeks ago, # |   +1 Why this solution for Div2 C gets TLE despite that fact that it's $O(n^2)$ ? It is because it should be written iteratively ?
•  » » 4 weeks ago, # ^ |   +38 Array bound
•  » » » 4 weeks ago, # ^ |   +2 Kuroni Yup, AC. Thanks a lot!
 » 4 weeks ago, # |   +8 Is there any greedy solution for Div2C? It was tagged greedy in the problemset, hence wondering :)
•  » » 4 weeks ago, # ^ |   +16 Maybe the fact that the minimum and maximum should be at the end is a greedy intuition?
•  » » 4 weeks ago, # ^ |   0 sorting step; a[i] is either the minimum or the maximum of the first i elements.
•  » » 4 weeks ago, # ^ | ← Rev. 2 →   0 I suppose the greedy solution should be based on constructing the solution array in reverse order, the last element being largest or smallest ,for which we choose the answer by greedily choosing which gives a lower sum for leftover n-1 elements, that is For n elements : discrepancy = max — min For n-1 elements : discrepancy = second_max — min OR max — second_min (greedy choice) ..... This should be achievable with two pointers for current max and min .EDIT : this approach only test for each successive term and doesn't indicate whether the sum of discrepancies will be in same relation as maximum discrepancies.So you have to go with a dynamic programming approach for the answer
•  » » » 4 weeks ago, # ^ |   0 How did you get to the EDIT observation ? Can you give some test I am confused at this point.
•  » » » » 4 weeks ago, # ^ | ← Rev. 3 →   0 I learnt it the long way : The above greedy approach works for shown 4 test cases . But fails for the first hidden test , i.e., 5th test case . So I ran the dp approach for the same problem and reconstructed the array of elements for that case. The last element (first choice) was different for both the cases, --> the max discrepancy is not indicative of sum of discrepancies. I suppose a simple example would be : 5 29 30 67 68 82, dp answer is 121 while greedy leads to 131the difference can be seen in the first choice itself, the sequence via greedy approach is : (68, 67), 30, 29, 82 while the best sequence is : (68, 67), 82, 30, 29 I used a random generator to get this sequence LINK TO CODE:( too lazy to sit on computer for more than half an hour, trying to improve sitting hours tho, hopefully i can do 3+ questions next contest :)
•  » » » » » 4 weeks ago, # ^ |   0 Thanks, use Ideone for code pasting.
•  » » » » » » 3 weeks ago, # ^ |   0 Thanks for the suggestion , Ideone seems great.
 » 4 weeks ago, # |   +1 I dont think my div1A/div2D is supposed to pass. Can someone hack ?
•  » » 4 weeks ago, # ^ | ← Rev. 2 →   +9 Done, random generator found a test that fails.Here is also a smaller test case (n=6), there is no example with smaller n. Maybe it helps: 000001011110 111100100000 111111111111 
•  » » » 4 weeks ago, # ^ |   +2 How you are generating these cases? (some online website or you have your own generator)
•  » » » » 4 weeks ago, # ^ | ← Rev. 2 →   0 For the original test I just generated a bunch of random inputs and tried to see if the solution works (using my own program, basically just a while loop that generates a random input (50% chance for 1 and 50% for 0 at each position) and then runs a simplified version of his code to see if it finds an answer).For finding the n=6 case, I went over all the possible inputs for n=1...6 and tried each one. There arent too many possible inputs for n=1,2,3,4,5 and for n=6 it found one quite quickly.
 » 4 weeks ago, # | ← Rev. 2 →   0 In problem 2D, after we find whether out frequent character is '0' or '1', what to do ? like how to iterate and create the resulting string ?
•  » » 4 weeks ago, # ^ |   0 The implementation given is based on first approach. You can look at the mix function.
 » 4 weeks ago, # | ← Rev. 3 →   +2 Since so many people were asking for problem C (div2), I decided to make a video tutorial for it.http://youtube.com/watch?v=7CPgT3gYPocHope you like it :)
 » 4 weeks ago, # |   +1 i failed to recognise these types of dp problems during short contests.so i know i need to practise more of these kinds of problem .it will be a great help if you people suggest me some similar kind of problems .
•  » » 4 weeks ago, # ^ |   +1 Search DP Tagged problems on CF and Leetcode. Some other problems:Longest Palindromic Substring (link)Longest Palindromic Subsequence (link)Matrix chain multiplication (similar problem to div2C)Burst the balloons (link)
•  » » » 4 weeks ago, # ^ |   0 thanks
 » 4 weeks ago, # |   +13 Auto comment: topic has been updated by Ari (previous revision, new revision, compare).
 » 4 weeks ago, # |   +1 I cannot understand div2C, help me!!. we have to minimize discrepancies so why are we adding the element having greatest discrepancies. like An-A1???
•  » » 4 weeks ago, # ^ |   +2 I will try to explain my approach. First thing to notice is that discrepancy for i = n-1 ( 0- indexing ) will be fixed (max element — min element). Now we put either the max element or the min element at the (n-1)th position that is the only possible way to change the discrepancy for (n-2)th index. Now choosing greedily won't work because we won't know whether our current best move would be a better decision in future also. This is very obvious that we will require to check all possible combinations hence dp is used to optimize the same. So let's talk about,1. dp States : sort the given array, let's say 'L' = any index to left and 'R' denotes any index to right( greater than 'L')- this would mean we have the option to choose between Lth element or Rth element for some index 'i'.2. Transition function : this is simple as you can choose between L or R so dp[L][R] = min(go(L+1, R) , go(L, R-1))3. Base case : L should be less than R Codeint go(int l, int r){  if( l > r) return 0; if(dp[l][r] != -1) return dp[l][r]; dp[l][r] = 0; dp[l][r] = (a[r] - a[l]); dp[l][r] += min(go(l+1,r), go(l,r-1)); return dp[l][r]; } void solve(){ int n; cin>>n;  for(int i =0;i < n;i++) cin>>a[i]; for(int i =0 ;i < 2001; i++){ for(int j = 0;j <2001;j++){ dp[i][j] = -1; } } sort(a,a+n); cout<
•  » » » 4 weeks ago, # ^ | ← Rev. 2 →   0 So you are first sorting the array and then checking if it is giving an optimal answer for if I put l+1,l+2...or r-1,r-2... elements in that place and finding the minimum of all the possibilities. Is that what you are doing??
•  » » » » 4 weeks ago, # ^ |   0 No, starting from last index I am trying to put L ( min element for current index i ) or R (max element for current index i ), sorting the array helped me to get imax, min easily/optimally but still this can be exponential (as at every index i can have two options ) so I optimized it using dp.
•  » » » » » 4 weeks ago, # ^ |   0 got it now thanks
•  » » » 4 weeks ago, # ^ |   0 hi can please explain what your transition equation is doing like why you are going from l+1 to r and l to r-1?
•  » » » » 3 weeks ago, # ^ |   0 since the array is sorted, l+1, r means I pick smallest element (rth element) and l , r -1 means I pick largest element ( rth element).
 » 4 weeks ago, # |   0 I enjoyed this round even though I sucked at it, the editorial is very well written and will help me learn.Cheers bois
 » 4 weeks ago, # |   -17 https://youtu.be/28njldrBEo4problem A hope it helps someone ;)
 » 4 weeks ago, # |   0 Problem C : What exactly do those transitions represent ? and if anyone has some intuitive or any other way to deduce the state of that dp ?
•  » » 4 weeks ago, # ^ |   +2 I explained it in my youtube video But I'll try to give a brief description of my solution (slightly different from editorial)Basically as mentioned in the editorial we start from the back because we have complete knowledge of d[n]d[n]= maximum element in a — minimum element in a (Coz it is for the whole array)Now to minimize d[n-1] we have to remove maximum element or minimum element coz otherwise d[n-1]=d[n]So after sorting at every stage we have two options either we remove from right side or left side. This gives us a feel for a dp approach because the same state of array can be reached by taking different paths(overlapping subproblems)So we define dp[i][j] as we have removed elements from left (minimum) i times and elements from the right (maximum) j times. We can obviously deduce the maximum and minimum in the current array as jth maximum and ith minimum in original arrayThis gives us the transition cost -> jth maximum element — ith minimum element As we can reach state (i,j) from (i-1,j) and (i,j-1)So basically dp[i][j]=min(dp[i-1][j],dp[i][j-1]) + cost (Very much like grid DP ) The answer is minimum sum we can get after removing exactly n-1 elements
•  » » » 4 weeks ago, # ^ |   +3 Beautiful it is.
•  » » » » 4 weeks ago, # ^ |   +4 There is an explanation for the author's Dp State that I liked and suhail.loya described in a youtube comment. The author's solution says that in the optimal solution, suppose after reordering the elements it is a[p1],a[p2],a[p3],..a[pn] where p1,p2,..,pn is a permutation of 1 to n, every prefix of this reordered optimal sequence will be a subarray of the sorted sequence. This makes sense because it is always optimal to put the maximum or minimum in the current prefix in the last step. This is basically same as my solution in which I remove elements only from the "ends" of the sorted array so the result will always be a subarray. So the author's solution starts with a subarray of size 1 for which the answer is 0. Now to expand this subarray either you can add one element to the left, or add one element to the right so at every state we have two options just like my solution(where I remove one element from left or right). State Description: dp[l][r] is the minimum cost you can create such that the elements of the current array are a[l],a[l + 1],...a[r] in the sorted array a.Transitions : You can reach state (l,r) from (l+1,r) [ expanding to left] or (l,r-1) [expanding to the right]. Cost of expanding will be maximum-minimum in the subarray a[l]....a[r] which is a[r]-a[l] since it is sorted.So : dp[l][r] = a[r] - a[l] + min(dp[l + 1][r], dp[l][r - 1])Answer : = dp[1][n].
•  » » » » » 4 weeks ago, # ^ |   0 Glad you liked it :)
•  » » » » » 4 weeks ago, # ^ |   0 every prefix of this reordered optimal sequence will be a subarray of the sorted sequence. jalotra Can you please explain why it will always be true?
•  » » » » » » 3 weeks ago, # ^ |   0 Example: s[] = 29 30 67 68 82optimal[] = 67 68 82 30 29, optimal d = 121.See that for each prefix i from 0 to len(s) — 1, the optimal sequence is some contiguous subarray of the sorted sequence.Proof : Let's choose a single starting speed S : s[k] => d[1] = 0 here. Now I have to add one element at the back of it.2a) S : s[k] => Choose either s[k + 1] s[k - 1], because choosing some other element will make d[2] higher.3a) S : s[k] s[k + 1] => Choose either s[k - 1] or s[k + 2].3b) S : s[k] s[k — 1] => Choose either s[k + 1] or s[k - 2].suhail.loya Is this correct?
•  » » » » » » » 3 weeks ago, # ^ | ← Rev. 2 →   0 Example: s[] = 29 30 67 68 82 optimal[] = 67 68 82 30 29, optimal d = 121. jalotra consider prefix for i = 3 in optimal solution (67 68 82 30) .How is this a contiguous subarray of s?
•  » » » » » » » » 3 weeks ago, # ^ | ← Rev. 2 →   0 The key observation is at every stage while expanding from the current sequence, you want to add an element which is either the minimum of the new sequence or maximum of the new sequence. So you want to either take one step left or one step right in the sorted sequence.What I meant by the statement "every prefix of this reordered optimal sequence will be a subarray of the sorted sequence." that every prefix will contain elements which are in a continguous subarray of the sorted sequenceSo prefix for i=3 (67 68 82 30) although is not a subarray of s[] in exact order but it contains only the elements in the contiguous subarray of s[] from index 1 to 4 (30 67 68 82)This result is more obvious in the solution I describe in this comment
•  » » » » » » » » » 3 weeks ago, # ^ |   0 suhail.loya Thanks for helping me out :).I was confused at the subarray part but now it is clear. Yeah, the exact order thing was creating the problem for me.
•  » » » 4 weeks ago, # ^ |   0 Nice Explanation . Thanks
•  » » » 4 weeks ago, # ^ |   0 So basically we can imagine that we are adding elements to an array let's call it A we know that the last element of that array is either the maximum or the minimum (because otherwise it will make the cost of the function d a lot bigger hence d(n) = max(A) — min(A)) and we work backwards by asking which is the last element max(A) or min(A) and after that we ask the same question for the next biggest and smallest elements left in the original array and here we observe that it must be dp
•  » » » 4 weeks ago, # ^ |   0 In the last line i think you meant dp[i][j] = min(dp[i + 1][j], dp[i][j — 1]) + Cost. Nice explanation though.
•  » » » » 4 weeks ago, # ^ |   0 No, according to my dp states it should be min(dp[i-1][j],dp[i][j-1])My states are dp[i][j] -> min cost we can get after removing i elements from minimum side and j elements from maximum side. So to reach (i,j) you can remove one element from maximum side in (i,j-1) or one element from minimum side in (i-1,j)Code
 » 4 weeks ago, # |   +14 The second solution for Div1A/Div2D is beautiful.
•  » » 4 weeks ago, # ^ |   0 can anyone explain second solution for Div2D im still unable to understand the math over there :/
•  » » » 4 weeks ago, # ^ |   +4 Let's say we have these strings, and we currently our pointer is pointed to bold ones in all three of them. (say p1, p2, p3) 0 10101 1 10011 1 00100 Since we have three pointers pointed to three different strings consists only of 0's and 1's, then atleast two of them must point to either 0 or 1.(In this case, p2 and p3 to 1) Now after adding the "1" to our answer, we increment pointer p2 and p3. 0 1 0101 1 1 0011 1 0 0100 Now p1 and p3 is pointed to "0". So in next step, we increment p1 and p3 by 1. 0 1 0101 1 1 0011 1 0 0100 We repeat this process until one of them is completely exhausted. 0101 01 1100 11 (exhausted) 1001 00 Our ans in this case comes to be 1010011 (len = 7) Now in simple terms, len + (one of the remaining in p1 (01) and p3 (00) ) is always less than or equal to 9 (3n). Complex termsLet's suppose our ans has k(7 here) characters, and thus the pointers have advanced by at least 2k(14 here), since at each step atleast two of them is advanced. We have exhausted the characters of s2, so we have advanced p2 by 2n, and the other two pointers have advanced 2k−2n (8 here), and thus one of them has advanced by at least k−n (4 here). Now just add the remaining characters of this string to t. There are at most 2n−(k−n)=3n−k of them, so in the end t has at most 3n characters.
•  » » » » 3 weeks ago, # ^ |   0 Thanks alot for your detailed explanation , now its clear
 » 4 weeks ago, # |   0 Could someone please explain how they approached (thought process) problem C in DIV 2 during the contest. How they arrived at greedy idea that it's always better to place the smallest/largest value in the end in DP transition.
•  » » 4 weeks ago, # ^ |   0 Discrepancies are calculated at every stage, so that means in the last stage when we'll have all the elements of the array and the discrepancy for the last stage will be equal to max(a1, a2, ...an) — min(a1, a2, ... an), this difference will be maximum among all the differences that you can have with the elements of the array. So this difference should occur at the last stage only because if it occurs at some stage i which is before the last stage, this difference will keep on adding in our solution and our final result will not be optimal. So this means that either of the max value of the whole array or the min value of the whole array should be added in the end, so this max difference will be added only once which is in the last stageI hope that helps :)
 » 4 weeks ago, # |   +9 Nice Editorial tho
 » 4 weeks ago, # |   0 Can anyone please help me where I am getting wrong?I have considered two cases the first and the last character should not be M 2 The number of T should be even and the number of M should be half of the number of TAm I missing anything?
•  » » 4 weeks ago, # ^ |   0 TTTMMT
•  » » » 4 weeks ago, # ^ |   0 Thank you for the reply can you please tell me how to tackle this test case
•  » » » » 4 weeks ago, # ^ |   0 basically keep a count of Ts and Ms in forward and reverse directions..if at any point count of Ms is less than Ts break and print NO refer to this https://codeforces.com/contest/1509/submission/113349202
•  » » » » » 4 weeks ago, # ^ |   0 Thank you very much for the help!
 » 4 weeks ago, # |   0 Can anyone prove problem C can or cannot be solved with greedy? I use greedy and cannot pass example 3.
 » 4 weeks ago, # |   0 Getting trouble in deciding the states for dp in C(The sports festival) can any one tell how to decide the state of dp like in two state dp problems like this?
•  » » 4 weeks ago, # ^ |   0 Refer to my post above : https://codeforces.com/blog/entry/89644?#comment-781189
 » 4 weeks ago, # |   0 Can anyone explain E Solution? Not very clear from tutorial
 » 4 weeks ago, # | ← Rev. 2 →   0 In problem C, I don't get how does dp(l+r, r) correspond to placing the smallest element at the end of the sequence.
•  » » 4 weeks ago, # ^ |   0
 » 4 weeks ago, # |   0 In the Problem div2B, shouldn't this statement: "A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters" be corrected as: "A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero) characters, without changing the order of the elements"? I wasted some time thinking if the order is important or not -_-
•  » » 4 weeks ago, # ^ | ← Rev. 2 →   +25 Why stop there?"... without changing the order of the elements, without inserting new characters, without turning the characters upside down,..." The list can go on.
 » 4 weeks ago, # |   0 editorial is awesome
 » 4 weeks ago, # |   0 Great contest. Even better editorial.
 » 4 weeks ago, # |   0 Great contest great editorial
 » 4 weeks ago, # |   0 I don't understand why my solution for Div1A is wrong. can somebody please help me check? 113203126. My method was just to try each pair of strings, and for every character of the strings, if they were different, my answer would add each of the characters once, and if they are the same, it would add just that one character. This generates three different solutions, and we take the shortest one. It should guarantee that the shortest string would have a length shorter than 3n. Please help, thanks
•  » » 4 weeks ago, # ^ | ← Rev. 2 →   0 Sometimes the length of your ans string is less than 3*n. Test case : n=2 0101 1010 1011 I fixed that but still, it gave WA. Then I found this case out. It is possible to make n+1 mismatches(positions in which the bits differ) in between the pairs of the strings. Thus the final string would be of length 3*n+1. 4 00011111 11100111 01010000
•  » » 4 weeks ago, # ^ |   0 What about,000011110000 111100000000 000000001111BTW, Same mistake ):
•  » » » 4 weeks ago, # ^ |   0 oh wow. thank you guys. I can't believe I made this logic error
 » 4 weeks ago, # |   0 Anyone, please I need help for the problem TMT Document(#715-DIV-2/B). My solution https://codeforces.com/contest/1509/submission/113289284 This solution is giving me 'runtime error, it is running fine and giving me output as normal in my IDE. I also think that I have passed the test cases.. any guess??
•  » » 4 weeks ago, # ^ |   0 Try for case: MMM (Cases where count of M is more than count of T)
•  » » » 4 weeks ago, # ^ |   0 thanks, It really helped. I had just forgotten about this edge case!!
 » 4 weeks ago, # |   -21 Speedforces :))
 » 4 weeks ago, # |   0 questions are really good...thank you for such good contest
 » 4 weeks ago, # |   0 Is there anyway to bound the maximum of minimum hamming distances of three distinct binary strings? I'd appreciate if someone can give out a proof or explain their intuition behind this. Thanks.
 » 4 weeks ago, # |   0 In TMT question I used counting. First incrementing count when T is found and when M is found then decrementing. Also another variable for counting m. If at anytime count becomes negative , solution is impossible. At the end I checked for if count of T == 2 * count of M. Can anyone help me why this solution was not working ?
•  » » 4 weeks ago, # ^ |   +1 Check your solution on: $TTTMMT$
•  » » » 4 weeks ago, # ^ |   0 Thanks , I may have missed some test cases like these.
•  » » 4 weeks ago, # ^ |   0 Could you elaborate on which 'count' becomes negative are you referring to?
•  » » » 4 weeks ago, # ^ | ← Rev. 2 →   0 ct — count of T , cm — count of M , cnt- just for checking if it becomes negative midway or notSo I was referring to 'cnt'.
•  » » » » 4 weeks ago, # ^ |   0 Could you share the submission, I think it may be an implementation detail
•  » » » » » 4 weeks ago, # ^ |   0 My implementation is exactly the same as jalotra's given below , just with a difference that I did not checked for reverse.
•  » » 4 weeks ago, # ^ | ← Rev. 2 →   0 Did you check for the reversed string as well? If yes this solution should work.
 » 4 weeks ago, # |   0 I'm new here but I don't understand my solution gets TLE on test 13 of problem div2/C. To me is exactly the same solution as in the editorial (but in Python). Could someone please check? https://codeforces.com/contest/1509/submission/113305170
 » 4 weeks ago, # |   0 So far one of the best tutorials
 » 4 weeks ago, # | ← Rev. 3 →   0 Hi, can somebody tell me I got WA on test2(C++) in B? Code: 113309060. Thanks : The accepted solution in Python: 113304387
•  » » 4 weeks ago, # ^ |   0 AC solution c++ — 113310825 compare to find your mistake (this is your code)
•  » » » 4 weeks ago, # ^ | ← Rev. 2 →   0 Thank you very much! I forgot about long long. Now, it works.
 » 4 weeks ago, # |   -6 Ari sorry if i'm wrong, but in my opinion 2D is much simpler than 2C. In D, the solution comes to mind immediately, and in 2C, vice versa.
•  » » 4 weeks ago, # ^ | ← Rev. 2 →   0 You are not wrong, but many people will disagree with you because we have tested and testers rated 2D harder than 2C :) Heck, at first we even proposed 2D as 2B since we had the same opinion as yours (you can check our comments on the problem).
 » 4 weeks ago, # |   0 In Div2 problem C, I understood that for every subarray we have two choices either we can remove leftmost element or the rightmost element and take the minimum out of them but why at every state we are adding s[r]-s[l] to the minimum? couldn't just we store dp[i][j]=min(dp[i][j-1],dp[i+1][j])? i didn't understand this part can someone explain this more clearly.
•  » » 4 weeks ago, # ^ |   0 because at evry state you already know that answer is atleast s[r]-s[l]. since we have sorted the array . Think this like you know the answer of the current length lets we have exmple n = 6 and arr = [1,3,3,3,6,6]; dn = s[5]-s[0] = 5 now dn-1 = you choose from (arr[0] to arr[4]) or (arr[1] to arr[5]) and you have to add that dn also for complete answer.
•  » » » 4 weeks ago, # ^ |   0 ohh right now i got it, we know that for any subarray the value will be atleast s[r]-s[l] no matter how we arrange the elements we just need to find the minimum score by removing leftmost and rightmost which we have stored in the dp and add that value for the complete answer of the current subarray. thank you :)
 » 4 weeks ago, # |   0 Please suggest some problems like div 2 C (matrix chain multiplication). Thanks in advance.
 » 4 weeks ago, # |   +1 Great set, even better editorial! Awaiting more rounds from you!
 » 4 weeks ago, # |   0 Ari, Please share with us what mistake were you doing in the first place that caused spoiler tags to overflow? Also, how did you rectify it?
 » 4 weeks ago, # | ← Rev. 3 →   0 In Problem C of div 2 could someone please explain and give a testcase why this approach fails ??? I did this and got WA on testcase 5 .Keeping the most frequent elements in the front and if frequency of two elements are same then keep the smaller among them in the front.
•  » » 4 weeks ago, # ^ |   0 your approach fails on test case 1 3 3 3 9 9. According to you answer should be 3 3 3 9 9 1, which gives a sum of d's = 20. But there exists another optimal solution 3 3 3 1 9 9 which gives a sum of d's = 18. So, check your solution on this test case.
 » 4 weeks ago, # |   +1 can somebody please tell me why i am getting tle in case 18 in 3 question if i initialize dp with 0 and ac when initiallizing with -1?please tell me when to initialize with -1 and when with 0
•  » » 4 weeks ago, # ^ |   0 because dp[i][j] value can be zero ( max == min ) let any pos dp[3][6] = 0 ; Now whenever dp[3][6] will call then it will again compute. That's why zero not working.
•  » » » 4 weeks ago, # ^ |   0 thanks
 » 4 weeks ago, # |   0 Problem D Solution video in HINDI. if anyone is having any problem in understanding the problem. This video and the solution discussed might help. https://www.youtube.com/watch?v=OxueGHcZYyY
 » 4 weeks ago, # |   0 For this submission of div2-D https://codeforces.com/contest/1509/submission/113251552 can someone provide a small enough test case. I was able to make a test case using random generator but it was large and not useful to find mistake.And i tried to find one myself but failed to do so. So a test case or a logical prove to show that the solution is wrong would be very helpful :) .
•  » » 4 weeks ago, # ^ |   +4 4101111000011110100000000
•  » » » 4 weeks ago, # ^ | ← Rev. 2 →   0 thanks, also you found it so quickly, i was also trying to make a similar kind of case but failed :( Also string 1 is just reverse of string 2 XD
•  » » » » 4 weeks ago, # ^ |   +4 It's pretty easy to find it: Codeint main() { cin>>n; while (1) { string a,b,c; for (int i=0;i<2*n;++i) a.pb(rand()%2+'0'); for (int i=0;i<2*n;++i) b.pb(rand()%2+'0'); for (int i=0;i<2*n;++i) c.pb(rand()%2+'0'); if (check(a,b).empty() && check(a,c).empty() && check(b,a).empty() && check(b,c).empty() && check(c,a).empty() && check(c,b).empty()) { cout<
•  » » » » » 4 weeks ago, # ^ |   0 Oh ok , I actually tried generating some random cases but didn't got any small case so i thought it was not possible for small cases.Now i feel estupid XD.
 » 4 weeks ago, # |   +28 in Div2E, how to DFS on the unassigned edges ? In general, the number of unassigned edges could be up to 1e10 so trivial DFS will cause TLE
•  » » 4 weeks ago, # ^ |   +11 Refer to 920E - Connected Components?
•  » » » 4 weeks ago, # ^ |   0 Thank you !!
 » 4 weeks ago, # | ← Rev. 2 →   0 I solved Div2C using ternary search. Any idea for the proof. 113242501Update: hacked
 » 4 weeks ago, # |   +28 Let me highlight that d1D is really interesting to solve. Thanks for such a great task!(btw, I failed to spot that d1E was indeed an attempt to 1477D - Nezzar and Hidden Permutations xD)
•  » » 4 weeks ago, # ^ |   0 I remember when I came up with this idea to solve your problem I was soo excited, then I saw it was wrong x(
 » 4 weeks ago, # |   0 why I'm getting TLE in problem C for Div2 even it working in O(n^2)? my solution
 » 4 weeks ago, # |   0 https://youtu.be/Z2cN0xLlNe4Problem B solution
 » 4 weeks ago, # |   0 Thanks for such an amazing editorial
 » 4 weeks ago, # | ← Rev. 3 →   0 Can anyone give me a test case or tell me what's wrong with my solution in div.2 D My approach was to try every possible 2 strings and greedily make a resulting string such that it contains both strings and check if its size is less than 3*n.Submission link getting wrong answer on test 843 of test-set#6
•  » » 3 weeks ago, # ^ |   0 Check this link :https://www.youtube.com/watch?v=-FGrj4umVR8 potentially did the same approach as you did
 » 3 weeks ago, # |   0 DIV 1 (A) OR DIV 2 (D) Video tutorial Link: https://www.youtube.com/watch?v=-FGrj4umVR8
 » 3 weeks ago, # |   0 can anyone tell why my submission getting wa in test case 3 113414122
•  » » 3 weeks ago, # ^ |   +8 5000000000000001111111110101010
 » 3 weeks ago, # |   0 How to solve Div2E/Div1B recursively? or how to find the length of the first block?
•  » » 3 weeks ago, # ^ | ← Rev. 2 →   0 Let's suppose there are n elements. Then, if you place a 1 at the front, then there will be n-1 positions to fill; for 2, n-2; similarly for 3, n-3 and so on. Eg. if you put 3 as the first element, this is how the array will look like.3, 2, 1, ....n-3 elementsNow, if you have n elements, then there are 2 ^ n-1 almost sorted permutations. So, if you put 3 at front, then the minimum ranking of an almost sorted permutation that starts with 3 will be 2^n-1 + 2^n-2 (because permutations starting with 1 and 2 will precede 3). Eg. 1, ...n elements (total: 2^n-1) 2, 1, ...n-2 elements (total: 2^n-2) 3, 2, 1, ... n-3 elements (total: 2^n-3).Thus if the k is between 2^n-3 (inclusive) and 2^n-4 (exclusive), then the permutation definitely starts with 3, and now you need to find rest of the elements of the permutation in this way recursively. Hope this clears it up.Btw, did you understand how the second solution works?
•  » » » 3 weeks ago, # ^ |   0 Hey thanks for your reply! and yes I do understand how the second solution works.
 » 3 weeks ago, # | ← Rev. 2 →   0 hey in the ques of binary literature, I just compared three-string taking two at once compared all the characters and when the char were the same pushed one in another string and if they were different pushed both of them into the string. I got three strings in the end and I printed the one with the least length. But it failed testcase2 part 30 can anyone explain to me why?https://codeforces.com/contest/1509/submission/113708604
 » 3 weeks ago, # |   0 Has anyone managed to solve the c problem with Python? I get always TLE on test 13: https://codeforces.com/contest/1509/submission/113919293
 » 3 weeks ago, # |   0 For div1 F,in Tutorial:However, there will be cases when u→ur is actually not needed (I will call this phenomenon being hidden). What is the condition for u→ur to be hidden? That's when there's a path from u to ur with length ≥2! Suppose this path is in the format u→⋯→t→ur. We can prove that tu then that implies at>au but at
•  » » 3 weeks ago, # ^ |   0 You are not wrong, it's just that we have proven that if there is such t then it cannot lie between u and ur, so we only need to query in [l, u].
•  » » » 3 weeks ago, # ^ |   +18 I don't think you have answered my question correctly.The defination of $l$ should be different.In my opinion,$l=\min_{i,[u,ur]\in[L_i,R_i]} L_i$In the tutorial,$l=\min_{i,u\in[L_i,R_i]} L_i$
•  » » » » 3 weeks ago, # ^ | ← Rev. 2 →   0 Ah yeah I see that's true, typo on my part. I will update the editorial.Edit: similar typo on second part of editorial as well :(
 » 3 weeks ago, # |   0 Can't think of the problematic case in div1D, can anyone provide an explicit example?
 » 12 days ago, # | ← Rev. 2 →   0 It is really a ****ing enjoyment to read your code for F(div2)...orz %%%%
 » 10 days ago, # | ← Rev. 3 →   0 if the pivot point we chose is on the convex hull of the set of points, then one of the border segments may actually intersect some other segments! Hi, I have tried to solve problem D and pick the smallest y, smallest x point which should definitely be on the convex hull. However, it still passed the tests without encountering the case above.Are the tests simply weak or is that case really impossible to encounter? Actually, I also cannot think of how that case should happen there should be no segment that can intersect with a border segment. Is my understanding wrong?
•  » » 10 days ago, # ^ |   0 Right below that, listed as one of the workarounds: "Always choose the bottom-most and left-most point as a pivot. This makes this potentially troublesome segment predictable, and also allows us to slightly simplify the angular sorting code." That's exactly what you did.A segment can intersect with a border segment if, for example, you have a square ABCD, you choose A as pivot and your sort by angle puts B and D next to each other(for example, by sorting like C(0°),B(45°),D(315°)).