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. 第四周 实验一 Java开发环境的熟悉 报告

    Java开发环境的熟悉 实验内容 1.IDEA的安装过程 2.使用IDEA代替虚拟机运行.编译.调试Java程序 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)&g ...

  2. Java变量声明,实例化,问题

    1.变量在输出前必须实例化,这是因为只有声明,没有分配内存空间 在这种情况下会报错 2.实例化后,尽管没有赋值,可能是默认了吧,但也不会输出null,什么也没有输出 上面的理解可能是错的,a赋值了,就 ...

  3. python 读取blob

    for num in range(76802): # if num == 0: # c[num] = imagedata[0:4] # d[num] = struct.unpack('i', c[nu ...

  4. Linux 安装php扩展 swoole

    swoole是一个PHP的异步.并行.高性能网络通信引擎,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据库连接池,AsyncT ...

  5. lintcode-450-K组翻转链表

    450-K组翻转链表 给你一个链表以及一个k,将这个链表从头指针开始每k个翻转一下. 链表元素个数不是k的倍数,最后剩余的不用翻转. 样例 给出链表 1->2->3->4->5 ...

  6. 使用 python 管理 mysql 开发工具箱 - 2

    这篇博文接着上篇文章<使用 python 管理 mysql 开发工具箱 - 1>,继续写下自己学习 python 管理 MySQL 中的知识记录. 一.MySQL 的读写分离 学习完 My ...

  7. 使用RStudio学习一个简单神经网络

    数据准备 1.收集数据 UC Irvine Machine Learning Repository-Concrete Compressive Strength Data Set 把下载到的Concre ...

  8. php奇葩错误:htmlspecialchars处理中文丢失

    $value = "中文中文"; $res = htmlspecialchars($value); 经过这个函数处理之后,$res就直接变成了空的字符串. 奇葩错误啊!后来发现要这 ...

  9. virtio是啥子

    这个山头今天好像要攻占下来了 guest os中的一些特权操作会被hypervhisor给接收,这里一个很重要的认识是:hypervisor是os的os,既然要访问资源,那么就需要经过整机资源的管理者 ...

  10. rxjs5.X系列 —— ErrorHandling/Condition/Mathematical系列 api 笔记

    欢迎指导与讨论 : ) 前言 本文是笔者翻译 RxJS 5.X 官网各类operation操作系列的的第四篇 —— ErrorHanding异常处理.Condition Operator情况操作.Ma ...