flamestorm's blog

By flamestorm, 2 years ago,

Thank you for participating in our contest! We hope you enjoyed it.

1698A - XOR Mixup

Hint
Solution
Implementation (C++)
Implementation (Python)

1698B - Rising Sand

Hint
Solution
Implementation (C++)
Implementation (Python)

1698C - 3SUM Closure

Hint
Solution
Implementation (C++)

1698D - Fixed Point Guessing

Hint
Solution
Implementation (C++)
Implementation (Python)

1698E - PermutationForces II

Hint 1
Hint 2
Hint 3
Solution
Implementation (C++)

1698F - Equal Reversal

Hint
Solution
Implementation (C++)

1698G - Long Binary String

Hint
Solution
Implementation (C++)
• +54

| Write comment?
 » 2 years ago, # |   -29 Lighting fast editorial!
•  » » 2 years ago, # ^ |   +11 All the editorial solutions say "soon"
•  » » » 2 years ago, # ^ | ← Rev. 4 →   +12 Sorry, it's a problem with Codeforces. We're trying to get it resolved ASAP. UPD: It's fixed now.
•  » » » » 2 years ago, # ^ |   0 It isn't fixed.
 » 2 years ago, # |   +45 I felt, a huge difference in the difficulty of problems D and E.
 » 2 years ago, # |   +3 One zero should be enough in C right? Since $0+0+a = a$ which is in the array.
•  » » 2 years ago, # ^ |   +3 Note that the problem statement says that indices i, j, k must be distinct.
•  » » » 2 years ago, # ^ |   +14 Note that the problem statement says that indices i, j, k must be distinct not the value.
•  » » » » 2 years ago, # ^ |   +5 Sorry, my bad.
 » 2 years ago, # | ← Rev. 2 →   -32 Another speedforces round but problems were good, enjoyed solving them
•  » » 2 years ago, # ^ |   +19
 » 2 years ago, # |   +12 it's very cool that you can solve every problem in python. Respect to the authors!
 » 2 years ago, # |   0 Thanks for the fast editorial!!!
 » 2 years ago, # |   +3 162104309 This is my python solution for D. It almost same with the c++ implementation.
 » 2 years ago, # |   0 I am quite new to problems like D. Can someone give me some tips on this type of question where you have to constantly take input and print . And if possible please share some questions to practice as well . Thank you in advance:)
•  » » 2 years ago, # ^ |   0 Read this blog https://codeforces.com/blog/entry/45307then select interactive tag in problem set and practice
 » 2 years ago, # |   0 How did a lot of people solve problem D? It looks very tricky to me.
•  » » 2 years ago, # ^ |   0 I don't know how other people solved it, but when I saw the number of questions <= 15, I thought that log2(1e4) == 14, so 80% is a binary search by my logic :)
•  » » » 2 years ago, # ^ |   -7 Yeah, but binary search decision function is tricky.
•  » » 2 years ago, # ^ |   -12 First of all, as the maximum numbers of queries you can ask it's 15, you know that this problem requires binary search.Secondly, for example if your query is 1-3, then you have to count the numbers that are in range 1-3 (if the problem returns you [1,4,5], then there is only one number in range 1-3 (1), at this point you can take the following conclusion: If the count var is odd, then the number that is haven't been swapped is in that range (1-3) because it could not be swapped by any other number, if the count var would have been even, then both numbers could have swapped each other. I haven't explained myself very well but I hope now u understand better now the problem.
 » 2 years ago, # | ← Rev. 2 →   0 My solution for problem d on my machine does n't printing zero but it says that i am printing zero when i submit my code 162169758
•  » » 2 years ago, # ^ |   +3 Run first test case on your machine Ur solution will return 0 as ans, actually same happend with me.
 » 2 years ago, # |   0 Can anyone tell me what's the difference between cout.flush(); and fflush(stdout); ?Used fflush(stdout); 162169561 : Idleness limit exceededUsed cout.flush(); 162169654 : AcceptedUsed fflush(stdout); removed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);162169729 : AcceptedI know we can simply use endl; but what's the difference between these two ?
