### flamestorm's blog

By flamestorm, 22 months ago,

Thanks for participating!

1692A - Marathon

Idea: mesanu

Tutorial
Solution

1692B - All Distinct

Idea: mesanu

Tutorial
Solution

1692C - Where's the Bishop?

Idea: flamestorm

Tutorial
Solution

1692D - The Clock

Idea: SlavicG

Tutorial
Solution

1692E - Binary Deque

Idea: flamestorm

Tutorial
Solution

1692F - 3SUM

Idea: flamestorm

Tutorial
Solution

1692G - 2^Sort

Idea: flamestorm

Tutorial
Solution

1692H - Gambling

Idea: mesanu

Tutorial
Solution
• +48

| Write comment?
 » 22 months ago, # |   +6 Thanks for the fast editorial
 » 22 months ago, # |   0 I loved the contest, especially problems F and G.
 » 22 months ago, # | ← Rev. 2 →   +3 whats wrong with this https://codeforces.com/contest/1692/submission/160657931 P.S. found.
 » 22 months ago, # |   0 How to solve H without using trees?
•  » » 22 months ago, # ^ | ← Rev. 3 →   +7 For example using a Kadane's algorithm. My submission that using it is here.
•  » » » 22 months ago, # ^ |   +1 But if we apply Kadane's algorithm for every possible value of a wouldn't the complexity will be O(n^2)?
•  » » » » 22 months ago, # ^ | ← Rev. 2 →   +1 for each a, store positions of a[i] = a then the complexity is O(nlogn) because use have to compress value of a[i] or store them in map
•  » » 22 months ago, # ^ |   +6 I used Kadane's algorithm, where I kept track of the indices of each number, and inserted a certain amount of negative 1's between each index.
•  » » 22 months ago, # ^ |   +5 As explained in the editorial, for a fixed $a$, you can replace $arr[i]=1$ if $arr[i]==a$ else $arr[i]=-1$.Now, you can find max sum subarray in this newly obtained array without using Segment Tree.Observe that the max sum subarray will always start and end at indices where $arr[i]==1$.Using this observation you can just iterate over indices where $arr[i]==1$ and use Kadane's Algorithm to find max sum subarray.Since, there are only $n$ distinct values of $a$ possible, overall time complexity will be $O(n)$.For more clarification, you can see my submission
•  » » » 22 months ago, # ^ |   0 I don't understand how you found the constant a in this case
•  » » » » 22 months ago, # ^ |   +1 No, I am not saying $a$ is constant but $a$ will definitely appear in the given array. So we can just iterate over all the distinct elements of the array and then considering current element as $a$ calculate the answer. Maximum over all elements will be the final answer.
•  » » » » » 22 months ago, # ^ |   0 but wouldn't that be O(n^2) because for every distinct a[i] you need to check the maximum subarray?
•  » » » » » » 22 months ago, # ^ |   0 For every distinct $a_i$ we will iterate over indices of its occurrence. Now sum of number of indices for all distinct $a_i$ will be $n$(obviously). So inner loop will run only n times for the whole array.Time complexity: $O(n+n)=O(n)$
•  » » » » » » » 22 months ago, # ^ |   0 again, there can be n distinct a[i] numbers, and if you iterate each one that would be n*n?
•  » » » » » » » » 22 months ago, # ^ |   0 Think about it. If there are n distinct numbers then number of occurrences of every number will be 1. So inner loop will run only 1 time for every element not n times.
•  » » » » » » » » » 22 months ago, # ^ |   +1 Great explanation , thanks!
•  » » » 21 month(s) ago, # ^ |   0 Correct me if I'm wrong, but creating the map $m$ will require $\mathcal O(n\log n)$ time.
•  » » » » 21 month(s) ago, # ^ |   +1 Yeah, you are right. I always tend to ignore map's complexity, lol. You can also use unordered_map for better complexity but that isn't necessary.
•  » » 22 months ago, # ^ |   0 A solution using set Code
•  » » 22 months ago, # ^ |   0 A solution using map here 160632225
•  » » 22 months ago, # ^ |   0 Then it's just kandene my dear friend.
•  » » 22 months ago, # ^ | ← Rev. 2 →   0 I used divide and conquer, here is my code
•  » » 22 months ago, # ^ |   0 I used DP
•  » » 22 months ago, # ^ |   0 I think this is easy solution using map 160604275
 » 22 months ago, # |   +8 **problem H **, also can be solved useing kadane's algorithm.
 » 22 months ago, # |   +8 H can be solved in O(n) just iterating for indexes of each a that is in array. My solution is here
