A2. Colored Path

Codeforces
IDCF1014A2
Time15000ms
Memory768MB
Difficulty
English · Original
Chinese · Translation
Formal · Original
Imagine an infinite colored stripe with a beginning but without end. The stripe is separated into squares, and its width is equal to the size of a square. Each square is painted in one of $C = 10$ colors. The colors are denoted by the first $10$ capital English letters. There are $U = 10$ chameleons living on the stripe. The chameleons are numbered by integers starting from zero. Initially, the chameleons occupy the first $10$ squares according to their order: chameleon $0$ is in the very first square, chameleon $1$ is in the square after the first one, and so on. Kurt is the shepherd of the chameleons. He can freely move around the colored stripe. Kurt wants the chameleons to move as far as possible from the beginning of the stripe before the night comes. Kurt holds $H = 10$ bottles of paint in his hands, and his backpack contains $S = 10\,000$ additional bottles. Each bottle also has one of the $C$ colors. Kurt can compel a chameleon to move. For that, he has to throw one of the bottles in his hands at one of the chameleons, painting it in the color of the bottle (let it be color $x$). After that, the chameleon which was hit by the bottle goes forward along the stripe until it finds the next free square painted in color $x$, and then stops at this square. As the chameleon moves because of the throw, other chameleons stand still, and Kurt just manages to blindly take the next bottle from his backpack, so that he again has exactly $10$ of them in his hands. Note that a chameleon starts to search for his color from the next square, so it can not stay in place even if the color of the thrown bottle was the same as the color of the square where it stood. Also note that two chameleons never appear at the same square simultaneously: if chameleon $h$ sees a square of the appropriate color, but the square is occupied by another chameleon, then $h$ will move further searching for a free square of the required color. The night comes when Kurt takes the last bottle from the backpack. (Kurt can also put chameleons at rest at any moment and wait for the night to come.) At this moment, consider chameleon $s$ which is closest to the beginning of the stripe. The result is an integer: the number of squares from the beginning of the stripe to this chameleon $s$. Come up with how Kurt should act to make this number as large as possible. **Input** The input consists of three lines. The first line contains integers $N$, $S$, $C$, $H$, and $U$: the length of the stripe pattern, the number of bottles of paint in the backpack, the number of colors, the number of bottles Kurt simultaneously holds in his hands, and the number of chameleons. These are given just in case: in all tests for this problem, $N = 100\,000$, $S = 10\,000$, $C = 10$, $H = 10$, and $U = 10$. The second line contains $N$ characters each of which is one of the first $10$ capital English letters. This is the stripe pattern: if we concatenate it with itself for infinity, and then read from left to right, the result will denote the colors of the squares on the stripe starting from the beginning. The third line contains $H + S$ characters each of which is also one of the first $10$ capital English letters. The first $H$ characters correspond to the bottles which Kurt initially holds in his hands. The next $S$ characters denote the colors of the bottles Kurt takes from his backpack, in the order in which it happens. As Kurt takes them blindly, the order in which they appear from the backpack can not be changed. **Output** Print from $0$ to $S$ actions for Kurt in chronological order, one per line. Each action is denoted as "$u$ $c$", where the integer $u$ is the number of the chameleon at which the bottle is thrown, and the character $c$ is the capital English letter denoting the color of the bottle. In case $u$ or $c$ are outside the required limits, or Kurt no bottle of the requested color in his hands, the output is judged as "_Wrong Answer_". **Testing** Your solution will be checked on sets of tests generated in advance. Each test is created using a pseudo-random number generator. You can consider that the colors of the squares in the stripe pattern, the bottles in Kurt's hands, and the bottles in the backpack are uniformly distributed (the probabilities of all $C$ colors are the same) and mutually independent (the probabilities of all $C^{N + H + S}$ possible tests are the same). A solution's score is the average of its results on all the tests. If a solution did not output a valid answer on a certain test, the result for this test is zero. During the main phase of the contest, there are two ways to send a solution for checking. * The first one is to check on examples (problem A1 in the contest). There are $10$ example tests which are also available for local testing. As soon as the solution is checked, you can see reports for all examples by clicking on the submission result. The example tests can be downloaded from here: [https://assets.codeforces.com/rounds/1014/examples.zip](https://assets.codeforces.com/rounds/1014/examples.zip) (Windows line endings), [https://assets.codeforces.com/rounds/1014/examples.tar.gz](https://assets.codeforces.com/rounds/1014/examples.tar.gz) (Linux line endings). * The second way is to check on preliminary tests (problem A2 in the contest). There are $100$ preliminary tests which are generated in advance but kept secret. The score for preliminary tests (but not for example tests) is used in the preliminary scoreboard. This score does not affect the final results, but nevertheless allows to roughly compare a solution with others. After the main phase ends, for each participant, the system chooses the final solution: * consider all solutions sent for **preliminary testing**; * choose the ones which got a total score strictly greater than zero; * define the final solution as the one of chosen solutions which has **the latest** submission time. Note that the solutions sent only to be checked on examples are not considered when choosing the final solution. During the final testing, all final solutions will be checked on the same large set of a large number ($\approx 1000$) of final tests. The score for final tests determines the final scoreboard. The winner is the contestant whose solution gets the highest total score. In case two or more participants have equal total score, the contestants with such score tie for the same place. **Example** To have an example which fits into the problem statement, let $N = 40$ and $S = 5$ (recall that in all the real tests for this problem, $N = 100\,000$ and $S = 10\,000$). 40 5 10 10 10 IFDFDIIJCDJCEJIJAHACEBACBAJBIFDIFJAEGCHC DIBJIDHDBFHIJDF Consider an output which lists three actions. 0 D 8 H 1 D The initial state: chameleons: 0123456789 line: IFDFDIIJCDJCEJIJAHACEBACBAJBIFDIFJAEGCHCIFDFD... hand: B,B,D,D,D,F,H,I,I,J next: H,I,J,D,F The state after the first action (_0 D_): chameleons: 123456789 0 line: IFDFDIIJCDJCEJIJAHACEBACBAJBIFDIFJAEGCHCIFDFD... hand: B,B,D,D,F,H,H,I,I,J next: I,J,D,F The state after the second action (_8 H_): chameleons: 1234567 9 8 0 line: IFDFDIIJCDJCEJIJAHACEBACBAJBIFDIFJAEGCHCIFDFD... hand: B,B,D,D,F,H,I,I,I,J next: J,D,F The state after the third action (_1 D_): chameleons: 234567 9 8 0 1 line: IFDFDIIJCDJCEJIJAHACEBACBAJBIFDIFJAEGCHCIFDFD... hand: B,B,D,F,H,I,I,I,J,J next: D,F The chameleon closest to the beginning of the stripe is the chameleon number $2$. The result, which is the number of squares from the beginning of the stripe to this chameleon, is equal to 2. **Note** The rules of chameleon movement are similar to the game mechanic used in the children's board games "Hoot Owl Hoot!" and "Cartagena". [samples]
[{"iden":"statement","content":"想象一条有起点但无终点的无限彩色条带。条带被划分为方格,其宽度等于一个方格的大小。每个方格被涂成 $C = 10$ 种颜色之一。颜色用前 $10$ 个大写英文字母表示。\n\n条带上生活着 $U = 10$ 只变色龙。变色龙按从零开始的整数编号。最初,变色龙按顺序占据前 $10$ 个方格:变色龙 $0$ 位于最前面的方格,变色龙 $1$ 位于下一个方格,依此类推。\n\nKurt 是变色龙的牧人。他可以在彩色条带上自由移动。Kurt 希望在夜晚来临前,让变色龙尽可能远离条带的起点。Kurt 手中握有 $H = 10$ 瓶颜料,他的背包中还有 $S = 10\\ 000$ 瓶额外的颜料。每瓶颜料也具有 $C$ 种颜色之一。\n\nKurt 可以命令一只变色龙移动。为此,他必须将手中的一瓶颜料投向其中一只变色龙,将其涂成该瓶颜料的颜色(记为颜色 $x$)。之后,被击中的变色龙将沿着条带向前移动,直到找到下一个颜色为 $x$ 的空方格,并停在该方格上。当变色龙因投掷而移动时,其他变色龙保持静止,而 Kurt 会从背包中盲取下一瓶颜料,使他手中再次恰好有 $10$ 瓶。\n\n请注意,变色龙从下一个方格开始寻找其颜色,因此即使投掷的颜料颜色与它当前所在方格的颜色相同,它也不能原地不动。另外请注意,两个变色龙永远不会同时出现在同一个方格上:如果变色龙 $h$ 看到一个颜色匹配的方格,但该方格被另一只变色龙占据,则 $h$ 将继续向前寻找下一个空的所需颜色方格。\n\n当 Kurt 从背包中取出最后一瓶颜料时,夜晚来临。(Kurt 也可以在任何时候让变色龙静止并等待夜晚来临。)此时,考虑距离条带起点最近的变色龙 $s$。结果是一个整数:从条带起点到该变色龙 $s$ 的方格数。请设计 Kurt 的行动策略,使该数值尽可能大。\n\n#cf_span(class=[], body=[*输入*])\n\n输入包含三行。\n\n第一行包含整数 $N$、$S$、$C$、$H$ 和 $U$:条带图案的长度、背包中颜料瓶的数量、颜色数量、Kurt 同时握在手中的瓶子数量、变色龙数量。这些值仅作为参考:在本题的所有测试用例中,$N = 100\\ 000$,$S = 10\\ 000$,$C = 10$,$H = 10$,$U = 10$。\n\n第二行包含 $N$ 个字符,每个字符是前 $10$ 个大写英文字母之一。这是条带图案:若将其无限重复连接,然后从左到右读取,结果将表示从起点开始的条带上各方格的颜色。\n\n第三行包含 $H + S$ 个字符,每个字符也是前 $10$ 个大写英文字母之一。前 $H$ 个字符对应 Kurt 最初握在手中的瓶子。接下来的 $S$ 个字符表示 Kurt 从背包中取出的瓶子的颜色,按取出顺序排列。由于 Kurt 是盲取,这些瓶子的出现顺序无法更改。\n\n#cf_span(class=[], body=[*输出*])\n\n按时间顺序,每行输出 $0$ 到 $S$ 个 Kurt 的操作。每个操作表示为 \"$u$ $c$\",其中整数 $u$ 是投掷瓶子的目标变色龙编号,字符 $c$ 是表示瓶内颜色的大写英文字母。\n\n若 $u$ 或 $c$ 超出所需范围,或 Kurt 手中没有请求颜色的瓶子,则输出将被判为 \"_Wrong Answer_\"。\n\n#cf_span(class=[], body=[*测试*])\n\n你的解决方案将被预先生成的测试集进行检查。每个测试用例使用伪随机数生成器创建。你可以认为条带图案中的颜色、Kurt 手中的瓶子和背包中的瓶子都是均匀分布的(所有 $C$ 种颜色的概率相同)且相互独立(所有 $C^{N + H + S}$ 种可能测试的概率相同)。\n\n一个解决方案的得分是其在所有测试用例上的结果的平均值。如果某个测试用例未能输出有效答案,则该测试用例的结果为零。\n\n在比赛主要阶段,有两种方式提交解决方案进行检查。\n\n主要阶段结束后,系统将为每位参赛者选择最终方案:\n\n请注意,仅用于示例检查的提交不会被用于选择最终方案。\n\n在最终测试阶段,所有最终方案将在同一组大型测试集(约 $1000$ 个最终测试)上进行检查。最终测试的得分决定最终排行榜。得分最高的参赛者获胜。若两名或多名参赛者得分相同,则他们并列相同名次。\n\n#cf_span(class=[], body=[*示例*])\n\n为使示例适配题目描述,设 $N = 40$ 且 $S = 5$(请注意,在本题所有真实测试中,$N = 100\\ 000$ 且 $S = 10\\ 000$)。\n\n考虑一个列出三个操作的输出:\n\n初始状态:\n\n第一次操作(_0 D_)后的状态:\n\n第二次操作(_8 H_)后的状态:\n\n第三次操作(_1 D_)后的状态:\n\n距离条带起点最近的变色龙是编号为 $2$ 的变色龙。结果,即从条带起点到该变色龙的方格数,等于 2。\n\n#cf_span(class=[], body=[*注释*])\n\n变色龙移动的规则类似于儿童桌游 \"Hoot Owl Hoot!\" 和 \"Cartagena\" 中的游戏机制。"}]}
**Definitions** Let $ N = 100{,}000 $, $ S = 10{,}000 $, $ C = 10 $, $ H = 10 $, $ U = 10 $. Let $ P = (p_0, p_1, \dots, p_{N-1}) $ be the periodic stripe pattern of length $ N $, where each $ p_i \in \Sigma = \{A, B, \dots, J\} $. The infinite stripe is defined by $ \hat{p}_i = p_{i \bmod N} $ for all $ i \in \mathbb{N}_0 $. Let $ B_{\text{hand}} = (b_0, b_1, \dots, b_{H-1}) \in \Sigma^H $ be the initial set of bottles in Kurt’s hands. Let $ B_{\text{back}} = (b_H, b_{H+1}, \dots, b_{H+S-1}) \in \Sigma^S $ be the sequence of bottles drawn from the backpack, in fixed order. Let $ \mathcal{C} = \{c_0, c_1, \dots, c_{U-1}\} $ be the set of chameleons, initially positioned at $ \text{pos}(c_u) = u $ for $ u = 0, \dots, 9 $. **Constraints** 1. At any time, chameleons occupy distinct positions: $ \text{pos}(c_u) \neq \text{pos}(c_v) $ for $ u \neq v $. 2. Kurt always holds exactly $ H = 10 $ bottles: initially $ B_{\text{hand}} $, and after each action, he discards one bottle from hand and draws the next from $ B_{\text{back}} $. 3. An action is valid only if the bottle color $ c $ is present in Kurt’s current hand. 4. After $ S $ actions, the process terminates (night comes). Kurt may terminate early (i.e., perform fewer than $ S $ actions). **Objective** Maximize $ \min_{u \in \{0,\dots,9\}} \text{pos}(c_u) $, i.e., the position of the leftmost chameleon at termination. **Action Dynamics** For an action $ (u, c) $: - Remove one bottle of color $ c $ from Kurt’s hand. - Draw the next bottle from $ B_{\text{back}} $ (if any) and add it to hand. - Chameleon $ c_u $ moves from current position $ x = \text{pos}(c_u) $ to the smallest $ y > x $ such that: - $ \hat{p}_y = c $, - $ y $ is unoccupied by any other chameleon. - Update $ \text{pos}(c_u) = y $. **Output** A sequence of $ k \in \{0, 1, \dots, S\} $ actions $ (u_i, c_i) $, $ i = 1, \dots, k $, such that: - $ u_i \in \{0, \dots, 9\} $, - $ c_i \in \Sigma $, - $ c_i $ is in Kurt’s hand at step $ i $, - The final configuration maximizes $ \min_u \text{pos}(c_u) $.
API Response (JSON)
{
  "problem": {
    "name": "A2. Colored Path",
    "description": {
      "content": "Imagine an infinite colored stripe with a beginning but without end. The stripe is separated into squares, and its width is equal to the size of a square. Each square is painted in one of $C = 10$ col",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 15000,
      "memory_limit": 786432
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1014A2"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Imagine an infinite colored stripe with a beginning but without end. The stripe is separated into squares, and its width is equal to the size of a square. Each square is painted in one of $C = 10$ col...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "[{\"iden\":\"statement\",\"content\":\"想象一条有起点但无终点的无限彩色条带。条带被划分为方格,其宽度等于一个方格的大小。每个方格被涂成 $C = 10$ 种颜色之一。颜色用前 $10$ 个大写英文字母表示。\\n\\n条带上生活着 $U = 10$ 只变色龙。变色龙按从零开始的整数编号。最初,变色龙按顺序占据前 $10$ 个方格:变色龙 $0$ 位于最前面的方格,变色龙 $1...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ N = 100{,}000 $, $ S = 10{,}000 $, $ C = 10 $, $ H = 10 $, $ U = 10 $.  \nLet $ P = (p_0, p_1, \\dots, p_{N-1}) $ be the periodic stripe pattern of length $ N $, where each $ p_i...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments