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.

题解:

简单贪心,看似有很多决策,其实并不存在最优解,只需一行一列的加即可

被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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. codeforces round #419 B. Karen and Coffee

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

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

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

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

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

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

随机推荐

  1. Beta版本展示

    Beta版本展示 开发团队:MyGod 团队成员:程环宇 张芷祎 王田路 张宇光 王婷婷 源码地址:https://github.com/WHUSE2017/MyGod MyGod团队项目的目标: 让 ...

  2. 20162328蔡文琛week04

    学号 20162328 <程序设计与数据结构>第4周学习总结 教材学习内容总结 本周学习了第四章和第七章,第四章中的内容已经有了初步定的掌握,布尔表达式的运用,是条件和循环语句的基础及数组 ...

  3. 【iOS】Swift if let 和 if var

    if let unwrappedOptional = postDict { print("The optional has a value! It's \(unwrappedOptional ...

  4. 同一个页面同时拥有collectionView和navigationBar和tabBar时可能遇到的问题

    写一个页面的时候,遇到了页面加载时候collectionView的最下面少了49个像素的位置,切换去别的页面之后,再返回,又变回正常,多方求解无果后,发现原来是系统自带的适应功能导致的,加入以下代码即 ...

  5. vue的简单tab

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  6. Gitlab的安装及项目新建

    1. Gitlab的安装及仓库创建 1.1下载gitlab安装包 1).官网下载速度较慢 建议先行下载 国内的源里面可以找到最新的版本https://mirrors.tuna.tsinghua.edu ...

  7. linux下xargs和管道的区别

    管道将前面的标准输出作为后面的标准输入,xargs则将标准输入作为命令的参数 一.简介 1.背景 之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了x ...

  8. jedis配置

    public interface IJedisClientFactory { Jedis getJedis(); } JedisClientFactoryImpl.java @Service publ ...

  9. 设置Nginx+php-fpm显示错误信息

    Begin 最近在用PHP写后台程序,但是有错误不会显示简直坑爹,全都是200这样的错误代码而已= =... 于是 于是就搜索如何打开错误显示,然后就在博客里面记录一下 修改配置文件 /etc/php ...

  10. 三、如何使用QtDesigner

    三.如何使用QtDesigner 启动 QtDesigner,创建一个PyQt项目 拖动Label到主窗体,双击并输入自己想输入的文字 并保持为 HelloWorld.ui 此时在你Python项目下 ...