Editorial for Codeforces Round #910 (Div. 2)

Правка en56, от n0sk1ll, 2023-11-18 22:09:53

1779A - Hall of Fame

Author: n0sk1ll

Hint
Solution
Bonus

1779B - MKnez's ConstructiveForces Task

Author: BULLMCHOW & n0sk1ll

Hint
Solution
Bonus

1779C - Least Prefix Sum

Author: n0sk1ll

Hint
Solution
Bonus

1779D - Boris and His Amazing Haircut

Author: n0sk1ll

Hint
Solution
Bonus

1779E - Anya's Simultaneous Exhibition

Author: BULLMCHOW

Hint

Solution

  • if $$$j<i$$$, then every character in the segment $$$s[j...i-1]$$$ must be greater than or equal to $$$s_i$$$;
  • if $$$i<j$$$, then every character in the segment $$$s[i+1...j]$$$ must be smaller than or equal to $$$s_i$$$.

We want to reorder the string $$$s$$$ and get the string $$$s'$$$. Then, we check if we can delete some characters in $$$s'$$$ to achieve $$$t$$$. In other words, we want $$$t$$$ to be a subsequence of $$$s'$$$. Let us take a look at a general algorithm that checks if the string $$$a$$$ is a subsequence of the string $$$b$$$. We iterate through $$$a$$$ and for each character we find its first next appearance in $$$b$$$. If such character does not exist, we conclude that $$$a$$$ is not a subsequence of $$$b$$$. If we complete the iteration gracefully, then $$$a$$$ is a subsequence of $$$b$$$. We will try to check if $$$t$$$ is a subsequence of $$$s$$$, but with allowing ourselves to modify $$$s$$$ along the way.

We maintain $$$26$$$ queues for positions of each lowercase English letter in the string $$$s$$$. We iterate through the string $$$t$$$ and for every character $$$t_i$$$, we try to move the first such character in $$$s$$$ to position $$$i$$$. In other words, at every moment, the prefix of string $$$s$$$ is equal to the prefix of string $$$t$$$ (if it is possible). For the current character $$$t_i$$$ and the corresponding $$$s_j$$$, prefixes $$$t_1t_2\dots t_{i-1}$$$ and $$$s_1s_2\dots s_{i-1}$$$ are the same, which means that $$$j\geq i$$$. To move $$$s_j$$$ to position $$$i$$$, we need to delete all characters between $$$s_i$$$ and $$$s_j$$$ that are smaller than $$$s_j$$$. We will delete them and all characters from the current prefix $$$s_1s_2\dots s_{i-1}$$$ from the queues because they are no longer candidates for $$$s_j$$$. By doing so, $$$s_j$$$ will be the first character in the corresponding queue. If at some moment in our greedy algorithm, the queue we are looking for becomes empty, then the answer is "NO". Otherwise, we will make the prefix $$$s_1s_2\dots s_m$$$ equal to the $$$t$$$ and delete the remaining characters from $$$s$$$.

Why this greedy is optimal? Let's suppose for some character $$$t_{i_1}$$$ we chose $$$s_{j_1}$$$ and for $$$t_{i_2}$$$ we chose $$$s_{j_2}$$$, such that $$$i_1< i_2$$$, $$$j_1>j_2$$$ and $$$t_{i_1}=t_{i_2}=s_{j_1}=s_{j_2}$$$. We need to prove that if we can move $$$s_{j_1}$$$ to position $$$i_1$$$ and $$$t_{i_2}$$$ to position $$$i_2$$$, when we can move $$$s_{j_2}$$$ to $$$i_1$$$ and $$$s_{j_1}$$$ to $$$i_2$$$. In the moment when we chose $$$s_{j_1}$$$, prefixes $$$t_1t_2\dots t_{i_1-1}$$$ and $$$s_1s_2\dots s_{i_1-1}$$$ are the same, so $$$j_1\geq i_1$$$. Similarly, $$$j_2\geq i_2$$$, which means the only possibility is $$$i_1<i_2\leq j_2<j_1$$$.

If we can move $$$s_{j_1}$$$ to position $$$i_1$$$, than we can also move $$$s_{j_2}$$$ to $$$i_1$$$ because $$$s_{j_1}=s_{j_2}$$$ and $$$j_2<j_1$$$. Also, if we can move $$$s_{j_1}$$$ to $$$i_1$$$, than we can move $$$s_{j_1}$$$ to $$$i_2$$$ because $$$i_1<i_2$$$, from which it follows that we can move $$$s_{j_2}$$$ to $$$i_2$$$, because $$$s_{j_2}=s_{j_1}$$$ and $$$j_2<j_1$$$.

Overall complexity is $$$O(\alpha (n+m))$$$, where $$$\alpha$$$ is the alphabet size (\alpha=26 in this problem).

Bonus

1779F - Xorcerer's Stones

Author: n0sk1ll

Hint

Solution

