C. Karen and Game
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

On the way to school, Karen became fixated on the puzzle game on her phone!

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 gi, 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 gi, j (0 ≤ gi, 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.

Examples
input
3 5
2 2 2 3 2
0 0 0 1 0
1 1 1 2 1
output
4
row 1
row 1
col 4
row 3
input
3 3
0 0 0
0 1 0
0 0 0
output
-1
input
3 3
1 1 1
1 1 1
1 1 1
output
3
row 1
row 2
row 3
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:

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:

Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.

 题意:

给定n行m列数字,每次可以让一行或一列都减一,求出让全部数字全为0的最小的次数,没有则输出-1;

思路:

就是对行和列处理,注意下n和m的大小就行。

第一次比赛打了三道。。。上了一波大分。

#include <bits/stdc++.h>

using namespace std;
const int N = ; int n, m, a[N][N], c[N], r[N], ans; int check() {
for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
if (a[i][j]) return ;
return ;
} void row_man() {
for (int i = ; i < n; i++) {
int mi = a[i][];
for (int j = ; j < m; j++) mi = min(mi, a[i][j]);
r[i] = mi;
ans += r[i];
for (int j = ; j < m; j++) a[i][j] -= mi;
}
} void col_man() {
for (int j = ; j < m; j++) {
int mi = a[][j];
for (int i = ; i < n; i++) mi = min(mi, a[i][j]);
c[j] = mi;
ans += c[j];
for (int i = ; i < n; i++) a[i][j] -= mi;
}
} int main() {
cin >> n >> m;
for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
scanf("%d", &a[i][j]);
if (n > m) {
col_man();
row_man();
} else {
row_man();
col_man();
}
if (!check()) puts("-1");
else {
cout << ans << endl;
for (int i = ; i < n; i++)
for (int j = ; j < r[i]; j++)
printf("row %d\n", i + );
for (int i = ; i < m; i++)
for (int j = ; j < c[i]; j++)
printf("col %d\n", i + );
}
}

Codeforces Round #419 (Div. 2) C. Karen and Game的更多相关文章

  1. Codeforces Round #419 (Div. 2) B. Karen and Coffee(经典前缀和)

    http://codeforces.com/contest/816/problem/B To stay woke and attentive during classes, Karen needs s ...

  2. Codeforces Round #419 (Div. 2) B. Karen and Coffee

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  3. Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)

    http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...

  4. Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)

    http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...

  5. Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP

    C. Karen and Supermarket     On the way home, Karen decided to stop by the supermarket to buy some g ...

  6. 【找规律】【递推】【二项式定理】Codeforces Round #419 (Div. 1) B. Karen and Test

    打个表出来看看,其实很明显. 推荐打这俩组 11 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 1000000000 ...

  7. 【贪心】 Codeforces Round #419 (Div. 1) A. Karen and Game

    容易发现,删除的顺序不影响答案. 所以可以随便删. 如果行数大于列数,就先删列:否则先删行. #include<cstdio> #include<algorithm> usin ...

  8. Codeforces Round #419 (Div. 2)

    1.题目A:Karen and Morning 题意: 给出hh:mm格式的时间,问至少经过多少分钟后,该时刻为回文字符串? 思路: 简单模拟,从当前时刻开始,如果hh的回文rh等于mm则停止累计.否 ...

  9. Codeforces Round #419 (Div. 2)(B)差分数组

    传送门:Problem B https://www.cnblogs.com/violet-acmer/p/9721160.html 题意: Karen有n个关于煮咖啡的食谱,每个食谱都有个煮咖啡的最适 ...

随机推荐

  1. jdk1.8安装后查看Java -version出错。

    最近在电脑行安装了多个jdk的版本 分别是jdk1.6,jdk1.7,jdk1.8三个版本,在配置环境变量的时候,选择的是jdk1.7; 但是奇怪的是,当我在cmd中输入java -version后, ...

  2. LOJ500 ZQC的拼图 二分答案、DP

    传送门 题意:给出$N$个直角三角形拼图和$M \times M$的网格,第$i$个直角三角形水平直角边边长为$\frac{1}{a_i}$,垂直直角边边长为$\frac{1}{b_i},$规定直角三 ...

  3. 51Nod 1443 路径和树

    还是一道很简单的基础题,就是一个最短路径树的类型题目 我们首先可以发现这棵树必定满足从1出发到其它点的距离都是原图中的最短路 换句话说,这棵树上的每一条边都是原图从1出发到其它点的最短路上的边 那么直 ...

  4. 【JVM.11】Java内存模型与线程

    鲁迅曾经说过“并发处理的广泛应用是使得Amdahl定律代替摩尔定律成为计算机性能发展源动力的根本原因,也是人类‘压榨‘ 计算机运行能力的最有力武器.” 一.概述 多任务处理在现代计算机操作系统中几乎已 ...

  5. Http指南(3)

    Web主机托管 主机托管服务 虚拟主机托管:许多Web托管者通过让一些顾客共享一台计算机来提供便宜的Web主机托管服务.这称为共享主机托管或虚拟主机托管 虚拟服务器请求缺乏主机信息: 不幸的是,HTT ...

  6. Facebook React 和 Web Components(Polymer)对比优势和劣势

    目录结构 译者前言 Native vs. Compiled 原生语言对决预编译语言 Internal vs. External DSLs 内部与外部 DSLs 的对决 Types of DSLs - ...

  7. BugkuCTF sql注入

    前言 写了这么久的web题,算是把它基础部分都刷完了一遍,以下的几天将持续更新BugkuCTF WEB部分的题解,为了不影响阅读,所以每道题的题解都以单独一篇文章的形式发表,感谢大家一直以来的支持和理 ...

  8. Linux系统本地yum源环境配置记录

    由于IDC的一些服务器没有外网,不能对外访问.所以打算部署一套内网的yum源环境,以供内网服务器使用.以下简单记录下操作过程: 1)下载centos6.9和centos7.3的镜像,并挂载 [root ...

  9. LVM基础详细说明及动态扩容lvm逻辑卷的操作记录

    LVM概念:---------------------------------------------------------------------------------------------- ...

  10. Individual P1: Preparation

    Individual Project - Word frequency program tally the frequency of words under a directory (2 modes) ...