•  » » 22 months ago, # ^ |   +8 You're inserting into a map, so it's not $O(n)$
•  » » » 22 months ago, # ^ |   +1 We can utilize the map in a way that it never holds more than two elements, and hence works in O(1), which makes the solution O(n).see 160608733
•  » » » » 22 months ago, # ^ |   0 I think for an input like [1,1,1,1,2,2,3,...] the map would have to hold 3 elements. So in the worst case the map holds log(n) elements.
•  » » » » » 22 months ago, # ^ | ← Rev. 2 →   0 You are right, so it is O(n log(log(n)))
•  » » 22 months ago, # ^ | ← Rev. 2 →   +3 Bro come Atleast do some research before claiming time complexity. It's not O(N) coz you are using map. But i guess the operations are quite low so we can it's O(N) but technically it's not.
•  » » 22 months ago, # ^ |   0 I tried running this with unordered_map but its fails with timeout error. Why is this the case? Why would iterating over an unordered_map take so much longer than iterating with map?
•  » » » 22 months ago, # ^ |   0 Because unordered_map has an O(n) complexy for operations in a worst case.
•  » » » » 22 months ago, # ^ |   0 Cool thanks. Where in the documentation can I find this just for future reference?
•  » » » » » 22 months ago, # ^ |   0 Read this
 » 22 months ago, # |   +4 Problem H can use Kadane in a nice way.Notice that the sum over frequencies of distinct elements is the size of our input array.We know any subarray endings should be at two elements that have the same value, otherwise, we are pointlessly reducing our answer and we can fix our boundaries to be on the same boundaries.say if we have XXXLXXXRXXX where [L, R] is the subarray we are considering then there is no point in having the left or right boundaries on Xs since we could improve the sum by restoring boundaries to L and R.We can make an array of positions for each distinct element and use the observation that the Xs can be compressed together, therefore for each distinct element, such a compression yields a size of array twice the array of all positions for that element.So our solution boils down to making a compressed array for each distinct element and then applying kadane to it, finally taking the one which gives the maximum answer.Time complexity is still the order of n as we are effectively applying kadane on twice the input size.
 » 22 months ago, # | ← Rev. 2 →   0 Problem F : O(10^3) preprocessing + O(n) approach Submission
•  » » 22 months ago, # ^ | ← Rev. 3 →   0 easier O(10³) + O(n): 160566183
 » 22 months ago, # |   0 https://codeforces.com/contest/1692/submission/160650500Here , is my submission for problem G, i didn't get how I am getting wrong answer on testcase 15, can someone please explain fault in my submission?. Thank you.
•  » » 22 months ago, # ^ |   0 probably a floating precision error cause you are using v[i]=log2f(x)
•  » » » 22 months ago, # ^ |   0 is there anyway we can store log base 2 of any number upto say 20 order of precision?
•  » » » » 22 months ago, # ^ |   0 Depends on what you mean by "20 order of precision". If you mean just 20 digits in the number then surely double-precision floating-point can hold that amount of digits. If you mean 20 digits in fractional part, then you can't have a guarantee since it depends on how big your number is
•  » » » » 22 months ago, # ^ | ← Rev. 2 →   0 Btw, you can use 63 - __buitin__clzll(x) instead of log2f(x) if you're using gcc/g++. Or you can use something like that (code below), but I'm not sure if it'll resolve your problem codeint my_log2(long long n) { int z = 1; int cnt = -1; while (z <= n) { z *= 2; ++cnt; } return cnt; } 
 » 22 months ago, # | ← Rev. 2 →   +22 Solution of H without treesInstead of doubling and halving, consider the score of a subarray to be the difference between the frequency of $a$ and the number of elements in the subarray not equal to $a$. This is done because we will double our score for $freq[a]$ times and halve it for the rest elements and hence we want this difference to be maximum. Notice that for the best subarray, $a = x_l = x_r$. The problem can now be solved with dp. Let $dp[i]$ be the best score for a subarray with $l = i$ and $a = x_i$. Then we can see that $dp[i] = 1 + max(0,dp[next[a[i]]] - (next[a[i]]-i-1))$, where $next[x]$ is used to store the next occurence of $x$ on iterating over the dp. The right end can be maintained similarly(if the dp is maximised then $right[i] = right[next[a[i]]]$.Submission at the time of the contest — https://codeforces.com/contest/1692/submission/160583853
 » 22 months ago, # |   0 Can we use sliding window for E by searching total sum minus reqd target in the sliding window?