•  » » 2 years ago, # ^ |   +1 stdout and cout are not the same, though they are synced by default. By ios::sync_with_stdio(0), you break the sync, making them work separately. In other words, cout is not flushed with fflush(stdout).
 » 2 years ago, # |   +2 It was a great Contest!! Solved Interactive for the very first time.
 » 2 years ago, # | ← Rev. 2 →   0 I didn't got the first claim of problem E that strength required is max ai-bi.Suppose a={5,1,2,3,4} b={1,2,3,4,5} still if s=1 a can be converted to b by these operations swap(a1,a2) swap(a2,a3) swap(a3,a4) swap(a4,a5),,then how is the claim true??
•  » » 2 years ago, # ^ |   +6 you swap x and y instead of a_x and a_y
•  » » » 2 years ago, # ^ |   0 my bad!!! I understood the problem incorrectly ;=(
 » 2 years ago, # |   0 Good problems, thanks for fast editorial
 » 2 years ago, # |   +6 I don't think you need sortings for E, making its complexity linear. For the last part, we can just take whatever is left as long as it is at least a[i]-s. We just need to do it in a decreasing order in a[i].
 » 2 years ago, # |   +29 flamestorm The time complexity for problem D is O(n + logn), not O(nlogn). You do only one binary search per test case, and the sum of lengths of all queried subarrays is at most n.
•  » » 2 years ago, # ^ |   -8 O(n + log(n)) is just O(n)
 » 2 years ago, # | ← Rev. 2 →   +22 Best Contest for me till date!! Great Tasks and Great Rating Changes as well. Thanks flamestorm and ScarletS
 » 2 years ago, # |   +42 Non-mathy interpretation for G:First of all treat the corner case of the string having only one 1.Let's use the string in the leftmost position possible. It's easy to prove that the best answer will have an operation there. Now, while there are more than 2 set positions, let's erase the second leftmost position that has some 1. To do that, just align the leftmost 1 from s with that position. I'll leave the proof of why that gives us the best answer as an exercise to the reader.Obviously we can't do that because it'd be too slow. Then the solution is just realizing that that process can be rewritten as matrix multiplication mod 2, the idea is that after doing the first operation, we keep a window of bits just to the right of the first set bit and slide it to the right, erasing the first position with an operation if it is 1. After realizing that, we can simply use the Baby-step Giant-step algorithm to find the first occurrence of our target 100000000...If you want another similar problem here's some problem from 2009 ICPC Brazilian Regionals. The statement is in portuguese and I don't know if it's translated somewhere, sorry for that.
•  » » 2 years ago, # ^ |   0 Could you please give a brief explanation on how the matrix multiplication works? I've got your first idea of how the greedy implementation will give the correct answer, but found it so hard to reduce the complexity. I've also searched for the definition of matrix multiplication on wiki and tried to figure it out myself but to no avail. Thanks so much for your attention.
 » 2 years ago, # |   0 Good C, it took me about 1 hour to guess the hidden conclusion :(
•  » » 23 months ago, # ^ |   0 same here
 » 2 years ago, # |   +20 Well,Here is my solution for problem E works in O(n) timecodeConsider to arrange a p[i] for each a[i] which satisfies $a[p[i]] - i <= s$ for each b[i] is not -1 , $p[b[i]] = i$ Then we make b[p[i]] = iIt can be prove in the same way as the officical solution that it's the sufficient and necessary condition for making such a valid b.So the answer is ways to arrange p[i].Firstly if an x is valid to be set as p[i],it must be valid to be set as p[i+1].So arrange p[i] from 1 to n.if p[i] is arranged by condition 2, we ignore it.else we count all the ways $all$ to arrange p[i] and mutiply our answer.$all$ can be calculated during the implementation.
