Codeforces Round #419 (Div. 2) C. Karen and Game
2 seconds
512 megabytes
standard input
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!
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).
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.
3 5
2 2 2 3 2
0 0 0 1 0
1 1 1 2 1
4
row 1
row 1
col 4
row 3
3 3
0 0 0
0 1 0
0 0 0
-1
3 3
1 1 1
1 1 1
1 1 1
3
row 1
row 2
row 3
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的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)
http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...
- Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)
http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...
- 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 ...
- 【找规律】【递推】【二项式定理】Codeforces Round #419 (Div. 1) B. Karen and Test
打个表出来看看,其实很明显. 推荐打这俩组 11 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 1000000000 ...
- 【贪心】 Codeforces Round #419 (Div. 1) A. Karen and Game
容易发现,删除的顺序不影响答案. 所以可以随便删. 如果行数大于列数,就先删列:否则先删行. #include<cstdio> #include<algorithm> usin ...
- Codeforces Round #419 (Div. 2)
1.题目A:Karen and Morning 题意: 给出hh:mm格式的时间,问至少经过多少分钟后,该时刻为回文字符串? 思路: 简单模拟,从当前时刻开始,如果hh的回文rh等于mm则停止累计.否 ...
- Codeforces Round #419 (Div. 2)(B)差分数组
传送门:Problem B https://www.cnblogs.com/violet-acmer/p/9721160.html 题意: Karen有n个关于煮咖啡的食谱,每个食谱都有个煮咖啡的最适 ...
随机推荐
- LED灯珠散热的计算方法
LED灯珠散热的计算方法 来源: 时间:2014-09-23 13:55 [编辑:lufieliu] [字体:大 中 小] 我来说两句 一.热对LED的影响 1.LED是冷光源吗? (1)LED的 ...
- RocketMQ环境搭建
1 源码下载 wget http://mirror.bit.edu.cn/apache/rocketmq/4.2.0/rocketmq-all-4.2.0-bin-release.zip unzip ...
- CF1106F Lunar New Year and a Recursive Sequence 原根、矩阵快速幂、BSGS
传送门 好久没写数论题了写一次调了1h 首先发现递推式是一个乘方的形式,线性递推和矩阵快速幂似乎都做不了,那么是否能够把乘方运算变成加法运算和乘法运算呢? 使用原根!学过\(NTT\)的都知道\(99 ...
- 【Java并发.1】简介
继上一本<深入理解Java虚拟机>之后,学习计划里的另一本书<Java并发编程实战>现在开始学习,并记录学习笔记. 第一章主要内容是介绍 并发 的简介.发展.特点. 编写正确的 ...
- 实例解析forEach、for...in与for...of
在开发过程中经常需要循环遍历数组或者对象,js也为我们提供了不少方法供使用,其中就有三兄弟forEach.for...in.for...of,这三个方法应该是使用频率最高的,但很多人却一值傻傻分不清, ...
- Git版本控制器使用总结性梳理
Git为何物?Git 是什么?大家肯定会说不就是版本控制器嘛,是的Git是目前世界上最先进的分布式版本控制系统(没有之一).1)那什么是版本控制器?举个简单的例子,比如我们用Word写文章,那你一定有 ...
- 《Linux内核分析》期终总结
作者:杨舒雯,原创作品转载请注明出处,<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 目录: 1.通过简 ...
- 作业6-COSPLAY孩子他家长
为了我提高我女儿的数学能力,我以下我会根据我想要的功能做出相应的解决方案,为了孩子,父母也可以想的比老师周到.可怜天下父母心. 编号. 名称. ...
- socket.io框架
socket.io框架 一.问题背景 目前公司在互联网产品上需要程序与前端部分要进行一个实时交互,在进行一定程度上的选型后,决定使用socket.io框架进行一个实践,算是公司的一个新的 尝试,也算是 ...
- SVN上传下载项目
1 本机安装svn服务器 1.1 创建新库 创建分支, Trunk:主干, Tags:分支节点 Branches:分支 比如说我们现在tomcat出了6.0以后已经定版了,但是还得继续开发呀,开 ...