•  » » 22 months ago, # ^ |   0 This is my code to use a sliding window Your text to link here...
•  » » » 22 months ago, # ^ |   0 O(N)
 » 22 months ago, # |   0 Solution of H without using segment trees: https://codeforces.com/contest/1692/submission/160692781
 » 22 months ago, # |   0 Could anyone help me with problem E: Binary Deque? My solution is kind of greedy. I create an array with distances (in numbers of zero entries) between two conseсutive ones. For example, for the sequence: 0 0 1 0 0 0 1 1 0 1, my array is [2 3 0 1 0] (last zero means that we have tail of zeros with length 0). Next, I use two iterators for decreasing the sum of the array choosing at every step iether the head or the tail with zeros and errasing it from my array. I cannot find the mistake and 1st test is OK, but I get WA for the second. If this idea is correct then the solution is O(n). Here the link: 160692840
•  » » 22 months ago, # ^ |   0 I have a solution of E: You can define an array $k$, It is prefix sum of $a$, so $k_i$ is "How many ones in [1,i]". The range [l,r]'s sum is S when $k_r-k_{l-1}=S$, you can define an array $m$. $m_i$ is "The max index j that k[j]=i". Then enumerate $l$, and the max of $r$ is $m_{s+k[l-1]}$, the answer is the minimum of $n-(r-l+1)$. If every $r$ is $0$ ($s+k[l-1]>n$), Then output -1. My code: 160569001
 » 22 months ago, # |   0 Can anyone explain the Kadane algorithm approach for the H problem? I am unable to understand what one is exactly trying to do.
•  » » 22 months ago, # ^ |   0 I've done something very similar to kadanes (with an early break when the sum reaches 0):For each position (starting from left) iterate through the array adding +1 for matches and -1 for other values. If the sum gets to 0 break. Any start positions that have previously added a +1 are skipped (as you've already calculated the sum from that position starting from at least 1).
•  » » » 22 months ago, # ^ |   0 ohk thanks:)
 » 22 months ago, # |   0 Thank you for fast editorial and interesting tasks.
 » 22 months ago, # |   0 Pretty sure we can do a O(n) solution for E right? I did this. Doesn't use map stl. https://codeforces.com/contest/1692/submission/160607428
•  » » 22 months ago, # ^ |   0 I did an O(n) with 2-pointers technique https://codeforces.com/contest/1692/submission/160604366
•  » » » 22 months ago, # ^ |   0 can you explain the logic behind this? I also tried a 2-pointers technique but I don't think my solution works. Also, how is your solution O(n) if it has nested loops?
•  » » » » 22 months ago, # ^ | ← Rev. 3 →   0 Ok I'll try.We're moving right pointer while sum in segment [l, r] is less/equal to s. If our sum is larger than s, we're moving the left pointer to reduce it. When sum is equal to s, update the answer. Thus, we'll find all subsegments with sum equal to s.Complexity is O(n) because we're moving the right pointer N times and left pointer no more than N times.Actually, my code is almost a copypaste of edus one (first block), there's a detailed explanation as well.
•  » » » 9 months ago, # ^ |   0 I also done in O(n) 214739145
 » 22 months ago, # |   0 anyone please tell me where am i wrong 160716728 problem D
•  » » 22 months ago, # ^ |   +16 I think line 74 should be "checkhour>=24" instead of "checkhour>24"Also line 41~44 is not correct because the clock never shows 24:00.
 » 22 months ago, # | ← Rev. 2 →   +24 I'm not too fond of the author's solution to problem H, though the problem was great. But no divisional 4 rounds should contain any Data Structure. Here's my solution — The basic observation is that the problem simply asks us to find, for each element(x) of the given array, the maximum subarray sum on another array(b) where b[i] = 1 if a[i] == x, -1 otherwise.We can apply Kadane's algorithm for each element lazily. In Kadane's algorithm, the important variables are current sum, current best, and where the best subarray is. So, as we iterate over the array, we evaluate elements one by one and remember these important variables for each element and their last positions, std::map works fine.And finally, we can just iterate over the map to find the best answer and the desired segment with the same asymptotic, O(n.log(n)). See my implementation if need be.
 » 22 months ago, # |   +1 What is the wrong on my solution Problem E ? im using two pointers to get the best distance from left and right.. 160727889
•  » » 22 months ago, # ^ |   0 1 12 1 0 0 1 0 0 1 1 1 1 0 0 0 You answer is 8 , but the correct answer should be 7. It is easy to know that this greed will not work...
 » 22 months ago, # |   0 160731775 : An Elegant solution for Problem H:no segment tree required
 » 22 months ago, # |   0 Where can I find a tutorial for the standard problem of finding the maximum sum of a subarray?
 » 22 months ago, # |   0 In D, why is the second for loop running for 2022 iterations?
