### Gol_D's blog

By Gol_D, history, 5 months ago,

1619A - Square String?

Idea: MikeMirzayanov

Tutorial
Solution

1619B - Squares and Cubes

Idea: MikeMirzayanov

Tutorial
Solution

Idea: MikeMirzayanov

Tutorial
Solution

1619D - New Year's Problem

Tutorial
Solution

1619E - MEX and Increments

Idea: SixtyWithoutExam

Tutorial
Solution

1619F - Let's Play the Hat?

Idea: MikeMirzayanov

Tutorial
Solution

1619G - Unusual Minesweeper

Idea: Gol_D

Tutorial
Solution

1619H - Permutation and Queries

Idea: Brovko

Tutorial
Solution

• +73

 » 5 months ago, # |   +13 Really liked Problem D. The observation about how pigeonhole theorem can be used in the check function of binary search was very good.
•  » » 5 months ago, # ^ |   +2 You guys used binary search ? Wow
•  » » » 5 months ago, # ^ |   0 How have you solved it? Would you like to share your approach?
•  » » » » 5 months ago, # ^ |   0 A = maximum of Top-2 by shops B = minimum of Top-1 by friends Result is min(A, B) https://codeforces.com/contest/1619/submission/140109705
•  » » » » » 5 months ago, # ^ | ← Rev. 3 →   0 podtelkin could you please explain why this idea worked? UPD: Got it. Nice idea
•  » » » » » » 5 months ago, # ^ |   +7 你可以在这里看到中文版本(Link)I think I can explain it.(Please forgive my poor English.)my code is hereAccording to the description of the problem, you can easily get the follow inequality: $ans \leq \text{minimum of Top-1 by friends}$I use a[i][j] to describe the j's contribution to friends numbered i.Then let's suppose $n-1 \leq m$.Now I give a strategy for choosing gifts: Choose a friend numbered I, and find his favorite gift J. Let's suppose a[I][J] is also the maximum among a[1][J],a[2][J],...,a[n][J].(That means J's contribution is also the biggest if it's for I.)Let me call this Hypothesis A. Then find the Top-2 among a[1][J],a[2][J],...,a[n][J], and pick the corresponding I'. Now both I and I' will be given the gift J. Joy Point of them is top1ByFriends[I] and top2ByShops[J]. For the rest n-2 friends, we can always pick their favorite gifts. For those friends, their Joy Point is top1ByFriends[i]. So if we try to choose everyone as the lucky I, we will have several $\alpha$ that satisfy the following condition: $max\{\alpha\}\leq \text{maximum of Top-2 by shops}$ After having this consequence, let's think the situation that Hypothesis A doesn't hold. It's quite easy to come to the conclusion that if top1ByFriends[I] < top2ByShops[J], the $\alpha$ will be determined by top1ByFriends[I] so any I' who has a[I'][J] bigger than a[I][J] is OK, because he won't influence the result in this situation. So now we can say: $ans \leq \text{min{minTop1ByFriends[i],maxTop2By Shops[j]}}$You can prove the result is exactly $\text{min{minTop1ByFriends[i],maxTop2By Shops[j]}}$ by category discussion.
•  » » » » » » » 5 months ago, # ^ |   +1 Supplement:If $n-1$ > $m$, everyone can get their favorite gifts.So ans is minTop1ByFriends[i].And you can find that at least two friends will have their favorite gifts in the same shop, so the maxTop2ByShops[j] won't less than minTop1ByFriends[i].
•  » » » » » » » 5 months ago, # ^ |   0 Thank you very much man
•  » » » » » » » 4 months ago, # ^ |   0 Thank you very much :)
•  » » » » » 5 months ago, # ^ |   0 I think one simple way of explaining this approach is:1) we have to pick at least two gifts from the same store (by the pidgeonhole principle).2) we can always choose the store that has the highest second max, and pick the max and second max gifts from this store and give them for the respective friends (lets call this decision opt*), and I argue this choice can always be made and still achieve an optimal solution: if our solution has 2 other items from any same store, then the second highest of these two will be smaller than the second highest of the opt* choice (by definition of highest second max), making it a worse choice. thus, we can always choose opt* and still get a optimal solution.3) now pick the best n-2 for the remaining n-2 friends.
•  » » » » 5 months ago, # ^ | ← Rev. 2 →   +4 What i did in the contest was we know that we can visit n-1 shops ,so the idea is that for every shop we can see the maximum and the 2nd maximum joy values which are attained for any two friends .let say its i and j ,so leaving these two friends apart we can easily take the maximum of joys from any shops for the rest of the friends .i won't suggest you to see my implementation it's really messy and the binary search logic is way more easy to implement and think .my implementation involves map of multiset of pair of int which was the best i could think in the contest
•  » » » » 5 months ago, # ^ | ← Rev. 3 →   +3 well, i have a solution using sort and some magicthis is my solution. I scared to fail the system test, but some how it passed.140057739here are my thoughts while doing this problem btw: if i sort the items by price and when I reach the $i$_th item, if i can delete all the item before $i$ and still albe to take some of the items after, i can just consider $i$ as an answer after that is finding how to check if the remaining item can sastisfy the condition at most n — 1 shop: i'm not so sure about this, but if one item was deleted from all shop you should break the loop, and another case is when the remaining items are the same as the number of remaining shop -> this means you can take two item from any shop so you also need to break loop
•  » » 5 months ago, # ^ |   +2 I also used the pigeonhole principle to solve it but the binary search part is still unclear to me... Like how I can prove that if I can get x units of joy I would be able to get x-1 units of joy? Please care to explain...Thankyou
•  » » » 5 months ago, # ^ |   +3 The binary search function $f(x)$ means I can get $x$ or more units of joy (not necessarily exactly $x$ units)If $f(x)$ is true, then $f(i)$ for $i < x$ is definitely trueIf $f(x)$ is false, then $f(i)$ for $i > x$ is definitely false.So you find the maximum $x$ for which $f(x)$ is true by binary search.
•  » » » » 5 months ago, # ^ |   0 Ohh, thanks a lot... Misunderstood binary search function.
•  » » » 5 months ago, # ^ |   0 Whenever i see maximize the minimum of something my brain just automatically think of binary search. In this problem i struggled with verifying if a particular mid can be achieved.
•  » » 5 months ago, # ^ | ← Rev. 4 →   -7 Solution to problem D without binary search :- Suppose we can visit at most n shops instead of n — 1, since we have n friends, we can select the best shop for each friend and doing so we will be selecting at most n shops. Now, for n — 1 shops, since we have n friends, there must be a shop from which we buy gifts for at least 2 friends. Hence we select the shop with the highest 2nd max. Now we need to select at most n — 2 shops for n — 2 friends. We can simply take the best shop for each remaining friend. Edit: I don't have any proof, but it works
 » 5 months ago, # | ← Rev. 2 →   0 When upsolving G with an unordered_map I got TLE for problem G. When using a map it passes. Took me a long time to realize this was the problem.Gol_D Did you on purpose make a test case that blows up an unordered_map? I doubt this could've happened accidentally. Especially since my code runs quickly on other max cases, it's only Case 14 that is slow.
