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 ...
随机推荐
- sublime Text 插件收录
插件 1.SublimeText3常用快捷键和优秀插件 2.常用的sublime text 3插件-1 主题 1.https://www.jianshu.com/p/1a1113213faf 2.ht ...
- ACM数论之旅11---浅谈指数与对数(长篇)(今天休息,不学太难的数论> 3<)
c/c++语言中,关于指数,对数的函数我也就知道那么多 exp(),pow(),sqrt(),log(),log10(), exp(x)就是计算e的x次方,sqrt(x)就是对x开根号 pow()函数 ...
- BZOJ5286 HNOI/AHOI2018转盘(分块/线段树)
显然最优走法是先一直停在初始位置然后一次性走完一圈.将序列倍长后,相当于找一个长度为n的区间[l,l+n),使其中ti+l+n-1-i的最大值最小.容易发现ti-i>ti+n-(i+n),所以也 ...
- 关于BIO和NIO的理解
摘要: 关于BIO和NIO的理解 最近大概看了ZooKeeper和Mina的源码发现都是用Java NIO实现的,所以有必要搞清楚什么是NIO.下面是我结合网络资料自己总结的,为了节约时间图示随便画的 ...
- 【省选水题集Day1】一起来AK水题吧! 题目(更新到B)
题解:http://www.cnblogs.com/ljc20020730/p/6937954.html 水题A: [AHOI2001]质数和分解 题目网址: https://www.luogu.or ...
- 【bzoj2780】 Sevenk Love Oimaster
http://www.lydsy.com/JudgeOnline/problem.php?id=2780 (题目链接) 题意 给出很多主串和很多询问串,求一个询问串在多少主串中出现过 Solution ...
- Linux内核分析实验二:mykernel实验指导(操作系统是如何工作的)
计算机是如何工作的?(总结)——三个法宝 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构: 函数调用堆栈,高级语言得以运行的基础,只有机器语言和汇编语言的时候堆栈机制对于计算机来说并不那么重要 ...
- Windows + Ubuntu下JDK与adb/android环境变量配置完整教程
假设JDK和android sdk路径分别如下: D:\Program Files\Java\jdkD:\android-sdk 1.JDK环境变量配置JAVA_HOME=D:\Program Fil ...
- [POI2008] BLO
link 试题分析 分两种情况考虑. 当此点不是割点是,答案是$2\times (n-1)$. 当是割点时,我们发现这个点把树分成了若干个联通块,只要两两相乘即可. #include<iostr ...
- ubuntu vim 配置
set nuset autoindent cindentmap<F9> :w<cr> :!g++ -O2 -o %< % -Wall<cr>map<F1 ...