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

**Hint**

**Solution**

**Implementation (C++)**

**Implementation (Python)**

**Hint**

**Solution**

**Implementation (C++)**

**Implementation (Python)**

**Hint**

**Solution**

**Implementation (C++)**

**Hint**

**Solution**

**Implementation (C++)**

**Implementation (Python)**

**Hint 1**

**Hint 2**

**Hint 3**

**Solution**

**Implementation (C++)**

**Hint**

**Solution**

**Implementation (C++)**

**Hint**

**Solution**

**Implementation (C++)**

Lighting fast editorial!

All the editorial solutions say "soon"

Sorry, it's a problem with Codeforces. We're trying to get it resolved ASAP.

UPD: It's fixed now.

It isn't fixed.

I felt, a huge difference in the difficulty of problems D and E.

One zero should be enough in C right? Since $$$0+0+a = a$$$ which is in the array.

Note that the problem statement says that indices i, j, k must be

distinct.Note that the problem statement says that

indicesi, j, k must bedistinctnot thevalue.Sorry, my bad.

Another speedforces round but problems were good, enjoyed solving them

Credits... https://codeforces.com/blog/entry/104259?#comment-926682

it's very cool that you can solve every problem in python. Respect to the authors!

Thanks for the fast editorial!!!

162104309 This is my python solution for D. It almost same with the c++ implementation.

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:)

Read this blog https://codeforces.com/blog/entry/45307

then select interactive tag in problem set and practice

How did a lot of people solve problem D? It looks very tricky to me.

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 :)

Yeah, but binary search decision function is tricky.

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:

I haven't explained myself very well but I hope now u understand better now the problem.

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

Run first test case on your machine Ur solution will return 0 as ans, actually same happend with me.

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 ?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)`

.It was a great Contest!! Solved Interactive for the very first time.

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??

you swap x and y instead of a_x and a_y

my bad!!! I understood the problem incorrectly ;=(

Good problems, thanks for fast editorial

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].

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.

O(n + log(n)) is just O(n)

Best Contest for me till date!! Great Tasks and Great Rating Changes as well. Thanks flamestorm and ScarletS

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.

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.

Good C, it took me about 1 hour to guess the hidden conclusion :(

same here

Well,Here is my solution for problem E works in O(n) time

code

Consider 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]] = i

It 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.

I did this too. To me it's much more intuitive than binary search — a pure combinatoric approach.

Why the same code about

`D`

,in`GNU C++20 (64)`

will get`Time 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`

Problem F How to prove that those conditions mentioned in the solution are sufficient to determine if a can be turned into b ?

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.

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

Too tall doesn't mean greater that adjacent elements, it means greater than the sum of the adjacent elements

Sorry mate, realized it after 2min of posting the comment, but can't delete comment after 2 min :*)

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.

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.

can anyone please explain the approach of problem C ?

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

That was a nice explaination

This was kinda my thought process when solving (slightly different from the editorial):

1) All 0s is an obvious YES

2) One non-zero and the rest are 0s is also an obvious YES

3) 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.

Can you please share your submission?

My submission during the contest (probably not very elegant)

My submission after reading the editorial (with explanatory comments)

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!

Read the statement again.

"The array is called 3SUM-closed if

for alldistinct 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.

Thanks Mate, didn't read the question properly, missed the

allpart.Can anyone tell me which case am I missing? It was failing the 5th pretest.

Thank you!

When the array size > 4 and it contains zeros (your 1st condition (pos >1 || neg>1 && (pos+neg<n)) won't execute, in this case, you are printing "YES" that's why you are getting WA on test 5. Ex; if array=[0,0,0,-2,3] in this case you are printing "YES". but the right answer would be "NO". You can refer to my code for this case. Code Link: https://codeforces.com/contest/1698/submission/162143985

Ohh yes! Got it. Thank you so much!!

Well, can anyone explain more about problem G? How is multiply and mod defined in GF(2)?

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/1z5pai9x

I 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

how to test interactive solutions in the local environment?

Write your own judge and use input/output piping.

we have to provide input for the fixed query our program will be making right?

Look at the input of an interactive problem in systest. Writing a judge being fed that input is usually a good idea.

For simple basic testing, you can write down your own array on paper that fits the requirements of the problem. When your program asks queries, consult your array and answer honestly. Observe whether your program eventually gets the correct answer, asks the queries that you expect it to ask (binary searching), etc.

Obviously, this is impractical for large comprehensive testing, but as with any other problem, you would need to write a separate program to deal with that. The bad news is that an interactive problem would require an interactive tester, so the testing program would be more complicated than simply generating test cases. The good news is that the interactive tester should easily identify whether your answers are correct or not (which is often difficult for non-interactive problems). In any case, it's questionable on whether this is worth your time and effort in a contest setting if you are reasonably confident in your solution already, since you could instead be focusing on other problems instead.

can anyone tell me a testcase where my solution for problem C is wrong? my submission 162205607

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.

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

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.

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

C was nice but annyoing. I liked the problems.

`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?

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.

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.

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.

F is amazing, tho wish I didn't face undefined behaviour and spend 2h debugging it :noo:

Unable to see why my solution is failing for problem C: https://codeforces.com/contest/1698/submission/162286028

Update: I missed some cases. Accepted here https://codeforces.com/contest/1698/submission/162287499

My E solution with time $$$O(n)$$$:162292830

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

remove

`cin.tie(0)`

use 'endl' to flush the buffer: Accepted

If you are/were getting a

WA/REverdict on problems from this contest, you can get thesmallestpossiblecounter examplefor 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

submissionandticket(s).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?

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.

Thank you.

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.`

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 a

Do you think my thoughts are valid?

Can anyone please help me in problem C ? I am getting wrong answer on test 12.

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.

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)$$$

Thanks for the Solution in Python!

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?

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

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 output

but 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

9+3+3 = 15 ,not in array

https://codeforces.com/contest/1698/submission/266519241 for problem C can anyone help me figure out why i am getting tle.