•  » » 5 months ago, # ^ |   +17 Yes, they do keep such a test case on purpose.
•  » » » 5 months ago, # ^ |   0 I believe it is because someone got hacked using unordered_map during the open hacking phase, then the test case used was included in the main tests
 » 5 months ago, # | ← Rev. 3 →   0 Implementation of C really messed up my contest it was failing on test case 3. Did anyone else face this issue.My Solution Can someone please have a look and point out what I missed.
•  » » 5 months ago, # ^ |   0 It is not an implementation issue, there is a logical error in this part: if (b_last_digit >= 10 or b_last_digit < 0) { b = -1; break; } Please check 140048744 for what should have been in place of the above piece of code.
 » 5 months ago, # |   0 can anyone please explain G in bit more detail ?
•  » » 5 months ago, # ^ |   +17 This is actually a graph problem, let's see it in graph terms.Let each bomb be a node in our graph, there is an edge between two nodes if they share a coordinate and have distance $\leq K$. Now look at its connected components. Clearly if we trigger a bomb, then the entire connected component containing that bomb will explode as well. So a connected component can be triggered by either: we trigger any bomb by ourselves, or we let the earliest bomb explode naturally. Now we have two tasks to solve: Find all the connected components (and the minimum bomb timer in each component), and choose which components to trigger manually (and which will explode naturally).To find connected components: Suppose all bombs are on a straight line, then it's easy. Sort the bombs, add an edge between all pairs of consecutive bombs with distance $\leq K$. For the general problem, use a map> mapx, mapy; or something like that to store bombs on the same x-straight line or y-straight line and solve it as if all bombs are on the straight line. You now have the graph.To choose which components to trigger: Try solving the problem for $K = 0$ (i.e. no bombs trigger one another). Obviously we want to sort the bomb timers, and trigger the highest timers first, until the rest explodes naturally.
•  » » » 5 months ago, # ^ | ← Rev. 2 →   +5 Ahh, sorry I meant H, there are so many problems in contest nowadays, nice explanation, thanks :) I understand sqrt decomp. is applied. I got the idea of query of type 2, but not of type1, why we need inverse indices and that part.
•  » » » » 5 months ago, # ^ |   +3 Think about the permutation as a functional graph where each vertex points to another vertex. In order to answer type 2 queries efficiently, we need a "jump" array which gives the index we will be at if we step forward $Q$ times. When we perform a type 1 query on indices $x$ and $y$, most values do not need to be recomputed--the only indices are $x$ and the $Q-1$ ones that jumped over $x$, so $r_x$, $r_{r_x}$, etc., and similarly for $y$.To recalculate these, start with $x$ and step forward $Q$ times to find $jump_x$. Then step backwards using the reverse permutation to calculate $jump_{r_x}$, $jump_{r_{r_x}}$, etc.
•  » » » » » 5 months ago, # ^ |   0 Thanks !!
•  » » » 5 months ago, # ^ | ← Rev. 2 →   0 140270271 Can you take a look at this submission of G, please? I would appreciate a lot.
 » 5 months ago, # |   0 In problem C's code it should be 18 instead of 19 if(x < y && y >= 10 && y <= 19) b.emplace_back(y — x);
