C. Karen and Game
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.
题解:
这道题首先想过网络流,然而没有什么用。
后来发现贪心可以过,求出每行每列的最小值,然后一个一个找,从行开始,每次维护一下列的最小值,最后统计一下值的总合是不是和之前的相同,不是的话就说明还有残余,如样例2。是的话就输出结果。
注:这个代码有问题,正解在最下面
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int s[][],n,m,sum,sum2;
int mminn[],mminm[];
int ans1[],cnt,ans2[];
int main()
{
int i,j;
memset(mminn,/,sizeof(mminn));
memset(mminm,/,sizeof(mminm));
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
scanf("%d",&s[i][j]);
sum+=s[i][j];
}
}
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
mminn[i]=min(mminn[i],s[i][j]);
}
for(j=;j<=m;j++)
{
for(i=;i<=n;i++)
mminm[j]=min(mminm[j],s[i][j]);
}
for(i=;i<=n;i++)
{
ans1[i]=mminn[i];
for(j=;j<=m;j++)
{
s[i][j]-=mminn[i];
mminm[j]=min(mminm[j],s[i][j]);
}
}
for(j=;j<=m;j++)
{
ans2[j]=mminm[j];
}
for(i=;i<=n;i++)
{
sum2+=ans1[i]*m;
}
for(i=;i<=m;i++)
{
sum2+=ans2[i]*n;
}
if(sum==sum2)
{
for(i=;i<=n;i++)
if(ans1[i])cnt+=ans1[i];
for(i=;i<=m;i++)
if(ans2[i])cnt+=ans2[i];
printf("%d\n",cnt);
for(i=;i<=n;i++)
{
for(j=;j<=ans1[i];j++)
printf("row %d\n",i);
}
for(i=;i<=m;i++)
{
for(j=;j<=ans2[i];j++)
printf("col %d\n",i);
}
}
else cout<<"-1";
return ;
}
没错,这份代码确实有问题,很显然随便设计一组测试数据
input
4 3
1 1 1
1 1 1
1 1 1
1 1 1
output
3
col 1
col 2
col 3
而上面这份代码会输出
output
4
row 1
row 2
row 3
row 4
然后就WA掉了,(心痛)。但是修改也很简单,最后判断一下是从行还是从列开始就可以了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int s[][],n,m,sum,sum2;
int mminn[],mminm[];
int ans1[],cnt,ans2[];
int main()
{
int i,j;
memset(mminn,/,sizeof(mminn));
memset(mminm,/,sizeof(mminm));
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
scanf("%d",&s[i][j]);
sum+=s[i][j];
}
}
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
mminn[i]=min(mminn[i],s[i][j]);
}
for(j=;j<=m;j++)
{
for(i=;i<=n;i++)
mminm[j]=min(mminm[j],s[i][j]);
}
if(m>n)
{
for(i=;i<=n;i++)
{
ans1[i]=mminn[i];
for(j=;j<=m;j++)
{
s[i][j]-=mminn[i];
mminm[j]=min(mminm[j],s[i][j]);
}
}
for(j=;j<=m;j++)
{
ans2[j]=mminm[j];
}
for(i=;i<=n;i++)
{
sum2+=ans1[i]*m;
}
for(i=;i<=m;i++)
{
sum2+=ans2[i]*n;
}
if(sum==sum2)
{
for(i=;i<=n;i++)
if(ans1[i])cnt+=ans1[i];
for(i=;i<=m;i++)
if(ans2[i])cnt+=ans2[i];
printf("%d\n",cnt);
for(i=;i<=n;i++)
{
for(j=;j<=ans1[i];j++)
printf("row %d\n",i);
}
for(i=;i<=m;i++)
{
for(j=;j<=ans2[i];j++)
printf("col %d\n",i);
}
}
else cout<<"-1";
}
else
{
for(i=;i<=m;i++)
{
ans2[i]=mminm[i];
for(j=;j<=n;j++)
{
s[j][i]-=mminm[i];
mminn[j]=min(mminn[j],s[j][i]);
}
}
for(j=;j<=n;j++)
{
ans1[j]=mminn[j];
}
for(i=;i<=n;i++)
{
sum2+=ans1[i]*m;
}
for(i=;i<=m;i++)
{
sum2+=ans2[i]*n;
}
if(sum==sum2)
{
for(i=;i<=n;i++)
if(ans1[i])cnt+=ans1[i];
for(i=;i<=m;i++)
if(ans2[i])cnt+=ans2[i];
printf("%d\n",cnt);
for(i=;i<=n;i++)
{
for(j=;j<=ans1[i];j++)
printf("row %d\n",i);
}
for(i=;i<=m;i++)
{
for(j=;j<=ans2[i];j++)
printf("col %d\n",i);
}
}
else cout<<"-1";
}
return ;
}
C. Karen and Game的更多相关文章
- B. Karen and Coffee
B. Karen and Coffee time limit per test 2.5 seconds memory limit per test 512 megabytes input standa ...
- A. Karen and Morning
A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes input standa ...
- CodeForces 816B Karen and Coffee(前缀和,大量查询)
CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...
- codeforces 815C 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 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 #460 D. Karen and Cards
Description Karen just got home from the supermarket, and is getting ready to go to sleep. After tak ...
- codeforces round #419 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 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 ...
随机推荐
- poj 1056 IMMEDIATE DECODABILITY 字典树
题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...
- Linux增加磁盘操作
首先,增加磁盘分为4个大步骤:1.插上硬盘:2.分区;3.格式化4.挂载,然后分别说说以上四步的具体事项和注意内容. 1.插上硬盘(本位以虚拟机为例) 新买来一块磁盘,把磁盘插到主板上.虚拟机中操作如 ...
- synchronized 修饰在 static方法和非static方法的区别
Java中synchronized用在静态方法和非静态方法上面的区别 在Java中,synchronized是用来表示同步的,我们可以synchronized来修饰一个方法.也可以synchroniz ...
- 【linux命令】打开关闭防火墙iptables
防火墙关闭 关闭防火墙(linux) 经过自己的实验,发现在ubuntu中service iptables 无法使用. 同时,在init.d中并没有iptables的程序,iptables程序在/sb ...
- Openstack(企业私有云)万里长征第一步——安装
一.前言 单位新进了十几台服务器,建了一个高标准的一体化机房,状似刘姥姥进大观园的我,从机房规划到企业私有云搭建一一重头学来,除了机房泥墙其他基本都涉猎到了. 从企业私有云这个名字就能看出这是多么复杂 ...
- webapi “ObjectContent`1”类型未能序列化内容类型“application/xml; charset=utf-8”的响应正文。
今天在来一发 webapi的一个知识点 相信用过webapi的对这个错误 已经看在眼里 痛在心里了把 我百度也搜了一下 看了一下 然后发现他们的解决办法 并没有什么软用. 然后想起来当时上学的 ...
- 面试(2)-java-se-HashSet和TreeSet
Set是java中一个不包含重复元素的collection.更正式地说,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素.正如其名称所暗示的, ...
- sublime工具篇
sublime快捷键的应用 熟悉掌握sublime快捷键,提高编码效率,享受编码乐趣. window操作系统常用快捷键 win+D:快速显示桌面 win+方向键:最大化最小化窗口 win+L ...
- springboot + shiro + cas4.2.7 实战
1. 下载地址 https://github.com/apereo/cas/archive/v4.2.7.zip 2. 解压后, 用intellj idea 打开 3. 执行 gradle build ...
- AutoMapper总结
AutoMapper是一个对象和对象间的映射器.对象与对象的映射是通过转变一种类型的输入对象为一种不同类型的输出对象工作的.让AutoMapper有意思的地方在于它提供了一些将类型A映射到类型B这种无 ...