•  » » 2 years ago, # ^ |   0 I did this too. To me it's much more intuitive than binary search — a pure combinatoric approach.
 » 2 years ago, # |   +1 Why the same code about D,in GNU C++20 (64) will getTime limit exceeded on test 1,in GNU C++17 can get accept.in GNU C++17 162184565 Time limit exceeded on test 1 and in GNU C++20 (64) 162184380 Accepted
 » 2 years ago, # |   +37 Problem F How to prove that those conditions mentioned in the solution are sufficient to determine if a can be turned into b ?
 » 2 years ago, # | ← Rev. 2 →   +8 Well, I think the complexity of the solution D is $O(n)$ rather than $O(n \log n)$. For the binary search here, the complexity is $\sum_i{n/2^i}$ which equals to $O(n)$ not $O(n\log n)$plus: The solution is fixed now.
 » 2 years ago, # | ← Rev. 2 →   0 In the Rising Sand problem, if we take an array [2,2,2] as input and k=2, we can make it too tall by alternating increasing one of adjacent piles one at a time. 2 2 2 k=2, 2 3 3 -we take 2nd and 3rd element and perform operation, 3 4 3 -we take 1st and 2nd element and perform operation
•  » » 2 years ago, # ^ |   0 Too tall doesn't mean greater that adjacent elements, it means greater than the sum of the adjacent elements
•  » » » 2 years ago, # ^ |   0 Sorry mate, realized it after 2min of posting the comment, but can't delete comment after 2 min :*)
 » 2 years ago, # | ← Rev. 2 →   -11 This is the best contest ever I've witnessed on codeforces, solving till D in a div 2 is something superb!! and it's my first time managing to do this!! Hats off for you guys! Please make lots of divs2 like this one.
 » 2 years ago, # |   0 Thank you for the great round.I went from only solving div. 4A to solving div. 2ABCD over the past two months; hopefully other fellow newbies can see similar improvements soon.
 » 2 years ago, # |   0 can anyone please explain the approach of problem C ?
•  » » 2 years ago, # ^ | ← Rev. 2 →   +2 Key Observations. 1)If the array contains more than 2 positive elements, then the array is not 3SUM-closed because if you take the summation of the 3 greatest positive numbers you won't get their sum in the array. 2) If the array contains more than 2 negative elements, then the array is not 3SUM-closed because if you take the summation of the 3 smallest negative numbers you won't get their sum in the array. 3) If the array contains more than one zeros, then you have to check only for one zero because, let the array is [0,0,a] then if you take 0+0+a=a, which is already present in the array. so finally your array only contains 1 zero (if present), and max 2 positive and 2 negative elements, which makes the array max of 5 sizes. you can check all triplets in O(N^3) for the array of max size 5. You can check my code for a better understanding. Code Link: https://codeforces.com/contest/1698/submission/162143985
•  » » » 2 years ago, # ^ |   0 That was a nice explaination
•  » » 2 years ago, # ^ | ← Rev. 3 →   +1 This was kinda my thought process when solving (slightly different from the editorial):1) All 0s is an obvious YES2) One non-zero and the rest are 0s is also an obvious YES3) Can we have exactly two non-zeros while the rest are 0s? Only if the two non-zeros sum to 0, i.e., if you have x, -x, and the rest are 0s.(Note: if you sort the array, the above three cases are easy to check)4) What if we have three non-zeros? Let's consider their signs.4a) If we have three positive values, then their sum would be an even greater positive value, which doesn't exist in the array. This would be a NO.4b) Similarly, we can't have three negative values. 4c) We could have at most two positive values and at most two negative values. But even with two positive values, we can't have any 0s then, since two positives plus 0 is a greater positive.4d) Similarly, if there are two negative values, then we can't have a 0.4e) Therefore, if there are at least three non-zeros, then we can have max 2 positives, max 2 negatives, and no 0s allowed at all. In other words, array size is max 4.Overall, my approach was to sort the array to quickly check cases 1, 2, and 3. That leaves Case 4, so first make sure the array size is max 4 (otherwise print NO). Once you're left with array size as max 4, you can simply brute force (I actually simplified this even further, but it's not necessary). The editorial suggests that sorting isn't required either. Simply save only the non-zero values when reading input. Then we can easily filter through the four cases with some counting, where the only remaining case is if we have at most four non-zeros (and no 0s), which we can brute force.
•  » » » 2 years ago, # ^ |   0 Can you please share your submission?
•  » » » » 2 years ago, # ^ |   0
•  » » » 8 months ago, # ^ |   0 How does this example works — [1,2,3,4,5,6]? Because if we see 1 + 2 + 3 = 6 which is also in array, so isn't this a 3SUM closed array?, Please help!
•  » » » » 8 months ago, # ^ |   0 Read the statement again."The array is called 3SUM-closed if for all distinct indices $i$, $j$, $k$, the sum $a_i+a_j+a_k$ is an element of the array."$4 + 5 + 6 = 15$ is not an element of the array, so it isn't 3SUM closed.
•  » » » » » 8 months ago, # ^ |   0 Thanks Mate, didn't read the question properly, missed the all part.
 » 2 years ago, # |   0 Can anyone tell me which case am I missing? It was failing the 5th pretest. #include #define ll long long using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >>t; int cas=0; while(t--){ cas++; int n,k; cin >>n; int pos=0,neg=0; vector v; ll sum=0; for(int i=0;i>inp; v.push_back(inp); if(inp<0) neg++; else if(inp>0) pos++; sum+=inp; } if((pos>1||neg>1)&&(pos+neg2||neg>2) cout<<"NO"<
