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=1n∣ai−ai−1∣S = \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+1−ai∣+∣ai−ai−1∣−∣ai+1−ai−1∣|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,n−1][1, n-1]. For the edge cases where i=0i = 0 or i=ni = n, the augmented points are simply ∣a1−a0∣|a_1 - a_0| and ∣an−an−1∣|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 min⁡iai\min_ia_i as choosing k0=min⁡iaik_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=min⁡iaia_j=\min_i a_i. Then aj%xj=aja_j \% x_j = a_j as xj≥k>k0=ajx_j \ge k \gt k_0 = a_j Namely, we see that it must be true then that ai≡aj(modxi)  ⟺  k≤xi∣ai−aja_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 ∣ai−aj∣|a_i-a_j| is ∣ai−aj∣|a_i-a_j| itself. Hence, it is clear that k=min⁡ixi=min⁡i∣ai−aj∣k=\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=min⁡i∣ai−a0∣=a1−a0k=\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,a1−a0) 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 n−in-i bits are always 1s, the iith bit is a 0, and the leftmost i−1i-1 bits iterate through the ordered sequence [0,(1<<(i−1))−1)[0,(1<<(i-1)) - 1) (see underlined). Hence, in the solution, we generate n−in-i 1s, and then shift ii into position.

B

cpp

C

cpp

D

cpp