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 ...
随机推荐
- 调用startActivityForResult后,onActivityResult为什么立刻响应
现象 今天在编写代码的时候,涉及到两个Activity通过Intent来传值的问题.具体描述为:activity A调用startActivityForResult()函数启动Activit ...
- 【JavaScript】让事件支持先发布后订阅
之前写过一个的事件管理器,就是普通的先订阅后发布模式.但实际场景中我们需要做到后订阅的也能收到发布的消息.比如我们关注微信公众号,还是能看到历史消息的.类似于qq离线消息,我先发给你,你登录了就能收到 ...
- pygame开发滑雪者游戏
pygame开发滑雪者游戏 一.实验说明 下述介绍为实验楼默认环境,如果您使用的是定制环境,请修改成您自己的环境介绍. 1. 环境登录 无需密码自动登录,系统用户名 shiyanlou,该用户具备 s ...
- jQuery遍历节点方法汇总
1.children()方法:$('div').children()---遍历查找div元素的所有子元素节点 <p>Hello</p> <div> <span ...
- 记录——时间轮定时器(lua 实现)
很长一段时间里,我错误的认识了定时器.无意中,我发现了“时间轮”这个名词,让我对定时器有了新的看法. 我错误的认为,定时器只需要一个 tick 队列,按指定的时间周期遍历队列,检查 tick 倒计时满 ...
- Socket中的异常和参数设置
1.常见异常 1.java.net.SocketTimeoutException . 这个异 常比较常见,socket 超时.一般有 2 个地方会抛出这个,一个是 connect 的 时 候 , 这 ...
- JavaScript、Python、java、Go算法系列之【快速排序】篇
常见的内部排序算法有:插入排序.希尔排序.选择排序.冒泡排序.归并排序.快速排序.堆排序.基数排序等. 用一张图概括: 选择排序 选择排序是一种简单直观的排序算法,无论什么数据进去都是 O(n²) 的 ...
- 关于cgi、FastCGI、php-fpm、php-cgi
搞了好长时间的php了,突然有种想法,想把这些整理在一起,于是查看各种资料,找到一片解释的很不错的文章,分享一下-- 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式 ...
- 开发一个Servlet示例
Servlet响应请求步骤: Servlet是一个基于Java技术的Web组件,运行在服务器端,用户利用Servlet可以很轻松地扩展Web服务器的功能,使其满足特定的应用需要.Tomcat是一个常用 ...
- prop()、attr()和data()
设置元素属性,用attr()还是prop()? 对于取值为true /false的属性,如 checked/selected/readonly或者disabled,使用prop(),其他属性使用 at ...