•  » » 5 months ago, # ^ |   0 2 one digit numbers can never sum upto 19. It can be at worse 9+9.
•  » » 5 months ago, # ^ |   0 No in the worst case both a and b contain 9,9. so 9 + 9 = 18
 » 5 months ago, # | ← Rev. 3 →   0 I saw some solutions for problem H that use link cut tree, could anyone help me to understand the idea behind these solutions?
•  » » 4 months ago, # ^ |   0 My understanding is: A permutation can be decomposed into several cycles. If you view each element (1,2,...,n) as a vertex and each (i -> p[i]) as a directed edge, you end up with a directed graph of some simple cycles. A type 1 query "1 x y" is equivalent to cut cycle(s) at the edge (x -> p[x]) and the edge (y -> p[y]), and join the broken cycle(s) with new edges (x -> p[y]) and (y -> p[x]). A type 2 query "2 i k" is equivalent to, (say vertex i is on cycle[i]) find the (k mod length(cycle[i]))-st neighbor of i on cycle[i]. To simulate the above process, all you need is a data structure that represents cycles and supports quick split/merge (for type 1 queries) and find k-th element (for type 2 queries). One obvious choice is the splay tree. Given link-cut tree is mostly based on splay tree, I guess that would work as well.
 » 5 months ago, # |   -7 couldn't get AC on E because of integer overflow. makes me wonder if there are any downsides to always using long long instead of int, I couldn't see any time difference with $2.10^9$ operations
•  » » 5 months ago, # ^ |   0 Maybe it will need more memory
•  » » » 5 months ago, # ^ |   0 yes, it will require double the memory. however, most questions have 256 MB of memory and I don't think I've ever used half of it.Here are the times for $2.10^9$ operationsint : 1107 ms, 1154 mslong long : 1154 ms, 1123 ms
 » 5 months ago, # |   0 In problem E editorial code why sort(a.begin(), a.end()); ? is it a mistake , or something i'm missing ?
