Codeforces Round #760 ABCDEFG Solutions (Java/C++)

A. Polycarp and Sums of Subsequences

Solution:

We assume a[1]<a[2]<a[3]. So naturally b[7]=a[1]+a[2]+a[3], b[6]=a[2]+a[3], b[5]=a[1]+a[3].
So a[1]=b[7]-b[6], a[3]=b[5]-a[1]. Finally a[2]=b[7]-a[1]-a[3].

Code:

Java

Submission #140178453 - Codeforces
Codeforces. Programming competitions and contests, programming community

C++

Submission #140178560 - Codeforces
Codeforces. Programming competitions and contests, programming community

B. Missing Bigram

Solution:

If the first character of the current string is equal to the last character of the previous string, then continue. If unequal is found, then a string must be missed here, just add it.

If the whole process is always equal, just add a character at the end.

Code:

Java

Submission #140180603 - Codeforces
Codeforces. Programming competitions and contests, programming community

C++

Submission #140180730 - Codeforces
Codeforces. Programming competitions and contests, programming community

C. Paint the Array

Solution:

Obviously this d is either the greatest common divisor of all odd digits or the greatest common divisor of all even digits. And this d must not divide any remaining number.

Code:

Java

Submission #140182897 - Codeforces
Codeforces. Programming competitions and contests, programming community

C++

Submission #140183130 - Codeforces
Codeforces. Programming competitions and contests, programming community

D. Array and Operations

Solution:

Obviously, we can perform the operation with as large a number as possible, and make the result of each operation to be 0 as much as possible.

Code:

Java

Submission #140596362 - Codeforces
Codeforces. Programming competitions and contests, programming community

C++

Submission #140596487 - Codeforces
Codeforces. Programming competitions and contests, programming community

E. Singers' Tour

Solution:

This is a problem of solving a linear equation (take n=5 as an example):$$\begin{cases}
1\cdot a_1 + 5\cdot a_2 + 4\cdot a_3 + 3\cdot a_4 + 2\cdot a_5=b_1 \\
2\cdot a_1 + 1\cdot a_2 + 5\cdot a_3 + 4\cdot a_4 + 3\cdot a_5=b_2 \\
3\cdot a_1 + 2\cdot a_2 + 1\cdot a_3 + 5\cdot a_4 + 4\cdot a_5=b_3 \\
4\cdot a_1 + 3\cdot a_2 + 2\cdot a_3 + 1\cdot a_4 + 5\cdot a_5=b_4 \\
5\cdot a_1 + 4\cdot a_2 + 3\cdot a_3 + 2\cdot a_4 + 1\cdot a_5=b_5 \\
\end{cases}$$

So, $15\cdot a_1 + 15\cdot a_2 + 15\cdot a_3 + 15\cdot a_4 + 15\cdot a_5=b_1+b_2+b_3+b_4+b_5$. And so, $a_1+a_2+a_3+a_4+a_5=\frac {b_1+b_2+b_3+b_4+b_5} {15}$。

We let $sum=\frac {b_1+b_2+b_3+b_4+b_5} {15}$
So that $$\begin{cases}
4\cdot a_2 + 3\cdot a_3 + 2\cdot a_4 + 1\cdot a_5=b_1-1\cdot sum \\
-1\cdot a_2 + 3\cdot a_3 + 2\cdot a_4 + 1\cdot a_5=b_2-2\cdot sum \\
-1\cdot a_2 + -2\cdot a_3 + 2\cdot a_4 + 1\cdot a_5=b_3-3\cdot sum \\
-1\cdot a_2 + -2\cdot a_3 + -3\cdot a_4 + 1\cdot a_5=b_4-4\cdot sum \\
-1\cdot a_2 + -2\cdot a_3 + -3\cdot a_4 + -4\cdot a_5=b_5-5\cdot sum \\
\end{cases}$$
We found that in the above formula, two adjacent expressions can find a value of a (not including $a_1$).

Finally, we can calculate $a_1$ based on other $a_i$.
During this process, we need to verify several points: 1. sum should be an integer; 2. $a_i$ cannot exceed the range; 3. $a_1$ must be calculated twice to ensure that there is a solution.

Regardless of the value of sum or the value of a, the solution process is $O(n)$.

Code:

Java

Submission #140723967 - Codeforces
Codeforces. Programming competitions and contests, programming community

C++

Submission #140724122 - Codeforces
Codeforces. Programming competitions and contests, programming community

F. Reverse

Solution:

Direct brute force search is enough. Because it is not difficult to find that adding 0 to the right is actually very difficult. In fact, adding 0 is difficult. Because 0 is added to the right and reversed, 0 will disappear. So the value that may appear is actually very limited, so direct brute force search is enough.

Code:

It is recommended to refer to the Java code. C++ has some optimizations due to the stack issue in the DFS process, which reduces the readability.

Java

Submission #140726198 - Codeforces
Codeforces. Programming competitions and contests, programming community

C++

Submission #140731042 - Codeforces
Codeforces. Programming competitions and contests, programming community

G. Trader Problem

This is a very good problem of Disjoint-set and offline processing.

Codeforces Round #760 Trader Problem Solution (Java/C++)
Solution:First, we can directly consider all items together.Let’s consider the following example (I modified it a little bit based on the sample data):
Click the link above for detail solution
DigitalOcean Referral Badge 蜀ICP备19018968号