For a matrix of type $$$1$$$, Misha can block all empty cells (except the one Vova stands on).

For a matrix of type $$$2$$$, Misha finds the shortest path to some exit with a single BFS and then blocks every other cell.

Matrices of type $$$3$$$ are more complicated. We want to find two shortest paths to the two closest exits and block the remaining empty cells.

But, notice how the paths will likely share their beginnings. We do not have to count those cells twice. Let's take a look at the junction where the two paths merge. If we first fix the junction, finding the shortest path to Vova can be done by running a single BFS and precalculating the shortest distances from each cell to Vova. Finding the shortest path from the junction to the two closest exits can also be done with BFS and precalculation. We modify the BFS, making it multi-source, with a source in each exit. Also, we will allow each cell to be visited twice (but by different exits). We will need to maintain the following data for each cell:

  • How many times was it visited;
  • The last exit/source that visited it;
  • The sum of paths from all exits/sources that visited the cell so far.

Running the BFS with proper implementation produces the answer. When everything said is precalculated, we fix the junction in $$$O(nm)$$$ ways (each empty cell can be a valid junction), and then calculate the shortest path from Vova to the two closest cells in $$$O(1)$$$ per junction.

Total complexity is $$$O(nm)$$$.

Bonus

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en80 Английский n0sk1ll 2023-11-19 19:35:24 0 (published)
en79 Английский n0sk1ll 2023-11-19 19:25:25 2 Tiny change: '## [1898B *-* Milic' -> '## [1898A *-* Milic'
en78 Английский n0sk1ll 2023-11-19 19:25:02 71
en77 Английский n0sk1ll 2023-11-19 19:24:46 67
en76 Английский n0sk1ll 2023-11-19 19:24:15 9 Tiny change: '## [1898B &dash; Milica and' -> '## [1898B-Milica and'
en75 Английский n0sk1ll 2023-11-19 19:23:54 1 Tiny change: '# [1898B &mdash; Mili' -> '# [1898B &dash; Mili'
en74 Английский n0sk1ll 2023-11-19 19:22:13 76
en73 Английский n0sk1ll 2023-11-19 19:19:56 1 Tiny change: 'oblem:1898A]\n\nAuth' -> 'oblem:1898 A]\n\nAuth'
en72 Английский n0sk1ll 2023-11-19 19:19:39 2 Tiny change: 'roblem:1897A]\n\nAuth' -> 'roblem:1898A]\n\nAuth'
en71 Английский n0sk1ll 2023-11-19 19:15:37 2 Tiny change: 'roblem:1898A]\n\nAuth' -> 'roblem:1897A]\n\nAuth'
en70 Английский n0sk1ll 2023-11-19 19:15:22 4 Tiny change: '[problem:12A]\n\nAuth' -> '[problem:1898A]\n\nAuth'
en69 Английский n0sk1ll 2023-11-19 19:07:55 4 Tiny change: '[problem:1898A]\n\nAuth' -> '[problem:12A]\n\nAuth'
en68 Английский n0sk1ll 2023-11-19 19:06:58 4 Tiny change: '[problem:12A]\n\nAuth' -> '[problem:1898A]\n\nAuth'
en67 Английский n0sk1ll 2023-11-19 19:06:40 4 Tiny change: '[problem:1898A]\n\nAuth' -> '[problem:12A]\n\nAuth'
en66 Английский n0sk1ll 2023-11-19 19:06:25 0 Tiny change: '## [problem:1898A]\n\nAuthor' -> '## \n\nAuthor'
en65 Английский n0sk1ll 2023-11-19 16:41:26 0 Tiny change: '## [problem:1' -> '[problem:1'
en64 Английский n0sk1ll 2023-11-19 16:39:45 24
en63 Английский n0sk1ll 2023-11-19 11:58:57 56
en62 Английский n0sk1ll 2023-11-18 22:23:08 6 Tiny change: '<j_1$.\n\nOverall com' -> '<j_1$.\n\nThe overall com'
en61 Английский n0sk1ll 2023-11-18 22:22:32 2 Tiny change: 'bet size (\alpha=26 in this p' -> 'bet size ($\alpha=26$ in this p'
en60 Английский n0sk1ll 2023-11-18 22:22:00 111
en59 Английский n0sk1ll 2023-11-18 22:11:55 12 Tiny change: 'ater than the secon' -> 'ater than or equal to the secon'
en58 Английский n0sk1ll 2023-11-18 22:11:19 4
en57 Английский n0sk1ll 2023-11-18 22:10:39 36
en56 Английский n0sk1ll 2023-11-18 22:09:53 1387
en55 Английский OutrunMyGun 2023-11-18 20:12:16 57
en54 Английский OutrunMyGun 2023-11-18 20:10:22 244
en53 Английский OutrunMyGun 2023-11-18 20:05:00 17
en52 Английский OutrunMyGun 2023-11-18 19:57:53 3
en51 Английский OutrunMyGun 2023-11-18 19:56:55 4 Tiny change: ' $s_j$ to the position ' -> ' $s_j$ to position '
en50 Английский OutrunMyGun 2023-11-18 19:54:02 9
en49 Английский OutrunMyGun 2023-11-18 19:52:28 2
en48 Английский OutrunMyGun 2023-11-18 19:52:28 0
en47 Английский OutrunMyGun 2023-11-18 19:52:28 2217
en46 Английский OutrunMyGun 2023-11-18 18:36:42 129
en45 Английский OutrunMyGun 2023-11-18 18:35:05 1924
en44 Английский OutrunMyGun 2023-11-18 18:34:10 1934 Reverted to en39
en43 Английский OutrunMyGun 2023-11-18 18:34:01 1600 Reverted to en41
en42 Английский OutrunMyGun 2023-11-18 18:33:19 2064
en41 Английский OutrunMyGun 2023-11-18 18:32:23 181
en40 Английский OutrunMyGun 2023-11-18 18:31:35 1997
en39 Английский n0sk1ll 2023-11-18 12:10:56 2 Tiny change: 'bilities. implementin' -> 'bilities. Implementin'
en38 Английский n0sk1ll 2023-11-18 12:07:00 7 Tiny change: 'd $j$, we simply find the ' -> 'd $j$, we find the '
en37 Английский n0sk1ll 2023-11-18 12:02:57 230
en36 Английский n0sk1ll 2023-11-15 23:17:37 45
en35 Английский n0sk1ll 2023-11-15 23:13:25 89
en34 Английский n0sk1ll 2023-11-15 23:11:36 54
en33 Английский n0sk1ll 2023-11-15 23:09:36 16 Tiny change: ' times as it should (and try ' -> ' times as necessary (and try '
en32 Английский n0sk1ll 2023-11-15 23:08:30 1332
en31 Английский n0sk1ll 2023-11-15 15:15:36 56
en30 Английский n0sk1ll 2023-11-15 15:12:25 2056
en29 Английский n0sk1ll 2023-11-13 21:55:30 2 Tiny change: 'ed in the *hint* section.\' -> 'ed in the hint section.\'
en28 Английский n0sk1ll 2023-11-13 21:53:55 155
en27 Английский n0sk1ll 2023-11-13 21:48:01 1519
en26 Английский n0sk1ll 2023-11-12 21:14:31 235
en25 Английский n0sk1ll 2023-11-12 21:11:13 57
en24 Английский n0sk1ll 2023-11-12 21:09:33 147
en23 Английский n0sk1ll 2023-11-12 21:04:05 643
en22 Английский n0sk1ll 2023-11-12 20:41:57 2562
en21 Английский n0sk1ll 2023-10-07 22:46:21 107
en20 Английский n0sk1ll 2023-10-07 22:44:30 38
en19 Английский n0sk1ll 2023-10-07 22:43:16 50
en18 Английский n0sk1ll 2023-10-07 22:39:42 2817 Tiny change: ' \equiv x (\mod p)$' -> ' \equiv x \ (\mod p)$'
en17 Английский n0sk1ll 2023-10-02 10:44:23 230
en16 Английский OutrunMyGun 2023-10-02 09:32:30 37 Tiny change: ' of $a_i$ after th' -> ' of $a_i$ to $\lfloor \frac{a_i}{m+1} \rfloor$ after th'
en15 Английский OutrunMyGun 2023-10-01 18:39:16 23
en14 Английский OutrunMyGun 2023-10-01 18:36:19 8 Tiny change: ' $a_i$ be divided into in' -> ' $a_i$ be splited into in'
en13 Английский OutrunMyGun 2023-10-01 18:32:14 224
en12 Английский OutrunMyGun 2023-10-01 18:24:48 5 Tiny change: 'we will solve the array' -> 'we will sort the array'
en11 Английский OutrunMyGun 2023-10-01 18:24:10 824
en10 Английский OutrunMyGun 2023-10-01 17:59:57 4
en9 Английский OutrunMyGun 2023-10-01 17:58:00 11 Tiny change: '>\nAfter every operation the number of' -> '>\nAfter each operation number of'
en8 Английский OutrunMyGun 2023-10-01 17:57:04 105
en7 Английский OutrunMyGun 2023-10-01 17:56:27 279
en6 Английский OutrunMyGun 2023-10-01 17:47:08 25
en5 Английский OutrunMyGun 2023-10-01 17:43:48 7 Tiny change: ' subarray left from $a_' -> ' subarray right from $a_'
en4 Английский OutrunMyGun 2023-10-01 17:42:48 189
en3 Английский OutrunMyGun 2023-10-01 17:33:21 45
en2 Английский n0sk1ll 2023-10-01 14:34:31 239
en1 Английский n0sk1ll 2023-10-01 14:31:01 1091 Initial revision (saved to drafts)