codeforces round #419 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.
题解:
简单贪心,看似有很多决策,其实并不存在最优解,只需一行一列的加即可
被hack的时候发现了刚开始加行和加列有区别 然后强行改对
但神tm a[j][i]写成a[i][j] 挂了一个点
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int a[N][N],b[N][N],Lm[N],Lx[N],rm[N],rx[N],sum=,n,m,ans1=,ans2=,ansi[N],ansj[N],ansi2[N],ansj2[N];
void work()
{
for(int i=;i<=m;i++)
{
ansj[i]=rm[i];sum-=rm[i]*n;ans1+=rm[i];
for(int j=;j<=n;j++)
{
a[j][i]-=rm[i];
if(a[j][i]<Lm[j])Lm[j]=a[j][i];
}
}
for(int i=;i<=n;i++)
{
ansi[i]=Lm[i];sum-=Lm[i]*m;ans1+=Lm[i];
}
for(int i=;i<=n;i++)Lm[i]=Lx[i],rm[i]=rx[i];
for(int i=;i<=n;i++)
{
ansi2[i]=Lm[i];ans2+=Lm[i];
for(int j=;j<=m;j++)
{
b[i][j]-=Lm[i];
if(b[i][j]<rm[j])rm[j]=b[i][j];
}
}
for(int i=;i<=m;i++)
{
ansj2[i]=rm[i];ans2+=rm[i];
}
if(sum)printf("-1");
else
{
if(ans1<ans2)
{
printf("%d\n",ans1);
for(int i=;i<=n;i++)for(int j=;j<=ansi[i];j++)printf("row %d\n",i);
for(int j=;j<=m;j++)for(int i=;i<=ansj[j];i++)printf("col %d\n",j);
}
else
{
printf("%d\n",ans2);
for(int i=;i<=n;i++)for(int j=;j<=ansi2[i];j++)printf("row %d\n",i);
for(int j=;j<=m;j++)for(int i=;i<=ansj2[j];i++)printf("col %d\n",j);
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
Lm[i]=2e8;
for(int j=;j<=m;j++)
{
scanf("%d",&a[i][j]);
sum+=a[i][j];b[i][j]=a[i][j];
if(a[i][j]<Lm[i])Lm[i]=a[i][j],Lx[i]=a[i][j];
}
}
for(int i=;i<=m;i++){rm[i]=2e8;for(int j=;j<=n;j++)if(a[j][i]<rm[i])rm[i]=a[j][i],rx[i]=a[j][i];}
work();
return ;
}
codeforces round #419 C. Karen and Game的更多相关文章
- Codeforces Round #419 D. Karen and Test
Karen has just arrived at school, and she has a math test today! The test is about basic addition an ...
- codeforces round #419 E. Karen and Supermarket
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...
- codeforces round #419 B. Karen and Coffee
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- codeforces round #419 A. Karen and Morning
Karen is getting ready for a new school day! It is currently hh:mm, given in a 24-hour format. As yo ...
- 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) C. Karen and Game
C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
- 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 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...
随机推荐
- Beta冲刺 第一天
Beta冲刺 第一天 1. 昨天的困难 由于今天还是第一天,所以暂时没有昨天的困难. 2. 今天解决的进度 潘伟靖: 对代码进行了review 1.将某些硬编码改为软编码 2.合并了一些方法,简化代码 ...
- 操作 numpy 数组的常用函数
操作 numpy 数组的常用函数 where 使用 where 函数能将索引掩码转换成索引位置: indices = where(mask) indices => (array([11, 12, ...
- Log4j详细教程
一.入门实例 1.新建一个JAva工程,导入包log4j-1.2.17.jar,整个工程最终目录如下 2.src同级创建并设置log4j.properties ### 设置### log4j.root ...
- js 防止重复点击
1.添加flag 适用于ajax 表单提交,提交之前flag = false , 提及中,true ,提交后false 2.事件重复点击: <script> var throttle = ...
- Python内置函数(52)——getattr
英文文档: getattr(object, name[, default]) Return the value of the named attribute of object. name must ...
- Netty事件监听和处理(下)
上一篇 介绍了事件监听.责任链模型.socket接口和IO模型.线程模型等基本概念,以及Netty的整体结构,这篇就来说下Netty三大核心模块之一:事件监听和处理. 前面提到,Netty是一个NIO ...
- Linux进程管理:后台启动进程和任务管理命令
一.为什么要使程序在后台执行 我们的应用有时候要运行时间很长,如:几个小时甚至几个星期,我们可以让程序在后台一直跑. 让程序在后台运行的好处有: 终端关机不影响后台进程的运行.(不会终端一关机或者网络 ...
- HTML5的常用新特性你必须知道
HTML5的常用新特性你必须知道 1 新的 声明 HTML 有多个不同的版本,只有完全明白页面中使用的确切 HTML 版本,浏览器才能完全正确地显示出 HTML 页面.这就是 的用处. 不是 HTML ...
- windows server 2016远程桌面进去,英文系统修改语言
由于我这边已经是改好了,以下截图来自中文版. 这边选了中文,然后点options. 选择:使该语言成为主要语言,保存. 会提示需要退出登录. 过一会重新登录,ok.
- Orm之中介模型
什么是中介模型 中介模型针对的是ManyToMany(多对多)的时候第三张表的问题, 中介模型其实指的就是我们不通过Django创建第三张表,如果自己不创建第三张表,而是由django给我们创建,那就 ...