E. Tests Renumeration

Codeforces
IDCF861E
Time2000ms
Memory256MB
Difficulty
English · Original
Chinese · Translation
Formal · Original
The All-Berland National Olympiad in Informatics has just ended! Now Vladimir wants to upload the contest from the Olympiad as a gym to a popular Codehorses website. Unfortunately, the archive with Olympiad's data is a mess. For example, the files with tests are named arbitrary without any logic. Vladimir wants to rename the files with tests so that their names are distinct integers starting from 1 without any gaps, namely, "_1_", "_2_", ..., "_n_', where _n_ is the total number of tests. Some of the files contain tests from statements (examples), while others contain regular tests. It is possible that there are no examples, and it is possible that all tests are examples. Vladimir wants to rename the files so that the examples are the first several tests, all all the next files contain regular tests only. The only operation Vladimir can perform is the "_move_" command. Vladimir wants to write a script file, each of the lines in which is "_move file_1 file_2_", that means that the file "_file_1_" is to be renamed to "_file_2_". If there is a file "_file_2_" at the moment of this line being run, then this file is to be rewritten. After the line "_move file_1 file_2_" the file "_file_1_" doesn't exist, but there is a file "_file_2_" with content equal to the content of "_file_1_" before the "_move_" command. Help Vladimir to write the script file with the minimum possible number of lines so that after this script is run: * all examples are the first several tests having filenames "_1_", "_2_", ..., "_e_", where _e_ is the total number of examples; * all other files contain regular tests with filenames "__e_ + 1_", "__e_ + 2_", ..., "_n_", where _n_ is the total number of all tests. ## Input The first line contains single integer _n_ (1 ≤ _n_ ≤ 105) — the number of files with tests. _n_ lines follow, each describing a file with test. Each line has a form of "_name_i type_i_", where "_name_i_" is the filename, and "_type_i_" equals "_1_", if the _i_\-th file contains an example test, and "_0_" if it contains a regular test. Filenames of each file are strings of digits and small English letters with length from 1 to 6 characters. The filenames are guaranteed to be distinct. ## Output In the first line print the minimum number of lines in Vladimir's script file. After that print the script file, each line should be "_move file_1 file_2_", where "_file_1_" is an existing at the moment of this line being run filename, and "_file_2_" — is a string of digits and small English letters with length from 1 to 6. [samples]
全俄罗斯信息学奥林匹克竞赛刚刚结束!现在弗拉基米尔希望将本次竞赛的题目作为训练赛上传至流行的 Codehorses 网站。 不幸的是,竞赛数据的归档文件非常混乱,例如测试文件的命名毫无规律可言。 弗拉基米尔希望将测试文件重命名为从 #cf_span[1] 开始、无间隔的连续整数,即 "_1_"、"_2_"、...、"#cf_span[n]',其中 #cf_span[n] 是测试文件的总数。 部分文件包含题目中的示例测试,其余文件包含常规测试。可能不存在示例测试,也可能所有测试都是示例。弗拉基米尔希望重命名文件,使得示例测试位于最前面若干个位置,其余所有文件均为常规测试。 弗拉基米尔唯一能执行的操作是 "_move_" 命令。他希望编写一个脚本文件,其中每一行形如 "_move file_1 file_2_",表示将文件 "_file_1_" 重命名为 "_file_2_"。若在执行该行时已存在文件 "_file_2_",则该文件将被覆盖。执行完 "_move file_1 file_2_" 后,文件 "_file_1_" 将不再存在,而文件 "_file_2_" 的内容与执行前的 "_file_1_" 相同。 请帮助弗拉基米尔编写一个行数最少的脚本文件,使得脚本执行后满足: 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 105]) —— 测试文件的数量。 接下来 #cf_span[n] 行,每行描述一个测试文件,格式为 "_name_i type_i_",其中 "_name_i_" 是文件名,"_type_i_" 等于 "_1_" 表示第 #cf_span[i] 个文件是示例测试,等于 "_0_" 表示是常规测试。每个文件名是由数字和小写英文字母组成的字符串,长度为 #cf_span[1] 到 #cf_span[6] 个字符,且所有文件名互不相同。 第一行输出弗拉基米尔脚本文件的最少行数。 随后输出脚本文件,每行形如 "_move file_1 file_2_",其中 "_file_1_" 是执行该行时存在的文件名,"_file_2_" 是由数字和小写英文字母组成的、长度为 #cf_span[1] 到 #cf_span[6] 的字符串。 ## Input 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 105]) —— 测试文件的数量。接下来 #cf_span[n] 行,每行描述一个测试文件,格式为 "_name_i type_i_",其中 "_name_i_" 是文件名,"_type_i_" 等于 "_1_" 表示第 #cf_span[i] 个文件是示例测试,等于 "_0_" 表示是常规测试。每个文件名是由数字和小写英文字母组成的字符串,长度为 #cf_span[1] 到 #cf_span[6] 个字符,且所有文件名互不相同。 ## Output 第一行输出弗拉基米尔脚本文件的最少行数。随后输出脚本文件,每行形如 "_move file_1 file_2_",其中 "_file_1_" 是执行该行时存在的文件名,"_file_2_" 是由数字和小写英文字母组成的、长度为 #cf_span[1] 到 #cf_span[6] 的字符串。 [samples]
**Definitions** Let $ n \in \mathbb{Z}^+ $ be the number of files. Let $ F = \{ (f_i, t_i) \mid i \in \{1, \dots, n\} \} $ be the set of files, where: - $ f_i \in \Sigma^* $ is the original filename ($ \Sigma = \text{digits} \cup \text{lowercase English letters} $, $ 1 \leq |f_i| \leq 6 $), - $ t_i \in \{0,1\} $ is the type: $ t_i = 1 $ if example, $ t_i = 0 $ if regular. Let $ E = \{ i \mid t_i = 1 \} $ be the set of indices of example files, and $ R = \{ i \mid t_i = 0 \} $ be the set of indices of regular files. Let $ e = |E| $, $ r = |R| $, so $ e + r = n $. **Constraints** 1. $ 1 \leq n \leq 10^5 $ 2. All $ f_i $ are distinct. 3. $ t_i \in \{0,1\} $ for all $ i $. **Objective** Rename all files via a minimum-length sequence of `move` operations such that: - The final filenames are $ \{ \texttt{"1"}, \texttt{"2"}, \dots, \texttt{"n"} \} $, - Files with $ t_i = 1 $ (examples) are assigned filenames $ \{ \texttt{"1"}, \dots, \texttt{"e"} \} $, - Files with $ t_i = 0 $ (regular) are assigned filenames $ \{ \texttt{"e+1"}, \dots, \texttt{"n"} \} $. Each `move src dst` renames file `src` to `dst`, overwriting `dst` if it exists. Minimize the number of `move` operations.
Samples
Input #1
5
01 0
2 1
2extra 0
3 1
99 0
Output #1
4
move 3 1
move 01 5
move 2extra 4
move 99 3
Input #2
2
1 0
2 1
Output #2
3
move 1 3
move 2 1
move 3 2
Input #3
5
1 0
11 1
111 0
1111 1
11111 0
Output #3
5
move 1 5
move 11 1
move 1111 2
move 111 4
move 11111 3
API Response (JSON)
{
  "problem": {
    "name": "E. Tests Renumeration",
    "description": {
      "content": "The All-Berland National Olympiad in Informatics has just ended! Now Vladimir wants to upload the contest from the Olympiad as a gym to a popular Codehorses website. Unfortunately, the archive with O",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF861E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "The All-Berland National Olympiad in Informatics has just ended! Now Vladimir wants to upload the contest from the Olympiad as a gym to a popular Codehorses website.\n\nUnfortunately, the archive with O...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "全俄罗斯信息学奥林匹克竞赛刚刚结束!现在弗拉基米尔希望将本次竞赛的题目作为训练赛上传至流行的 Codehorses 网站。\n\n不幸的是,竞赛数据的归档文件非常混乱,例如测试文件的命名毫无规律可言。\n\n弗拉基米尔希望将测试文件重命名为从 #cf_span[1] 开始、无间隔的连续整数,即 \"_1_\"、\"_2_\"、...、\"#cf_span[n]',其中 #cf_span[n] 是测试文件的总数。\n\n...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z}^+ $ be the number of files.  \nLet $ F = \\{ (f_i, t_i) \\mid i \\in \\{1, \\dots, n\\} \\} $ be the set of files, where:  \n- $ f_i \\in \\Sigma^* $ is the original file...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments