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中的Object类的toString()方法,equals()方法

    Object类是所有类的父类,若没有明确使用extends关键字明确表示该类继承哪个类,那么它就默认继承Object类,也就可以使用Object中的方法: 1.toString 如果输出一个对象的时候 ...

  2. FPGA的软核与硬核

    硬核 zynq和pynq系列的fpga都是双ARM/Cortex-A9构成,这里的ARM处理器为硬核,Cortex-A9部分为FPGA部分.即整体分为两部分:PS/PL.PS部分为A9处理器部分,PL ...

  3. Java微笔记(8)

    Java 中的包装类 Java 为每个基本数据类型都提供了一个包装类,这样就可以像操作对象那样来操作基本数据类型 基本类型和包装类之间的对应关系: 包装类主要提供了两大类方法: 将本类型和其他基本类型 ...

  4. C#控制台应用程序

    使用C#创建控制台应用程序的基本步骤: (1)创建项目: (2)编辑C#源代码: (3)编译运行: 例题:在控制台输出“Hello world!”. 第一步:文件→新建→项目:选择“项目类型”为Vis ...

  5. 201621123037 《Java程序设计》第14周学习总结

    作业14-数据库 标签(空格分隔): Java 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造 ...

  6. QMdiArea及QMdiSubWindow实现父子窗口及布局方法

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMdiArea及QMdiSubWindow实现父子窗口及布局方法     本文地址:http ...

  7. ASP.NET存储Session的StateServer

    由于公司要对服务器做个负载均衡,所以Web项目在两台前端服务器(web1.web2)各部署了一份.但是在项目中会用到session.当一开始在web1上登陆后,由于web1之后负载可能会变大,就有可能 ...

  8. Centos7安装完毕后联网-设置ip地址(VMware虚拟机)

    VMware虚拟机中安装了Centos7,为了让Centos能够访问外网及设置固定的ip地址以方便本地通过SSH访问Centos,做以下几步.本文来自osfipin note. 1.确认虚拟机网络链接 ...

  9. H5跳转到百度地图并定位

    找了半天的JS api,发现没有,后来发现这个叫 url api,让我好找. 官方文档: http://lbsyun.baidu.com/index.php?title=uri/api/web : 简 ...

  10. 洛谷P2894[USACO08FEB]酒店Hotel(线段树)

    问题描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...