816C. Karen and Game 贪心
题意:给出n*m的矩阵图,现有对行或对列上的数减1的操作,问最少几步使所有数变为0,无解输出-1
思路:贪心暴力即可。先操作行和先操作列结果可能不同注意比较。
/** @Date : 2017-07-01 10:22:53
* @FileName: 816C.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; int n, m, cnt1, cnt2;
int a[110][110], b[110][110];
int r1[2][110];
int r2[2][110];
void debug()
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
printf("%d", a[i][j]);
printf("\n");
}
}
int main()
{
while(cin >> n >> m)
{
cnt1 = cnt2 = 0;
MMF(r1);
MMF(r2);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
scanf("%d", &a[i][j]);
b[i][j] = a[i][j];
}
}
int mi;
for(int k = 0; k < 2; k++)
{
for(int i = 0; i < n; i++)
{
mi = INF;
for(int j = 0; j < m; j++)
if(k) mi = min(mi, a[j][i]);
else mi = min(mi, a[i][j]);
for(int j = 0; j < m; j++)
if(k) a[j][i] -= mi;
else a[i][j] -= mi;
r1[k][i]= mi;
cnt1+=mi;
}
swap(n, m);
}
//debug();
for(int k = 0; k < 2; k++)
{
for(int i = 0; i < m; i++)
{
mi = INF;
for(int j = 0; j < n; j++)
if(k) mi = min(mi, b[i][j]);
else mi = min(mi, b[j][i]);
for(int j = 0; j < n; j++)
if(k) b[i][j] -= mi;
else b[j][i] -= mi;
r2[k^1][i] = mi;
cnt2+=mi;
}
swap(n, m);
}
//debug();
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
{
if(a[i][j])
cnt1 = -1;
if(b[i][j])
cnt2 = -1;
}
if(cnt1 <= cnt2 && cnt1 >= 0)
{
printf("%d\n", cnt1); for(int i = 0; i < n; i++)
while(r1[0][i]--)
printf("row %d\n", i + 1);
for(int i = 0; i < m; i++)
while(r1[1][i]--)
printf("col %d\n", i + 1);
}
else if(cnt2 <= cnt1 && cnt2 >= 0)
{
printf("%d\n", cnt2);
for(int i = 0; i < n; i++)
while(r2[0][i]--)
printf("row %d\n", i + 1);
for(int i = 0; i < m; i++)
while(r2[1][i]--)
printf("col %d\n", i + 1);
}
else if(cnt1 == -1 && cnt2 >= 0)
{
printf("%d\n", cnt2);
for(int i = 0; i < n; i++)
while(r2[0][i]--)
printf("row %d\n", i + 1);
for(int i = 0; i < m; i++)
while(r2[1][i]--)
printf("col %d\n", i + 1);
}
else if(cnt2 == -1 && cnt1 >= 0)
{
printf("%d\n", cnt1);
for(int i = 0; i < n; i++)
while(r1[0][i]--)
printf("row %d\n", i + 1);
for(int i = 0; i < m; i++)
while(r1[1][i]--)
printf("col %d\n", i + 1);
}
else printf("-1\n");
}
return 0;
}
816C. Karen and Game 贪心的更多相关文章
- CodeForces - 816C Karen and Game(简单模拟)
Problem Description On the way to school, Karen became fixated on the puzzle game on her phone! The ...
- #419 Div2 Problem C Karen and Game (贪心 && 暴力)
题目链接:http://codeforces.com/contest/816/problem/C 题意 :给出一个 n*m 的变化后的矩阵,变化前矩阵的元素全是0,变化的规则是选择其中的一行或者一列将 ...
- Codeforces Round #419 (Div. 2) A-E
上紫啦! E题1:59压哨提交成功翻盘 (1:00就做完了调了一个小时,还好意思说出来? (逃)) 题面太长就不复制了,但是配图很可爱所以要贴过来 九条可怜酱好可爱呀 A - Karen and Mo ...
- Codeforces Round #419
A Karen and Morning 找最近的回文时间 模拟 往后推 判判就行 //By SiriusRen #include <bits/stdc++.h> using namesp ...
- 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 816C】Karen and Game
[题目链接]:http://codeforces.com/contest/816/problem/C [题意] 给你一个n*m的矩阵; 一开始所有数字都是0; 每次操作,你能把某一行,或某一列的数字全 ...
- 【贪心】 Codeforces Round #419 (Div. 1) A. Karen and Game
容易发现,删除的顺序不影响答案. 所以可以随便删. 如果行数大于列数,就先删列:否则先删行. #include<cstdio> #include<algorithm> usin ...
- Codeforces 816C/815A - Karen and Game
传送门:http://codeforces.com/contest/816/problem/C 本题是一个模拟问题. 有一个n×m的矩阵.最初,这个矩阵为零矩阵O.现有以下操作: a.行操作“row ...
- C. Karen and Game
C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
随机推荐
- 2018-2019-20172329 《Java软件结构与数据结构》第四周学习总结
2018-2019-20172329 <Java软件结构与数据结构>第四周学习总结 经过这样一个国庆节的假期,心中只有一个想法,这个国庆假期放的,不如不放呢!! 教材学习内容总结 < ...
- linux awk,sort,uniq,wc,cut命令详解
1.awk awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息 $ 表示当前行 $ 表示第一列 NF 表示一共有多少列 $NF 表示最 ...
- 咱们的team1序章
之前都参加了好多组织,这是第一次参加变成组织.首先要介绍团队名称了,为什么叫“咱们的team”呢,因为,我们需要每个人都认真的参与进来,只有每个人都十分投入地参与进来,这个team才能称之为一个tea ...
- [数位DP]把枚举变成递推(未完)
动态规划(DP)是个很玄学的东西 数位DP实际上 就是把数字上的枚举变成按位的递推 有伪代码 for i =这一位起始值 i<=这一位终止值 dp[这一位][i]+=dp[这一位-1][i]+- ...
- Halcon 笔记1
Halcon Example位置: C:\Users\Public\Documents\MVTec\HALCON-13.0\examples 安装位置:C:\Program Files\MVTec\H ...
- Matlab里面.M文件不能运行,预期的图像也显示不出来的一个原因
matlab中function函数的函数名与保存的文件名需要一样: 函数名是GAconstrain,文件名保存成GAconstrain.m,不要使用复制时候产生副本GAconstrain(1).m.
- 编写shell时,遇到let: not found错误及解决办法
#!/bin/bashi=1sum=0while [ $i -le 100 ]do let sum=sum+$i let i++ done 在写一个简单的循环脚本时,报错 let: not fou ...
- PHP 在windows下配置sendmail,通过 mail() 函数发送邮件
php mail()函数在windows不能用,需要安装sendmail. 1. 从http://glob.com.au下载sendmail.zip 2. 解压sendmail.zip到目录下(最好使 ...
- Delphi DbGridEh实现表格没有内容的渐变效果
OptionsEh = dghExtendVertLines 就会有这个效果, 去掉就会没有这个效果
- 对ViewModel自定义约束
有时候我们常要对一些属性进行自定义的约束,可以这么做 using ListSys.Entity; using System; using System.Collections; using Syste ...