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 ...
随机推荐
- 使用robot封装一个模拟键盘复制粘贴并按下回车的方法
/** * 复制数据到剪切板并粘贴出来并按下回车 * @param writeMe 需要粘贴的地址 * @throws java.awt.AWTException */ public void use ...
- php aes加密
<?php namespace Aes; error_reporting(E_ALL); ini_set('display_errors', '1'); class Aes { /** * va ...
- hdu 4686 Arc of Dream(矩阵快速幂)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 其中a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY ...
- php curl常用的5个例子
转载:http://www.jb100.net/html/content-22-821-1.html php curl常用的5个例子 我用php ,curl主要是抓取数据,当然我们可以用其他的方法 ...
- Java的StringBuIlder扩容机制
JDK 1.6中,扩容的源码是这样: void expandCapacity(int minimumCapacity) { int newCapacity = (value.length + 1) * ...
- Linux下如何确认磁盘是否为SSD
方法 法1:通过查看/sys/block/sda/queue/rotational 通过cat /sys/block/sda/queue/rotational进行查看,返回值0即为SSD:返回1即为H ...
- jenkins自动部署windwos服务器
jenkins 持续构建windows 项目 需求说明 公司新购windwos服务器,并配置了堡垒机,由于经常要提交代码进行更新,导致手动部署很是麻烦,故采用公司jenkins实行持续构建 jenki ...
- es各类SearchType的意思
元素 含义 QUERY_THEN_FETCH 查询是针对所有的块执行的,但返回的是足够的信息,而不是文档内容(Document).结果会被排序和分级,基于此,只有相关的块的文档对象会被返回.由于被取到 ...
- kibana使用(ELK)、Lucene 查询语法
Lucene查询 Lucene查询语法以可读的方式书写,然后使用JavaCC进行词法转换,转换成机器可识别的查询. 下面着重介绍下Lucene支持的查询: Terms词语查询 词语搜索,支持 单词 和 ...
- bzoj 1814: Ural 1519 Formula 1 插头dp经典题
用的括号序列,听说比较快. 然并不会预处理,只会每回暴力找匹配的括号. #include<iostream> #include<cstdio> #include<cstr ...