•  » » 2 years ago, # ^ | ← Rev. 3 →   0 When the array size > 4 and it contains zeros (your 1st condition (pos >1 || neg>1 && (pos+neg
•  » » » 2 years ago, # ^ |   0 Ohh yes! Got it. Thank you so much!!
 » 2 years ago, # |   +18 Well, can anyone explain more about problem G? How is multiply and mod defined in GF(2)?
•  » » 2 years ago, # ^ |   +27 I had a hard time understanding it, and here's my opinion. Please point out if I make any mistakes. https://www.luogu.com.cn/paste/1z5pai9xI don't know why my comment doesn't display properly in Codeforces :( So I have to use a Chinese cloud clipboard. If you can't visit that, here's the origin code in Ubuntu.https://paste.ubuntu.com/p/JyYRF9TDhG
 » 2 years ago, # |   0 how to test interactive solutions in the local environment?
•  » » 2 years ago, # ^ |   0 Write your own judge and use input/output piping.
•  » » » 2 years ago, # ^ |   0 we have to provide input for the fixed query our program will be making right?
•  » » » » 2 years ago, # ^ |   0 Look at the input of an interactive problem in systest. Writing a judge being fed that input is usually a good idea.
 » 2 years ago, # |   0 can anyone tell me a testcase where my solution for problem C is wrong? my submission 162205607
•  » » 2 years ago, # ^ | ← Rev. 2 →   0 I believe the issue is with the case where mp[0] == 0 and n == 4. Your for loop only checks two sums: (a[0] + a[1] + a[2]) and (a[1] + a[2] + a[3]), but there are four possible triples. You can have an array that passes both of these sums but fails one of the other two sums that aren't checked.For example, consider the array [3, 1, -1, -2]. The two sums you check are (3 + 1 — 1) = 3 and (1 — 1 — 2) = -2, which both pass. But the sums (3 + 1 — 2) = 2 and (3 — 1 — 2) = 0 should result in NO. Your submission doesn't check these and seems to say YES.
 » 2 years ago, # |   0 I have a question about E problem. It is said that the minimum strength needed is max(a_i — b_i). However when the a is [1, 2, 7, 4, 3, 6, 5] and the b is [1, 2, 3, 4, 5, 6, 7], it only need strength 2.First, we swap 5 and 7, then the a changed to [1, 2, 5, 4, 3, 6, 7]. Second, we swap 3 and 5, then the a changed to [1, 2, 3, 4, 5, 6, 7]. It is 2 not 4
