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 ...
随机推荐
- mysql按日期分组统计数据
最近在做一个招聘网时,需要显示一个月内企业招聘信息的发布数量,按日期分组统计,刚开始是直接从源数据库表里面进行group by,但这样子就出现日期不连续的问题了,我想要的效果是,若当天没有数据,则显示 ...
- mysql 查询缓存优化文章
还不错 http://www.jzxue.com/shujuku/mysql/200910/20-2981.html
- BZOJ5297 CQOI2018社交网络(矩阵树定理)
板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- 函数防抖与函数节流 封装好的debounce和throttle函数
/** * 空闲控制 返回函数连续调用时,空闲时间必须大于或等于 wait,func 才会执行 * * @param {function} func 传入函数,最后一个参数是额外增加的this对象,. ...
- 如何将SLIC集成到ESXi中
如何将SLIC集成到ESXi中 参考 http://forums.mydigitallife.info/threads/12982-ESX-ESXi-Bios-Tools/page34?p=72183 ...
- hbase 原子操作cas
在高并发的情况下,对数据row1 column=cf1:qual1, timestamp=1, value=val1的插入或者更新可能会导致非预期的情况, 例如:原本客户端A需要在value=val ...
- 【刷题】BZOJ 5418 [Noi2018]屠龙勇士
www.lydsy.com/JudgeOnline/upload/noi2018day2.pdf Solution 将攻击的式子列出来,\(atk \times x-p \times y=a_i\) ...
- Android Studio & eclipse 调试技巧
如上图设置多个断点,开启调试.想跨断点移动到下一个断点,点击如下图1箭头,程序将运行一个断点到下一个断点之间需要执行的代码.如果后面代码没有断点,再次点击该按钮将会执行完程序.点击箭头2指向的按钮,可 ...
- golang 解码未知键的 json 字符串
我们可以使用 interface 接收 json.Unmarshal 的结果,然后利用 type assertion 特性来进行后续操作. package main import ( "en ...
- python学习(23)requests库爬取猫眼电影排行信息
本文介绍如何结合前面讲解的基本知识,采用requests,正则表达式,cookies结合起来,做一次实战,抓取猫眼电影排名信息. 用requests写一个基本的爬虫 排行信息大致如下图 网址链接为ht ...