### Aris's blog

By Aris, history, 8 months ago, 1690A - Print a Pedestal (Codeforces logo?)

Idea: MikeMirzayanov

Tutorial
Solution

1690B - Array Decrements

Idea: MikeMirzayanov

Tutorial
Solution

1690C - Restoring the Duration of Tasks

Idea: MikeMirzayanov

Tutorial
Solution

1690D - Black and White Stripe

Idea: MikeMirzayanov

Tutorial
Solution

1690E - Price Maximization

Tutorial
Solution

1690F - Shifting String

Idea: MikeMirzayanov

Tutorial
Solution

1690G - Count the Trains

Idea: Aris

Tutorial
Solution Tutorial of Codeforces Round #797 (Div. 3) Comments (68)
 » Auto comment: topic has been updated by Aris (previous revision, new revision, compare).
•  » » No-one else understands your python code except you!!!
 » speedforces
 » Wasn't it round 797?
 » 8 months ago, # | ← Rev. 2 →   For users (like me) trying to learn implementation, I suggest looking at jiangly's submissions of these problems.
 » 8 months ago, # | ← Rev. 2 →   For G, can someone please give a proof to "remove in total no more than 2*n elements from the set".
•  » » The set contains the starts of trains. Initially there could be at most n trains. When the speed of a train is reduced, you can at most create one new train. This adds m additional trains. It is possible that every element could be removed from the set, so in total, n+m elements can be removed from the set.
•  » » » is it possible that some subset of trains are getting removed and added? I mean after we remove a starting train from the set, it can be added as well afterward. doesn't this yield to an n^2 solution?
•  » » » » Not sure what you mean. Each operation can only create one new train. The operation can only create a new train with start index at the index of the operation. Indices before the operation will not be affected and indices after will either match the new value at the index of operation (and thus will be within the same train) or will not be affected.
 » If you are/were getting a WA/RE verdict on problems from this contest, you can get the smallest possible counter example for your submission on cfstress.com. To do that, click on the relevant problem's link below, add your submission ID, and edit the table (or edit compressed parameters) to increase/decrease the constraints. If you are not able to find a counter example even after changing the parameters, reply to this thread (only till the next 7 days), with links to your submission and ticket(s).
 » Hi, for problem $A$, since $n \leq 10^5$ we can use brute force. But I want to know $\mathcal{O}(1)$ solution. I saw codes from top people from standings. But I don't understand the idea
•  » » We want to have the minimal value of h1.We know that h1 + h2 + h3 = n, so we should maximize the values of h2 and h3 in order to minimize the value of h1.The "best case" scenario would be that n mod 3 = 0, so that we can set: h3 = n/3 — 1, h2 = n/3 = h3 + 1, h1 = n/3 + 1 = h/2 + 1. (because n / 3 is a whole number)This is the best case because h1 > h2 > h3 is required by the problem statement.If n mod 3 = 1, then (n — 1) mod 3 = 0. We can perform the above solution for n — 1 and get: h1 = (n-1) / 3 + 1, h2 = (n-1) / 3, h3 = (n-1) / 3 — 1. We have one extra block though, and the only way to place it without disrupting the inequality (h1 > h2 > h3) is to place it at the top of h1, making h1 = (n-1) / 3 + 1.If n mod 3 = 2, we can follow the above process once again, this time placing our second extra block at the top of h2 making h2 = (n-2) / 3 + 1. Since h1 = (n-2) /3 + 2, the inequality is not disrupted and h1 stays minimal.
•  » » » Thank you very much for detailed explanation. I will try to understand the concept. :)
•  » » » Great explanation thanks!!
•  » » Without any ifs greedy solution:n — input; h1, h2, h3 — pedestals;We now that that h2 >= h3 + 1 and h1 >= h2 + 1 >= h3 + 2 => n = h1 + h2 + h3 >= 3 * h3 + 3 => max h3 equals h3=(n-3)/3 (rounding down).Than just set s = n - h3Using the same logic s = h1 + h2 >= 2 * h2 + 1 => max h2 equals h2 = (s - 1) / 2 (rounding down again). And h1 = s - h2
•  » » » Thanks :)
•  » » » WOW! Thanks a lot
•  » » in contest submission: 159731522
•  » » Check out my submission. Let me know if you have any questions!
 » Auto comment: topic has been updated by Aris (previous revision, new revision, compare).
 » Can anyone share their approach for problem F? Why are we always considering the minimum shift? As we are taking LCM, it might be better to take a larger number if the LCM with ans is getting minimized.
•  » » 8 months ago, # ^ | ← Rev. 4 →   deleted.
•  » » » consider this string abcabcabc there is a period of length 3 so shifts are [3,6,9] notice that all shifts are multiple of period 3 so that minimal number is the best as itcontains the minimal number of prime factors.
•  » » » » Understood! Thanks!
•  » » » » 4 months ago, # ^ | ← Rev. 2 →   but what if shifts like 2,5 are possible?upd: got it sorry.if a,b is possible then a-b is also possible.
 » proof for E ?
•  » » This is not like a formal proof but should make the idea clear as it is basic mathematics. Let C1, C2, C3,....Cm be the length of some m number of disjoint cycles present in the string, (m <= n). Pick any Ci. the length of cycle Ci means that if you do Ci number of operations as mentioned in the question, you will get the starting permutation of those indices and hence the string will be the same.Say X = LCM(C1, C2, C3, ..... Cm). Since we want the entire string to be as starting string after operations, all individual cycles must come back to the original configuration in starting string indices. And since lowest common multiple of cycle lengths is the least number when all lengths complete full rotation, we can guarantee that X is definitely the answer. But is X the best possible answer always ? It might not be. Let me give an example. Consider the string "aaa". You can get "aaa" as same definitely after 3 rotations. but are 3 rotations necessary, No. You can also get the same after 1 rotation only because the possibility of characters might match.So instead of taking the cycle length 3 as possible rotations, you take 1. So formally answer becomes:X = LCM(Y1, Y2, Y3,......Ym), where Yi (<= Ci) is the minimum number of shifts we need to perform for i-th cycle to make the string equivalent to a complete rotation.Here is my C++ implementation after reading from editorial.
•  » » » Thanks bro, but i asked for E :D however i proofed my way
•  » » » » Could you please explain what proof you got?vukvuk
 » In problem F, the rotation of string will cost o(n^2) time. it's ok for this problem. But I want to know is there any o(n) approach to do this work?
•  » » You can use KMP algorithm for $O(n)$ search, or hashing will also give you average of $O(n)$ time.
•  » » Damn, I didn't even see that the constraints were so less up until I saw your comment. My approach was using a rolling string hash, which works in O(n) time and O(n) space.
 » Getting a TLE on 147, Can I get that testcase ?My solution appears to be O(m log n)
•  » » 8 months ago, # ^ | ← Rev. 2 →   try to use ap.lower_bound(v) instead of lower_bound(all(ap),v)
•  » » » Wow! Worked, didn't know that both of them have this much difference in running time.Thanks!
•  » » » » look at the section "Complexity" in https://en.cppreference.com/w/cpp/algorithm/lower_bound
•  » » » » That's because we can only visit a element in set in O(n), So using lower_bound(all(ap),v) will cost you O(nlogn) time.
 » In problem F, I have another approach, using the Chinese Remainder Theorem.My approach: Apply one operation to avoid result 0 Use DFS to find the cycles Apply operations individually in each cycle, find the "first" and "second" time when it is same as the input after applying the shifts. Create the equations like "x % (second — first) = first" Apply the Chinese Remainder Theorem and add one to the answer (because of the first step of my approach) I hope this could be useful for someone.
 » Is there any DP idea for problem D, as at every W we have a choice to color it or not. If we color it then it contributes 1 to our cost and increases the max length of consecutive B so far by 1, if we choose not to color it, then from here max length becomes 0. Initially I thought of this approach but couldn't code it out. Am i thinking right/wrong in this direction ?
•  » » The problem can be solved with two pointers. If you want to solve it by DP, you surely can, but that code will also boil down to a two-pointer code essentially. Since we want to maintain k consecutive Blacks, we do not have a choice for W to add it or not. If it happens to come inside our string of length G then we have to consider it otherwise not.
•  » » » 8 months ago, # ^ | ← Rev. 2 →   Can you give me a solution/submission link of problem D using two pointers? I have upsolved D using prefix sum.
•  » » I think you are thinking about doing it using a sliding window. I recommend checking my last submission to see if it is the same idea. If not, you can try out your idea and see where you get stuck (why you can’t code it).
 » 8 months ago, # | ← Rev. 2 →   can someone tell me where this is failing? 160027351