•  » » 2 years ago, # ^ | ← Rev. 2 →   +12 In the $i$-th move you pick two integers $x$ and $y$ such that $i \leq x \leq y \leq min(i + s, n)$.If the strength is two, you can swap 5 and 7 in the 5th move, and swap 3 and 5 in the 3rd move. But you have to process the moves in order (1st move, 2nd move, ...). For this example, you swap 3 and 7 in the 3rd move and swap 5 and 7 in the 5th move, making the required strength 4.
•  » » » 2 years ago, # ^ |   +8 thank you. I misunderstanding problem. I think in i-th move I can pick x and y such that x >=i and abs(y — x) <= s. thanks again
 » 2 years ago, # |   0 C was nice but annyoing. I liked the problems.
 » 2 years ago, # |   +49 Otherwise, it can be shown that there must exist some subarray with equal endpoints that contains b1 followed by b2, so we can reverse it.Show it then?
•  » » 2 years ago, # ^ |   0 It's just a pigeon hole principle. Consider all elements that are adjacent to elements equal to a1 (=b1) and note that we have a pair of same value on a right side.
•  » » 2 years ago, # ^ |   0 Thank you for asking this.To me this is the hardest part of this problem, and the author just left it as an exercise to the readers. What.
•  » » 15 months ago, # ^ | ← Rev. 2 →   0 Like in the editorial, assume array $a$ starts with $b_1, a_2, ...$ where $a_2 != b_2$. Consider the array $a$ where there is some consecutive $b_1, b_2$:$b_1, a_2, a_3, ..., a_n, b_1, b_2, y_1, y_2, ..., y_k$.Here, $a_2, ..., a_n$ is the sequence elements between the two $b_1$s and $y_1, y_2, ..., y_k$ is the rest of $a$ until the end of the array. Now consider the sets $A = \{a_2, ..., a_n\}$ and $Y = \{b_2, y_1, ..., y_k\}$ and suppose they are disjoint.Then all elements next to $b_2$ are either $b_1$ or in $Y$. Also, all elements in Y are only next to elements in $Y$.So the array b must be $b_1, b_2$ followed by some permutation of elements of Y. So if $A$ and $Y$ are disjoint then $A$ must be empty, which is a contradiction.
 » 2 years ago, # |   0 F is amazing, tho wish I didn't face undefined behaviour and spend 2h debugging it :noo:
 » 2 years ago, # | ← Rev. 2 →   0 Unable to see why my solution is failing for problem C: https://codeforces.com/contest/1698/submission/162286028Update: I missed some cases. Accepted here https://codeforces.com/contest/1698/submission/162287499
 » 2 years ago, # |   +1 My E solution with time $O(n)$:162292830
 » 2 years ago, # |   0 Can any one help me with verdict Idleness limit exceeded in D , I have not solved much of interactive problems link to my solution ... Any help is appreciated
•  » » 2 years ago, # ^ |   0 remove cin.tie(0)
•  » » 2 years ago, # ^ | ← Rev. 2 →   0 use 'endl' to flush the buffer: Accepted
 » 2 years ago, # |   +6 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).
 » 2 years ago, # |   0 In Problem E let strength s = 2 a = [1 6 2 4 3 5] b = [1 2 3 4 5 6]when i = 1 we swap 4 with 6 a = [1 4 2 6 3 5] b = [1 2 3 4 5 6]when i = 2 we swap 4 with 2 a = [1 2 4 6 3 5] b = [1 2 3 4 5 6]when i = 3 we swap 3 with 4 a = [1 2 3 6 4 5] b = [1 2 3 4 5 6]when i = 4 we swap 4 with 6 a = [1 2 3 4 6 5] b = [1 2 3 4 5 6]when i = 5 we swap 5 with 6 a = [1 2 3 4 5 6] b = [1 2 3 4 5 6]when i = 6 we swap 6 with 6 a = [1 2 3 4 5 6] b = [1 2 3 4 5 6]According to editorial minimum strength required is 4 as a2-b2 = 4 which will be max but here strength 2 is enough. What am I missing?
•  » » 2 years ago, # ^ |   +6 When i = 1, you can only swap elements between 1 and 3. The range is from i to min(i + s, n) for the ith swap.
•  » » » 2 years ago, # ^ |   0 Thank you.
 » 2 years ago, # |   0 Thanks for the great problem set. For problem F, how can we prove this statement:Otherwise, it can be shown that there must exist some subarray with equal endpoints that contains b1 followed by b2, so we can reverse it.
