Codeforces Round #1071

Problem A

The problem reduces to finding the longest string such that

  1. there are only kk characters in the string
  2. every xx characters are different

The answer is kx+1kx+1

Problem B

Suppose that you don't skip any floors. The distance you need to travel isS=i=1naiai1S = \sum_{i = 1}^{n} |a_i - a_{i-1}|

To figure out which floor to skip, we consider skipping each floor in the sequence, in particular to skip floor ii, the points is augmented by ai+1ai+aiai1ai+1ai1|a_{i + 1} - a_i| + |a_i - a_{i - 1}| - |a_{i + 1 } - a_{i - 1}|

Note that this works for all ii in [1,n1][1, n-1]. For the edge cases where i=0i = 0 or i=ni = n, the augmented points are simply a1a0|a_1 - a_0| and anan1|a_n - a_{n - 1}| respectively. Note that it is only important to store the best net difference because we can apply the difference at the end.

Problem C

Two observations help simplify the problem:

  1. We can immediately lower bound kk by miniai\min_ia_i as choosing k0=miniaik_0=\min_i a_i is possible if we choose xi=aix_i=a_i, namely ai%ai=0a_i\%a_i=0
  2. x,m\forall x, m, x<m    x(modm)xx<m \implies x\pmod m\equiv x.

Suppose we choose some k>k0k\gt k_0. Let aj=miniaia_j=\min_i a_i. Then aj%xj=aja_j \% x_j = a_j as xjk>k0=ajx_j \ge k \gt k_0 = a_j Namely, we see that it must be true then that aiaj(modxi)    kxiaiaja_i\equiv a_j\pmod{x_i}\iff k\le x_i\big|a_i-a_j Note that the biggest xix_i which divides aiaj|a_i-a_j| is aiaj|a_i-a_j| itself. Hence, it is clear that k=minixi=miniaiajk=\min_i x_i=\min_i |a_i-a_j| Without loss of generality, let us sort aa. Namely, this makes j=0j=0. The closest number to a0a_0 is a1a_1. Hence, k=miniaia0=a1a0k=\min_i |a_i-a_0|=a_1-a_0

Hence, the final answer must be the best of the two strategies, namely k=max(a0,a1a0) k=\max(a_0, a_1 - a_0)

Problem D

Problem D is a typical bitwise manipulation question. The order of the numbers we want to generate looks as follow:

(i=0)
1111111...
(i=1)
0111111...
(i=2)
0011111...
1011111...
(i=3)
0001111...
0101111...
1001111...
1101111...
...

This solution minimizes the number of bit positions whereby there exists a 0 in any row, while preserving a lexographical sorting. We systematically sacrifice the leftmost bits, and iterate thorugh all the permutations which require the sacrifice but have not appeared.

Notice that the rightmost nin-i bits are always 1s, the iith bit is a 0, and the leftmost i1i-1 bits iterate through the ordered sequence [0,(1<<(i1))1)[0,(1<<(i-1)) - 1) (see underlined). Hence, in the solution, we generate nin-i 1s, and then shift ii into position.

B

cpp

C

cpp

D

cpp