LINK

题意:给出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 贪心的更多相关文章

  1. CodeForces - 816C Karen and Game(简单模拟)

    Problem Description On the way to school, Karen became fixated on the puzzle game on her phone! The ...

  2. #419 Div2 Problem C Karen and Game (贪心 && 暴力)

    题目链接:http://codeforces.com/contest/816/problem/C 题意 :给出一个 n*m 的变化后的矩阵,变化前矩阵的元素全是0,变化的规则是选择其中的一行或者一列将 ...

  3. Codeforces Round #419 (Div. 2) A-E

    上紫啦! E题1:59压哨提交成功翻盘 (1:00就做完了调了一个小时,还好意思说出来? (逃)) 题面太长就不复制了,但是配图很可爱所以要贴过来 九条可怜酱好可爱呀 A - Karen and Mo ...

  4. Codeforces Round #419

    A Karen and Morning 找最近的回文时间 模拟  往后推 判判就行 //By SiriusRen #include <bits/stdc++.h> using namesp ...

  5. 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 ...

  6. 【codeforces 816C】Karen and Game

    [题目链接]:http://codeforces.com/contest/816/problem/C [题意] 给你一个n*m的矩阵; 一开始所有数字都是0; 每次操作,你能把某一行,或某一列的数字全 ...

  7. 【贪心】 Codeforces Round #419 (Div. 1) A. Karen and Game

    容易发现,删除的顺序不影响答案. 所以可以随便删. 如果行数大于列数,就先删列:否则先删行. #include<cstdio> #include<algorithm> usin ...

  8. Codeforces 816C/815A - Karen and Game

    传送门:http://codeforces.com/contest/816/problem/C 本题是一个模拟问题. 有一个n×m的矩阵.最初,这个矩阵为零矩阵O.现有以下操作: a.行操作“row  ...

  9. C. Karen and Game

    C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

随机推荐

  1. qq浏览器的用户体验

    用户界面: qq浏览器的用户界面简介,把一些不必要的东西去点,可以很容易让用户找到自己想看的网页,很方便. 记住用户的选择: qq浏览器和QQ相连,可是用QQ账户登录,并且会记住自己访问的高频网页,以 ...

  2. VS2015做单元测试

    1.安装测试插件 2.新建测试用例 这里就用课堂练习找水王  作例子 写一个类waterKing.h和waterKing.cpp //idList.h #pragma once #include< ...

  3. linux 虚拟网络模型介绍

    第一种隔离模型          每一个虚拟机实例的网卡都有两个接口,一端接在虚拟机内部,一端接在宿主机内部,如上图所示eth0就是接在虚拟机内部的,而vnet0就是接在宿主机内部的,只要再创建一个虚 ...

  4. Struts2(四)

    以下内容是基于导入struts2-2.3.32.jar包来讲的 1.struts2配置文件加载的顺序 struts2的StrutsPrepareAndExecuteFilter拦截器中对Dispatc ...

  5. 对小组项目alpha发布的评价

    第一组:新蜂小组 项目:俄罗斯方块 评论:看见同学玩的时候,感到加速下落时不是很灵敏,没有及成绩的功能,用户的界面仍在修正. 第二组:天天向上 项目:连连看 评论:这个游戏增加了很多好玩的功能,比如更 ...

  6. 【BioCode】删除未算出PSSM与SS的蛋白质序列

    代码说明: 由于一些原因(氨基酸序列过长),没有算出PSSM与SS,按照整理出来的未算出特征的文件,删除原来的蛋白质序列: 需删除的氨基酸文件732.txt(共732条氨基酸): 删除前 氨基酸共25 ...

  7. 利用C#进行文件读写的方法选择总结

    小的文本文件(100M以下)直接用File类的ReadAllText()和WriteAllText()方法 这两个方法内部其实就是封装了StreamReader类的ReadToEnd()和Stream ...

  8. stm32f4xx系统总线架构

    最近有人在STMCU社区网站咨询如下问题: 由于实验需要,要用到STM32F407的两个DMA并用定时器触发,在使用过程中发现DMA1无法把GPIO的IDR上的数据传输到内存,调试过程中出现DMA1的 ...

  9. bzoj4754[JSOI2016]独特的树叶

    这个题....别人写得怎么都....那么短啊? 我怎么....WA了好几次啊....怎么去loj扒了数据才调出来啊? 这个算法...怎么我还是不知道对不对啊 怎么回事啊怎么回事啊怎么回事啊? 请无视上 ...

  10. 用PHP写出显示客户端IP与服务器IP的代码

    打印客户端IP: echo $_SERVER[‘REMOTE_ADDR’]; 或者: getenv(‘REMOTE_ADDR’); 打印服务器IP: echo gethostbyname(“www.b ...