•  » » 5 months ago, # ^ |   0 the sorting is not required. you don't even need to store all elements in a[], all you need is the frequency array (cnt[]).
 » 5 months ago, # |   0 Can someone explain this part from Problem G ? Calculating Min From Componentsint ans = res.size() - 1; for (int i = 0; i < res.size(); ++i) { ans = min(ans, max((int)res.size() - i - 2, res[i])); } cout << ans << '\n'; 
•  » » 5 months ago, # ^ |   0 I guess you forgot to sort res vector, I also did the same mistake
•  » » 6 weeks ago, # ^ |   0 If you are considering bombs till component i to be exploded by timeout, then remaining bombs need to be detonated manually. The count of remaining bombs is (res.size()-i-1). Since you can start detonating from time 0, total time taken to manually detonate remaining bombs is ((res.size()-i-1)-1) i.e. (res.size()-i-2). For a particular i, you would be taking max of time taken to manually detonate and time taken to explode by timeout. Because we can be sure that in that time, all bombs are gone. The minimum such time where all bombs are gone would be the answer.
 » 5 months ago, # | ← Rev. 7 →   +26 Solution for problem D without the log factor:For every person, find $B_i$. $B_i$ means the best shop to choose while buying a gift for person $i$. From now on, I'll rephrase "buying a gift for a person $i$ from shop $s$" as "assigning person $i$ to shop $s$.Case 1: Number of different "optimal" shops (i.e. number of distinct integers $B_i$ for $1 \leq i \leq n$) does not exceed $n - 1$. We can assign person $i$ to $B_i$ and calculate the answer directly. Case 2: Otherwise. Then there has to be at least one person $i$ such that it's not assigned to $B_i$. Let's call such a person "unhappy". Then I claim that there is no more than $2$ unhappy people in an optimal assignment. Proof is left as an exercise for the reader. What we can conclude from here is that in an optimal assignment, we'll choose exactly $2$ people and assign both of them to the same shop. Note that none or one of them can be assigned to their best shop. And every person $i$ in remaining $n - 2$ people are assigned to $B_i$. Call the shop with $2$ people assigned as $s$, and friends to be assigned to $s$ as $x$ and $y$. Without loss of generality, assume that $p_{sx} \leq p_{sy}$. What is the answer for a fixed $x$, $y$, $s$? Turns out it's the minimum of $min_{1 \leq i \leq n, i \neq x, i \neq y}(p_{B_ii}), p_{sx}$ and $p_{sy}$.Note that we can assume as if $y$ is also assigned to $B_y$ because we know that $p_{sy}$ can't minimize the answer while $p_{sx}$ is there. Therefore we don't have to iterate over $y$'s and only take the minimum of $min_{1 \leq i \leq n, i \neq x}(p_{B_ii})$ and $p_{sx}$. So let's iterate over different $x$ and $s$. But we have to find a way to ensure that there is at least one $y$ such that $p_{sx} \leq p_{sy}$ holds for a fixed $x$, $s$. Here is a way to check it: We can store for every shop the value of its most valuable gift and its count. Call it $V_s$ and $C_s$ for shop $s$ respectively. If $p_{sx} < V_s$ or $p_{sx} = V_s$ and $C_s \geq 2$ then it is a valid $(x, s)$ pair.Complexity is $\mathcal{O}(nm)$:In case 1, checking is done in $\mathcal{O}(nm)$. In case 2, at every iteration of $x$, you first find $min_{1 \leq i \leq n, i \neq x}(p_{B_ii})$ in $\mathcal{O}(n)$ and iterate shop $s$ in $\mathcal{O}(m)$. Total runtime is $\mathcal{O}(n(n + m))$, which is bounded by $\mathcal{O}(nm)$ because $n \leq m$ must hold at case 2.See example submission: 140114559. Note that I have swapped the dimensions of $p$ in the problem for implementation simplicity. I also have a vector $d$ to count the number of distinct shops for case 1.
•  » » 5 months ago, # ^ |   +4 Brilliant!
•  » » 5 months ago, # ^ |   0 Can you help me rectify error in this comment, i have described a similar idea but i can't make sense of downvotes
 » 5 months ago, # |   0 I can't figure out what's wrong in my solution? It's failing on test 2. Can anyone please help? my code.
