C. Karen and Game

Codeforces
IDCF816C
Time2000ms
Memory512MB
Difficulty
brute forcegreedyimplementation
English · Original
Chinese · Translation
Formal · Original
On the way to school, Karen became fixated on the puzzle game on her phone! <center>![image](https://espresso.codeforces.com/ddb523e5319d3e419119c3679bdc76340ed480e8.png)</center>The game is played as follows. In each level, you have a grid with _n_ rows and _m_ columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the _i_\-th row and _j_\-th column should be equal to _g__i_, _j_. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! ## Input The first line of input contains two integers, _n_ and _m_ (1 ≤ _n_, _m_ ≤ 100), the number of rows and the number of columns in the grid, respectively. The next _n_ lines each contain _m_ integers. In particular, the _j_\-th integer in the _i_\-th of these rows contains _g__i_, _j_ (0 ≤ _g__i_, _j_ ≤ 500). ## Output If there is an error and it is actually not possible to beat the level, output a single integer _\-1_. Otherwise, on the first line, output a single integer _k_, the minimum number of moves necessary to beat the level. The next _k_ lines should each contain one of the following, describing the moves in the order they must be done: * _row_ _x_, (1 ≤ _x_ ≤ _n_) describing a move of the form "choose the _x_\-th row". * _col_ _x_, (1 ≤ _x_ ≤ _m_) describing a move of the form "choose the _x_\-th column". If there are multiple optimal solutions, output any one of them. [samples] ## Note In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: <center>![image](https://espresso.codeforces.com/9725553e228c387c0f12702a054f3a965a7b0805.png)</center>In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: <center>![image](https://espresso.codeforces.com/fd3281c5d83a863405f380c56f861f780a802d8c.png)</center>Note that this is not the only solution; another solution, among others, is _col 1_, _col 2_, _col 3_.
在去学校的路上,Karen 被她手机上的拼图游戏吸引了! 游戏规则如下。在每一关中,你有一个包含 #cf_span[n] 行和 #cf_span[m] 列的网格。每个单元格最初包含数字 #cf_span[0]。 一次操作包括选择一行或一列,并将该行或列中的所有单元格的值增加 #cf_span[1]。 要赢得该关卡,在所有操作完成后,位于第 #cf_span[i] 行和第 #cf_span[j] 列的单元格中的数字必须等于 #cf_span[gi, j]。 Karen 目前卡在某一关,她想知道如何用最少的操作次数通过这一关。请帮她解决这个问题! 输入的第一行包含两个整数 #cf_span[n] 和 #cf_span[m](#cf_span[1 ≤ n, m ≤ 100]),分别表示网格的行数和列数。 接下来的 #cf_span[n] 行,每行包含 #cf_span[m] 个整数。特别地,第 #cf_span[i] 行中的第 #cf_span[j] 个整数为 #cf_span[gi, j](#cf_span[0 ≤ gi, j ≤ 500])。 如果存在错误,实际上无法通过该关卡,请输出一个整数 _-1_。 否则,在第一行输出一个整数 #cf_span[k],表示通过该关卡所需的最少操作次数。 接下来的 #cf_span[k] 行,每行应描述一个操作(按执行顺序): 如果存在多个最优解,输出其中任意一个即可。 在第一个测试用例中,Karen 有一个包含 #cf_span[3] 行和 #cf_span[5] 列的网格。她可以通过以下 #cf_span[4] 次操作通过该关卡: 在第二个测试用例中,Karen 有一个包含 #cf_span[3] 行和 #cf_span[3] 列的网格。显然,无法通过该关卡;执行任何操作都会在网格上产生三个 #cf_span[1],但题目要求中心位置只有一个 #cf_span[1]。 在第三个测试用例中,Karen 有一个包含 #cf_span[3] 行和 #cf_span[3] 列的网格。她可以通过以下 #cf_span[3] 次操作通过该关卡: 请注意,这并非唯一解;其他解之一是 _col 1_、_col 2_、_col 3_。 ## Input 输入的第一行包含两个整数 #cf_span[n] 和 #cf_span[m](#cf_span[1 ≤ n, m ≤ 100]),分别表示网格的行数和列数。接下来的 #cf_span[n] 行,每行包含 #cf_span[m] 个整数。特别地,第 #cf_span[i] 行中的第 #cf_span[j] 个整数为 #cf_span[gi, j](#cf_span[0 ≤ gi, j ≤ 500])。 ## Output 如果存在错误,实际上无法通过该关卡,请输出一个整数 _-1_。否则,在第一行输出一个整数 #cf_span[k],表示通过该关卡所需的最少操作次数。接下来的 #cf_span[k] 行,每行应描述一个操作(按执行顺序): _row_ #cf_span[x],(#cf_span[1 ≤ x ≤ n])表示选择第 #cf_span[x] 行的操作。 _col_ #cf_span[x],(#cf_span[1 ≤ x ≤ m])表示选择第 #cf_span[x] 列的操作。 如果存在多个最优解,输出其中任意一个即可。 [samples] ## Note 在第一个测试用例中,Karen 有一个包含 #cf_span[3] 行和 #cf_span[5] 列的网格。她可以通过以下 #cf_span[4] 次操作通过该关卡: 在第二个测试用例中,Karen 有一个包含 #cf_span[3] 行和 #cf_span[3] 列的网格。显然,无法通过该关卡;执行任何操作都会在网格上产生三个 #cf_span[1],但题目要求中心位置只有一个 #cf_span[1]。 在第三个测试用例中,Karen 有一个包含 #cf_span[3] 行和 #cf_span[3] 列的网格。她可以通过以下 #cf_span[3] 次操作通过该关卡: 请注意,这并非唯一解;其他解之一是 _col 1_、_col 2_、_col 3_。
**Definitions** Let $ n, m \in \mathbb{Z}^+ $ denote the number of rows and columns, respectively. Let $ G = (g_{i,j}) \in \mathbb{Z}^{n \times m} $ be the target grid, where $ 0 \leq g_{i,j} \leq 500 $. Let $ r_i \in \mathbb{Z}_{\geq 0} $ denote the number of times row $ i $ is incremented. Let $ c_j \in \mathbb{Z}_{\geq 0} $ denote the number of times column $ j $ is incremented. **Constraints** For all $ i \in \{1, \dots, n\} $, $ j \in \{1, \dots, m\} $: $$ g_{i,j} = r_i + c_j $$ **Objective** Minimize the total number of moves: $$ \sum_{i=1}^n r_i + \sum_{j=1}^m c_j $$ subject to the above constraints. If no non-negative integer solution $ (r_1, \dots, r_n, c_1, \dots, c_m) $ exists, output $-1$. Otherwise, output the minimal number of moves and a sequence of moves (row or column increments) achieving it.
Samples
Input #1
3 5
2 2 2 3 2
0 0 0 1 0
1 1 1 2 1
Output #1
4
row 1
row 1
col 4
row 3
Input #2
3 3
0 0 0
0 1 0
0 0 0
Output #2
\-1
Input #3
3 3
1 1 1
1 1 1
1 1 1
Output #3
3
row 1
row 2
row 3
API Response (JSON)
{
  "problem": {
    "name": "C. Karen and Game",
    "description": {
      "content": "On the way to school, Karen became fixated on the puzzle game on her phone! <center>![image](https://espresso.codeforces.com/ddb523e5319d3e419119c3679bdc76340ed480e8.png)</center>The game is played a",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF816C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "On the way to school, Karen became fixated on the puzzle game on her phone!\n\n<center>![image](https://espresso.codeforces.com/ddb523e5319d3e419119c3679bdc76340ed480e8.png)</center>The game is played a...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "在去学校的路上,Karen 被她手机上的拼图游戏吸引了!\n\n游戏规则如下。在每一关中,你有一个包含 #cf_span[n] 行和 #cf_span[m] 列的网格。每个单元格最初包含数字 #cf_span[0]。\n\n一次操作包括选择一行或一列,并将该行或列中的所有单元格的值增加 #cf_span[1]。\n\n要赢得该关卡,在所有操作完成后,位于第 #cf_span[i] 行和第 #cf_span[j...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, m \\in \\mathbb{Z}^+ $ denote the number of rows and columns, respectively.  \nLet $ G = (g_{i,j}) \\in \\mathbb{Z}^{n \\times m} $ be the target grid, where $ 0 \\leq g_{i,j} \\leq...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments