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. c# 修改exe.config文件并且及时更新

    1.config文件地址:AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 注意:如果是在调试程序中运行,此地址指代的是vhost. ...

  2. tensorflow之曲线拟合

    视频链接:https://morvanzhou.github.io/tutorials/machine-learning/ML-intro/ 1.定义层 定义 add_layer() from __f ...

  3. 第三章 ServerSpcket用法详解

    构造ServerSocket ServerSocket的构造方法如下: ServerSocket() //Creates an unbound server socket. ServerSocket( ...

  4. Distributed transactions in Spring, with and without XA

    While it's common to use the Java Transaction API and the XA protocol for distributed transactions i ...

  5. 【前端学习笔记03】JavaScript对象相关方法及封装

    //Object.create()创建对象 var obj = Object.create({aa:1,bb:2,cc:'c'}); obj.dd = 4; console.log(obj.cc); ...

  6. iOS开发--字典(NSDictionary)和JSON字符串(NSString)之间互转

    iOS开发--字典(NSDictionary)和JSON字符串(NSString)之间互转 1. 字典转Json字符串 // 字典转json字符串方法 -(NSString *)convertToJs ...

  7. 【bzoj4709】[Jsoi2011]柠檬 斜率优化

    题目描述 给你一个长度为 $n$ 的序列,将其分成若干段,每段选择一个数,获得 $这个数\times 它在这段出现次数的平方$ 的价值.求最大总价值. $n\le 10^5$ . 输入 第 1 行:一 ...

  8. 精通android学习笔记(一)---广播

    普通广播:sendBroadcast 有序广播:sendOrderedBroadcast,有序广播优先级可以再manifest中设置,数值越大,最先收到.-1000~1000 <receiver ...

  9. 秒杀多线程第十四篇 读者写者问题继 读写锁SRWLock (续)

    java 包实现了读写锁的操作: package com.multithread.readwritelock; import java.util.concurrent.CountDownLatch; ...

  10. Educational Codeforces Round 55 Div. 2 翻车记

    A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...