•  » » 5 months ago, # ^ |   0 You should consider the situation that (num2
•  » » » 5 months ago, # ^ |   0 Thank you very much dude, I got my mistake.
 » 5 months ago, # |   0 At first, I think the permutation in problem H can be considered with a graph with n nodes and n edges (A Base loop tree)Finally, I realize that it is just a graph with serval loops.What a pity!
 » 5 months ago, # | ← Rev. 2 →   0 In problem B, replacing sqrt(n) with sqrt(n+ 0.5L) give correct answer. Why is this happening? Thanks in advance.Hacked solution : https://codeforces.com/contest/1619/submission/140041728Accepted solution : https://codeforces.com/contest/1619/submission/140180623
•  » » 5 months ago, # ^ | ← Rev. 2 →   0 Because of representation of real numbers in binary. For example, pow(10^9, 1/3) = 999.9999... and not 1000 as it should be. By adding 0.5 you're avoiding this problem.
 » 5 months ago, # |   0 problem F is actually a joke
•  » » 5 months ago, # ^ |   0 Can you please explain the joke part in it?
•  » » » 5 months ago, # ^ |   0 it's easier than D and E. look at this submission
•  » » » » 5 months ago, # ^ |   0 Only after you realize it/read the editorial :)D/E are like just do whatever said, F needs a lil bit of thinking
•  » » » » » 5 months ago, # ^ |   0 No buddy, he is right, problem F is comparable to C, just a little bit of implementation and knowledge of deque(I used it for putting last to front), D involved more than 5x thinking than F... I am more convinced on this fact also due to the line mentioned in the announcement also that problems may not be in their proper order... According to me, the order could be A B C F E D.
•  » » » » » » 5 months ago, # ^ |   0 Whenever you see a problem like "maximize the minimum" most of the time it turns out to be a binary search problem. That's the reason I said D didn't require much thinking for me.
 » 5 months ago, # |   0 Why need to sort the input array of problem E? As we use the frequency array later, any need to sort the input array?
•  » » 5 months ago, # ^ |   0 you are completely right, filling and sorting of vector a are not required
 » 5 months ago, # |   0 Please help me debugging my code for problem G. It is getting WA on test case 2 line 6. 140296294I think I am committing some mistake in calculating ans at the very end of the code.
 » 5 months ago, # |   +2 I solve B using inclusion and exclusion principle. here's my submission:140032787I was worried about the case where I use both sqrt and cbrt but it passed all testcases!After the contest I found that most of the contestants solved this problem using set or any other data structure. However, I find this solution much easier.
•  » » 5 months ago, # ^ |   0 Brilliant idea!
•  » » 5 months ago, # ^ | ← Rev. 3 →   -18 for someone who can't understand this idea... count numbers of the form x^2 such that x^2 <= n (say A2) count numbers of the form x^3 such that x^3 <= n (say A3) numbers of the form x^6 are counted twice so we subtract them once.ans = A2 + A3 — A6
 » 5 months ago, # |   0 Problem C : I'm getting Memory limit exceeded on test 2. I can't rectify my code. Plz help. My Submission
 » 5 months ago, # |   0 No need to sort the array in problem E.
 » 5 months ago, # |   0 Problem E: If anybody is getting TLE then try multiset.upper_bound(num) rather than upper_bound(multiset.begin(), multiset.end(), num). Reason
 » 5 months ago, # |   0 What is the meaning of res.size()-i-2 in the G tutorial?
• »
»
5 months ago, # ^ |
0

-if we want to find the remaining number of elements after index 'i' , to do so we need to make the difference between the size and the index we are at.

-Indexes are from 0 to res.size()-1 , so the actual difference would be res.size() — (i+1) = res.size()-i-1. (Or you can think it as res.size()- 1 — i too)

-So we found out that the numbers of remaining elements after index 'i' is res.size()-i-1

-Assume we have 'x' remaining elements . Since the seconds begin at 0 , the minimum number of seconds we need to erase the remaining 'x' elements is 'x-1'.

-So we found out that the minimum number of seconds we need to erase the remaining elements after current index 'i' is "the number of remaining elements"-1 = (res.size()-i-1)-1 = res.size()-i-2.

