CodeForces 816C 思维
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:
- rowx, (1 ≤ x ≤ n) describing a move of the form "choose the x-th row".
 - colx, (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.
Sample Input
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
Hint
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.
分析这种棋盘的特性,要加加一行,要加加一列,且不能减,且棋盘初始都为零。所以如果同一行上出现了不同的数,则数大的一列必然有该列的加,行同理。
所以可以先处理列,把所有列加过的都还原回去,所以只剩下了由个别的行改变所导致的棋盘,再用同样的方法将行还原回去,最后如果棋盘仍然不为零的话,说明所有的行都进行过相同次数的
加(这里要注意!!!如果最后棋盘不为零的话,既有可能是所有的行进行了操作,也有可能是所有的列进行了操作,因为题目让求最少的操作次数,所以应判断行和列谁小操作的谁)。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; LL con[][];
LL check[][];
LL col[],row[]; int main()
{
LL i,p,j,m,n;
LL flag=;
scanf("%lld%lld",&m,&n);
LL head=,tail=,mid; for(i=; i<=m; i++)
{
for(j=; j<=n; j++)
{
scanf("%lld",&con[i][j]);
if(con[][j]<head)
head=con[][j];
if(con[i][]<tail)
tail=con[i][];
}
} if(m<=n)
{
for(i=; i<=n; i++)
{
if(con[][i]>head)
col[i]+=con[][i]-head;
}
mid=tail-col[];
for(i=; i<=m; i++)
{
if(con[i][]>tail)
row[i]+=con[i][]-tail;
row[i]+=mid;
}
}
else
{
for(i=; i<=m; i++)
{
if(con[i][]>tail)
row[i]+=con[i][]-tail;
}
mid=head-row[];
for(i=; i<=n; i++)
{
if(con[][i]>head)
col[i]+=con[][i]-head;
col[i]+=mid;
}
} flag=; for(i=; i<=n; i++)
{
if(col[i])
{
flag+=col[i];
for(j=; j<=m; j++)
check[j][i]+=col[i];
}
}
for(i=; i<=m; i++)
{
if(row[i])
{
flag+=row[i];
for(j=; j<=n; j++)
check[i][j]+=row[i];
}
} for(i=; i<=m; i++)
{
for(j=; j<=n; j++)
{
if(con[i][j]!=check[i][j])
break;
}
if(j<=n)
{
flag=-;
break;
}
} if(flag==-)
printf("-1\n");
else
{
printf("%lld\n",flag);
for(i=; i<=n; i++)
{
while(col[i])
{
col[i]--;
printf("col %lld\n",i);
}
}
for(i=; i<=m; i++)
{
while(row[i])
{
row[i]--;
printf("row %lld\n",i);
}
}
}
return ;
}
CodeForces 816C 思维的更多相关文章
- Karen and Game CodeForces - 816C (暴力+构造)
		
On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as fo ...
 - Codeforces 424A (思维题)
		
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
 - Codeforces 1060E(思维+贡献法)
		
https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...
 - Queue CodeForces - 353D (思维dp)
		
https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...
 - Codeforces 816C/815A - Karen and Game
		
传送门:http://codeforces.com/contest/816/problem/C 本题是一个模拟问题. 有一个n×m的矩阵.最初,这个矩阵为零矩阵O.现有以下操作: a.行操作“row ...
 - 【codeforces 816C】Karen and Game
		
[题目链接]:http://codeforces.com/contest/816/problem/C [题意] 给你一个n*m的矩阵; 一开始所有数字都是0; 每次操作,你能把某一行,或某一列的数字全 ...
 - codeforces 1244C (思维 or 扩展欧几里得)
		
(点击此处查看原题) 题意分析 已知 n , p , w, d ,求x , y, z的值 ,他们的关系为: x + y + z = n x * w + y * d = p 思维法 当 y < w ...
 - CodeForces - 417B (思维题)
		
Crash Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
 - CodeForces - 417A(思维题)
		
Elimination Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
 
随机推荐
- Python入门:条件控制
			
条件控制其实就是if...else...(如果...条件是成立的,就做...:反之,就做...)的使用,其基本结构是: 具体看下面这个例子: def account_login(): # 定义函数 p ...
 - cxDBVerticalGrid
			
定位在第一行并显示内置编辑器 cxDBVerticalGrid1.FocusedRow := cxDBVerticalGrid1.Rows[0]; cxDBVerticalGrid1.ShowEdit ...
 - Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)
			
文章转自:http://www.ibm.com/developerworks/cn/java/j-lo-keyboard/
 - [转帖]HR职能划分三支柱模型
			
HR职能划分三支柱模型 http://blog.sina.com.cn/s/blog_afbd12640101o5hf.html COE(Centre of Excellence or Center ...
 - [转帖]HTTPS系列干货(一):HTTPS 原理详解
			
HTTPS系列干货(一):HTTPS 原理详解 https://tech.upyun.com/article/192/HTTPS%E7%B3%BB%E5%88%97%E5%B9%B2%E8%B4%A7 ...
 - IDEA 开发工具的快捷键
			
IDEA 开发工具的快捷键 原文链接:http://blog.csdn.net/wfp458113181wfp/article/details/24579781 1.文本编辑 删除 ctr + ...
 - iOS BCD码、数据流、字节和MD5计算
			
一.各个之间的相互转换 1.字符串转数据流NSData NSString *str = @"abc123"; NSData *dd = [str dataUsingEncoding ...
 - bzoj2095-Bridge
			
题意 一个 \(n\) 个点 \(m\) 条边的图,每条边双向都有权值(可能不一样).求从 1 开始,经过所有点,经过所有边一次且仅一次(即一定要经过这条边的某个方向)回到 1 的路径上权值最大的最小 ...
 - springmvc+mybatis 实现分页查询
			
为简化分页功能,设计了一个分页的JSP标签,只需要在页面使用分页标签,就可以完成所有页面的分页功能. 1. 项目结构和数据库设计 (1) 项目结构: (2) 数据库设计 2. PageModel.ja ...
 - 对\${ctx}的一点理解
			
一.\${ctx}与${pageContext.request.contextPath}的区别 相同点: \${ctx}和\${pageContext.request.contextPath}都是获取 ...