•  » » 22 months ago, # ^ | ← Rev. 2 →   0 Any number greater than 1440 because after that the numbers will repeat again.
 » 22 months ago, # | ← Rev. 3 →   +16 An alternative solution to problem H without segment tree. Notice that if we choose $(a,l,r)$, the money we will get is $2^{x-(r-l+1-x)}\ =2^{2x-(r-l)+1}\$ ($x$ is the number of times that $a$ appears in round $l$ to round $r$). Thus our goal is maximizing $2x-(r-l)+1$. For a certain number $a'$, we can create an array $b$, where $b[i]=-i+$ $2\times$(the number of times that $a'$ appears in round $1$ to round $i$). And the money we will get by choosing $(a',l,r)$ is equal to $2^{2x-(r-l)+1}=2^{b[r]-b[l]+1}\$! On the other hand, it's obviously that if the answer is $(a,l,r)$, then the dice must show $a$ in both round $l$ and round $r$. Therefore, we only need to consider the rounds where dice show $a'$ for a certain number $a'$. Based on the above, the problem can now be solved with time complexity $\mathcal{O}(nlogn)$ by using maps and arrays. Checks this code for better comprehension:https://codeforces.com/contest/1692/submission/160769341
 » 22 months ago, # |   -9 when div5 begins to be held? I want a more easier division!
•  » » 22 months ago, # ^ |   0 kidding ?
•  » » » 22 months ago, # ^ |   -8 not kidding, i want a more happy contest, during which I can ak very fast
 » 22 months ago, # |   0 mesanu For Problem H, can you explain how you are updating and calculating the maximum sum subarray having elements -1, 1, and like how you are updating it in log(N). Like in your code what does pref, suf, Val, and sum store, and how your modify function is working as I'm having trouble with these parts. Thank you.
 » 22 months ago, # |   0 Here's another approach to problem H which is very surprising for me. We can just make store all positions of each distinct value. Then apply the Kadane's algorithm to find the leftmost and the rightmost of each local maximum power of 2. Lastly, just find the maximum of them. Here's the code to what I'm saying: https://codeforces.com/contest/1692/submission/161122830
 » 22 months ago, # | ← Rev. 2 →   0 [Deleted]
 » 22 months ago, # |   0 Greedy Approach for last question : Gambling ->Suppose we have this example : 10 8 8 8 9 9 6 6 9 6 6 First make map< long long , vector> and insert key : [with vector of indexes] 8 : [0,1,2] 9 : [3,4,7] 6 : [5,6,8,9] then iterate over vector of indexes of each element Initialize res = 1 ( represent power of 2. Initially 1 because for one occurrence we have 1*2) for 8 : res = 1 ( index 0 ) start iterating from : index 1 if gap>0 subtract gap ( res -= gap (here gap is 0) ) and add +1 ( for current occurrence ) index 2 : gap is zero so just add +1 we get res = 3 this means final money is pow(2,3) = 8 if at any moment res become <= 0 then set res =1 again (as this is minimum value possible) Do this for all and record max res CODE `#include using namespace std; using namespace chrono; #define ll long long #define pb push_back #define For(i,n) for(int i=0; i>n; vectorv(n); For(i,n)cin>>v[i]; mapm; For(i,n){ m[ v[i] ].pb(i); } ll a = v[0] , l = 1, r = 1; double sum = 1; for(auto itr : m){ vli temp = itr.second; ll siz = temp.size(); double res = 1; ll start = temp[0]; Fora(i,1,siz){ ll gap = (temp[i]-(temp[i-1]))-1 ; res -= gap; res ++ ; if(res>sum){ sum = res; a = itr.first; l = start+1; r = temp[i]+1; } if(res<1){ start = temp[i]; res = 1; } } } cout<>t; while(t--){ solve(); cout<
 » 22 months ago, # | ← Rev. 3 →   0 hi
 » 22 months ago, # |   0 Another div4 is coming soon
 » 21 month(s) ago, # |   0 Will participate in next div4
 » 10 months ago, # |   0 In problem E for all those implementing binary search , try to use lower bound instead . It will reduce the lines of code .
 » 4 months ago, # |   0 In problem E editorial there is a mistakehere we have to choose largest value of r , not smallest.
 » 2 weeks ago, # |   0 What was the logic of 1st Question?