-------------------------------------------------------------------------------------------------

## Below is explanation of the approach if needed

Assume you are at index 'i' in the sorted array 'a' in which you stored the Minimum time u have to wait until 1 bomb from the 'i'th component will explode , erasing the whole component with it.

Since the array is sorted , you wonder : What is the minimum time for all the bombs to explode , if you wait until components 1->i explode (without exploding anything by myself) , and you erase all the remaining components in front of the one from index 'i' by yourself.

Assume it takes 30 seconds for the 'i'th component to explode (all the i-1 components will explode too since the array is sorted) , and u have 40 more components after that . U need to wait 30 seconds , but u needed at least 40 seconds to erase all the remaining ones , so u take the maximum from these 2 values. (If we had 20 more components after this , we needed 20 seconds to erase those by ourselves , so waiting 30 seconds for 'i'th one to explode will be enough , so again we had the maximum from those 2).

•  » » » 5 months ago, # ^ |   0 thank you, I understand.
 » 5 months ago, # | ← Rev. 2 →   0 Did anyone solve prolem H with treap?
•  » » 5 months ago, # ^ |   0 Indeed, it's quite reminiscent of this problem from SecondThread's treap educational contest. Just need to figure out the right treap operations to do, and implement a few new ones.
•  » » » 5 months ago, # ^ |   0 Came up with treap solution as my first approach and became unable to think of the $O((N+Q)\sqrt{N})$ one :(
•  » » » 5 months ago, # ^ |   0 Can you guys leave submission link / code?
•  » » » » 5 months ago, # ^ | ← Rev. 3 →   0 140540828(apologies in advance for the rather rambly Rust code lol... Rust doesn't come with a RNG in its standard library, so there is also code for it)Edit: I also recommend this tutorial video by SecondThread to learn about implementing treaps
 » 5 months ago, # | ← Rev. 2 →   0 Can anyone prove or help in proving the greedy approach for problem D? Thank you.UPD: got it.
 » 5 months ago, # |   0 In the question named "Squares and Cubes".....just use unordered_set rather than ordered_set, it will reduce time complexity. :D
 » 5 months ago, # | ← Rev. 2 →   0 Could someone tell me what is wrong with my code? C Problem/*package whatever //do not write package name here */import java.io.*; import java.util.*;public class GFG { public static Scanner scn=new Scanner(System.in); public static void main (String[] args) { int t=scn.nextInt(); while(t-->0) { long a=scn.nextLong(); long s=scn.nextLong();  ArrayList num1=new ArrayList(); ArrayList sum=new ArrayList(); ArrayList ans=new ArrayList(); while(a>0) { num1.add(a%10); a/=10; } while(s>0) { sum.add(s%10); s/=10; } int i=0; int j=0; boolean flag=false; while(i=two) { ans.add(one-two); i++;j++; } else if(i+119) { flag=true; System.out.println(-1); break; } ans.add(one-two); i+=2; j++; } else{ flag=true; System.out.println(-1); break; } } if(!flag) { if(j=0;k--) { res=res*10+ans.get(k); } System.out.println(res); } } } } }
 » 5 months ago, # |   0 I think I have an O(n + qlogn) solution for problem H: A permutation is a composition of circles (i -> p[i] -> ... -> i). We can represent those circles with treaps, one for each circle, when the nodes are i, p[i], p[p[i]], ... from left to right. Note that we can use split and join to make any element the leftmost. first-type queries can be answered — When we swap p[i], p[j] and i,j are in different circles, we merge two circles with O(1) split & join operations to receive: (i) -> (p[j] -> ... -> j) -> (p[i] -> ... i). When we swap P[i], p[j] and i,j are in the same circle, we split two circles with O(1) split & join operations to receive: (p[j] -> ... i), (p[i] -> ... -> p[j]). Second-type queries can be answered easily — we will keep for each subtree it's size.*We will keep an arrays of pointers, Arr[i] is a pointer for the node that represents i.
 » 4 months ago, # |   0 last problem is crazy!!!
 » 4 months ago, # |   0 I implemented E with a std map instead145083862
 » 2 months ago, # |   0 Could anyone help me Problem E? Why my submission is Memory limit exceeded? submission