•  » » 2 years ago, # ^ | ← Rev. 3 →   0 for example, a = 13...12...b = 12........we can understand array a and array b as different eulerian path of same graph. "12..." of a is shorter than "12........" of b. but, both start with "12". To satisfy this length condition, eulerian path of b must go "13..." part of aDo you think my thoughts are valid?
 » 2 years ago, # |   -25 Can anyone please help me in problem C ? I am getting wrong answer on test 12. #include #define int long long using namespace std; int32_t main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; int pos = 0, neg = 0, zeros = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] == 0) { zeros++; } if (arr[i] > 0) { pos++; } if (arr[i] < 0) { neg++; } } sort(arr, arr + n); if (pos >= 3 || neg >= 3) { cout << "NO" << endl; continue; } if (pos == 2 && neg == 2) { if (zeros > 0) { cout << "NO" << endl; continue; } else { if (arr[n - 1] == -arr[0] && arr[n - 2] == -arr[1]) { cout << "YES" << endl; continue; } else { cout << "NO" << endl; continue; } } } if (pos == 2 && neg == 1) { if (zeros > 0) { cout << "NO" << endl; continue; } else { if (arr[0] == -arr[n - 1] || arr[0] == -arr[n - 2]) { cout << "YES" << endl; continue; } else { cout << "NO" << endl; continue; } } } if (pos == 1 && neg == 2) { if (zeros > 0) { cout << "NO" << endl; continue; } else { if (arr[n - 1] == -arr[0] || arr[n - 1] == -arr[1]) { cout << "YES" << endl; continue; } else { cout << "NO" << endl; continue; } } } if (pos == 2 && neg == 0) { cout << "NO" << endl; continue; } if (pos == 0 && neg == 2) { cout << "NO" << endl; continue; } if (pos == 1 && neg == 0) { cout << "YES" << endl; continue; } if (pos == 0 && neg == 1) { cout << "YES" << endl; continue; } if (pos == 0 && neg == 0) { cout << "YES" << endl; continue; } if (pos == 1 && neg == 1) { if (arr[n - 1] == -arr[0]) { cout << "YES" << endl; continue; } else { cout << "NO" << endl; continue; } } } return 0; } 
•  » » 2 years ago, # ^ |   0 When pos and neg are both 2, your submission checks if (arr[n — 1] == -arr[0] && arr[n — 2] == -arr[1]). While the second condition is required for YES-instances (can be proven), the first condition is not. For example, the array [-2, -2, 2, 6] does not satisfy the first condition (so your submission would say NO), but it should actually answer YES.
 » 2 years ago, # |   +5 I think the complexity in problem F should be $O(n^2)$ because you can find equal endpoints around $b_1,b_2$ in $O(n)$
 » 2 years ago, # |   0 Thanks for the Solution in Python!
 » 19 months ago, # |   0 I don't understand why we need to have a maximum of two zeroes. If the whole array is made up of zeroes, wouldn't we need at least 3 zeroes in order to have 3 distinct indices?
•  » » 12 months ago, # ^ | ← Rev. 2 →   0 It is implemented in such a way that if required condition is not satisfied it return as NO, suppose we take only 2 zeros, the for loop will won't run for as there is less than 3 elements, so wrong condition is not met, we return as YES. Now, by considering your example, by constraints n >= 3 then we know more than 3 elements exits, and wrong condition is not met. Pause and think
 » 9 months ago, # |   0 I have a doubt with 3 sum closure basically its says more than 2 positive or negative number stands out to be no as a outputbut see this test case -2 -3 -7 0 9 3 3 in this the answer should be no but -2-7+9 turns out to be 0 which is there in the array
•  » » 4 months ago, # ^ |   0 9+3+3 = 15 ,not in array
 » 7 weeks ago, # |   0 https://codeforces.com/contest/1698/submission/266519241 for problem C can anyone help me figure out why i am getting tle.