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 ...
随机推荐
- 基于ABP框架的权限设置
需求:在界面展示中,"定向包管理","竞价管理","竞拍管理","发布定向资源","添加竞价资源", ...
- 【css笔记】css中的盒模型和三种定位机制(固定定位,绝对定位,浮动)
html页面上的元素都可以看成是框组成的,框通过三种定位机制排列在一起就过程了我们看到的页面.而框就是盒模型. 盒模型 1.页面上的每个元素可以看成一个矩形框,每个框由元素的内容,内边距,边框和外边距 ...
- Linux之定时任务
定时任务Crond介绍 Crond是linux系统中用来定期执行命令/脚本或指定程序任务的一种服务或软件,一般情况下,我们安装完Centos5/6 linux操作系统之后,默认便会启动Crond任务调 ...
- Spring Boot Dubbo applications.properties 配置清单
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 与其纠结,不如行动学习.Innovate ,And out execute ! 』 本文 ...
- httpd配置ResponseHeader
今天遇到一个问题:我把项目编译后的静态文件发布到开发机上,开发机使用httpd启的静态文件服务,页面的访问是在特制的壳浏览器里面,我更新了代码后,发现页面被缓存了,找到壳的RD联调了一下,发现我的主页 ...
- GitExtensions-2.48安装详细教程
在安装GitExtensions时你可能遇到如下问题,如果出现此提示,则先退出安装,去下载安装.NET Framework4.0之后,再启动GitExtension的安装. 开始进行安装: 安装完成, ...
- poj2253 Frogger Dijkstra变形
题目链接:http://poj.org/problem?id=2253 就是求所有路径的最大边权值的最小值 处理时每次找出距离当前的已选的节点的最短距离,然后更新每个未选节点的值 代码: #inclu ...
- HiveHbase集成实践
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl/p/6857891.html 转载请注明出处 简单的说就是可以通过Hive SQL直接对hbase的表进行读写操 ...
- python selenium 环境搭建(一)
elenium 虽然过了这么多年,但是到目前为止依然是比较流行的自动化框架了,还有很多的初学者在学习,所以根据自己的时间将把相关的资料汇总一下,下面首先我们需要搭建一下基础环境. 首先自己本身比较笨, ...
- bitnami gitlab 安装
安装gitlab需要安装的依赖软件比较多,基于偷懒的原则,从网上找到了bitnami-gitlab-8.7.1-0-linux-x64-installer.run ,集成了所有的相关软件,一键安装,省 ...