•  » » 8 months ago, # ^ | ← Rev. 2 →   Take the string $abab$ for example, it only need $2$ operations to repeat itself, but your code will calculate it as $4$
 » Is time complexity $m \log^2n$ per test case not allowed in G? I'm getting TLE for this submission 160038706. I've used segment tree with lazy propagation, and I've also used binary search. I'm making a range query for every iteration in the binary search, that's why its $m \log^2n$.
•  » » Don't quite know why you are getting TLE, but I used binary search + segment tree with lazy propagation and it passed. Here's my submission : https://codeforces.com/contest/1690/submission/159839709
•  » » » That's a nice method, initially, I had written the exact same segment tree implementation as you (the cp-algorithms one lol), but I wasn't able to figure out the distinct numbers part, so I had to modify the type of queries I was making. Complexity wise our codes seem to be the same, maybe yours has a lower constant factor? If someone can pinpoint the reason for me getting TLE that would be really helpful.
 » 8 months ago, # | ← Rev. 3 →   deleted
 » In F why does having the same character at multiple positions doesn't come into consideration? I.E. Couldn't there be a string such that the resultant string is the same as the original but indices are not the same since the same characters are occupying each other's place?Example:Original string and indices:A B A B 0 1 2 3 // indicesAfter some permutations:A B A B 2 1 0 3 // original indices of chars
•  » » That's exactly why we need to find $k_j$.
 » 8 months ago, # | ← Rev. 2 →   Problem E: Why does this TLE? For each test-case, I think only O(k) work is being done (both i and j only go once from k-1 to k/2, and 1 to O(k), respectively). Edit: Turns out that map (O(log k)) gives TLE, but array (O(1)) does not (?).
 » Can G be done by segment trees?
•  » » yup,you can get the prefix minimum while updating using a segtree. also you can use fenwick
 » 8 months ago, # | ← Rev. 2 →   i tried segment tree approach for G but i got TLE — Test 134 https://codeforces.com/contest/1690/submission/160192601 is it possible to optimize this solution ? my idea is to merge elements from right array that are < min(left) to left arrayexample: left: 10 5 right: 8 7 4 2 merge result: 10 5 4 2
 » PLZ CAN ANYONE TELL WHAT I AM DOING WRONG WITH THE CODE
•  » » CF Stress returns that verdict because your code doesn't compile on my machine, due to this line: min(mini, (long long)times / s.size()); It complains that that the second argument is long long unsigned. Explicitly typecasting this makes it compile, resulting in a counter example: Ticket 15364
 » 7 months ago, # | ← Rev. 4 →   For G, there are two unnecessary things in the following code segment- if (it != tmp.begin()) { it = prev(it); if (*it == j || a[*it] > a[j]) { tmp.insert(j); } } else { tmp.insert(j); } 1) the else segment is not required as the first train will always remain in the set and so upper_bound of any index can never point to tmp.begin(), so the code will never enter the else section!2) in the nested if condition, there is no need to check if *it==j, as if this is true then we already know that jth index is there in the set so we don't need to confirm this condition in order to add jth index in the set.
 » 6 months ago, # | ← Rev. 3 →   more optimized solution for problem E #include using namespace std; #define ll long long #define pb push_back #define ppb pop_back #define mii map #define msi map #define mci map #define mis map #define vi vector #define ii pair #define vii vector #define testcase int t; cin >> t; #define gcd( a, b ) __gcd(a, b) #define lcm( a, b ) ((a)*((b)/gcd(a,b))) int main(){ ll t; cin >>t; while(t--){ ll n,k; cin >> n>>k; ll a[n]; ll ans = 0; ll freq[k] = {0}; for(ll i =0;i> a[i]; freq[a[i]%k] += 1; ans += a[i]/k; } for(ll i = k-1;i >= k/2;i--){ ll j = k-i; while(freq[i] >0 && j<=i){ if(freq[j] > 0){ if(i == j){ if(i+i freq[j]){ ans += freq[j]; freq[i] -= freq[j]; freq[j] = 0; } } } j++; } } cout <