codeforces 816C Karen and Game
来源:互联网 发布:数码暴龙网络侦探透明 编辑:程序博客网 时间:2024/05/17 23:30
找到每行每列的最小值依次消除,但是比较先考虑行和先考虑列哪种更优.
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int n,m;const int MAXN=110;int a[MAXN][MAXN];int aa[MAXN][MAXN];struct node{ int op; int x; int cnt; node(){} node(int opp,int xx,int yy){ op=opp; x=xx; cnt=yy; }}p[20010],pp[20010];const int INF=100000;int main(){ scanf("%d%d",&n,&m); int tot=0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%d",&a[i][j]); aa[i][j]=a[i][j]; } } for(int i=1;i<=n;i++){ int MIN=INF; for(int j=1;j<=m;j++){ MIN=min(MIN,a[i][j]); } if(MIN>0){ p[tot++]=node(0,i,MIN); for(int j=1;j<=m;j++){ a[i][j]-=MIN; } } } for(int j=1;j<=m;j++){ int MIN=INF; for(int i=1;i<=n;i++){ MIN=min(MIN,a[i][j]); } if(MIN>0){ p[tot++]=node(1,j,MIN); for(int i=1;i<=n;i++){ a[i][j]-=MIN; } } } int to=0; for(int j=1;j<=m;j++){ int MIN=INF; for(int i=1;i<=n;i++){ MIN=min(MIN,aa[i][j]); } if(MIN>0){ pp[to++]=node(1,j,MIN); for(int i=1;i<=n;i++){ aa[i][j]-=MIN; } } } for(int i=1;i<=n;i++){ int MIN=INF; for(int j=1;j<=m;j++){ MIN=min(MIN,aa[i][j]); } if(MIN>0){ pp[to++]=node(0,i,MIN); for(int j=1;j<=m;j++){ aa[i][j]-=MIN; } } } int ok=1; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(a[i][j]){ ok=-1; break; } } if(ok==-1) break; } if(ok==-1){ puts("-1"); return 0; } int sum=0; for(int i=0;i<tot;i++) sum+=p[i].cnt; int ssum=0; for(int i=0;i<to;i++) ssum+=pp[i].cnt; if(sum<=ssum) ok=0; else ok=1; printf("%d\n",min(sum,ssum)); if(ok==0){ for(int i=0;i<tot;i++){ if(p[i].op==0){ for(int j=0;j<p[i].cnt;j++) printf("row %d\n",p[i].x); }else{ for(int j=0;j<p[i].cnt;j++) printf("col %d\n",p[i].x); } } }else{ for(int i=0;i<to;i++){ if(pp[i].op==0){ for(int j=0;j<pp[i].cnt;j++) printf("row %d\n",pp[i].x); }else{ for(int j=0;j<pp[i].cnt;j++) printf("col %d\n",pp[i].x); } } }}
阅读全文
0 0
- Karen and Game codeforces 816c
- Codeforces 816C Karen and Game【思维】
- codeforces 816C Karen and Game
- Karen and Game Codeforces
- C. Karen and Game
- Codeforces Round #419 (Div. 2) C.Karen and Game 思维
- Codeforces Round #419 (Div. 2) C. Karen and Game 题解
- Codeforces Round #419 (Div. 2) C. Karen and Game【贪心】
- Codeforces Round #419 A. Karen and Game
- Codeforces 815A-Karen and Game
- C. Karen and Game【模拟+思维】
- Round#419 C Karen and Game
- Codeforces Round #419 (Div. 2)-贪心&思维-C. Karen and Game
- codeforces815A Karen and Game
- Karen and Game
- Codeforces Round #419 (Div. 2)Karen and Game
- Karen and Coffee codeforces 816B
- Codeforces 816 -B. Karen and Coffee
- ArangoDB高级操作
- SimpleDateFormat使用详解
- C#多线程实现方法——异步委托/调用(一)
- javascript高级程序设计(javascript简介)
- web前端左右选择框移动,及查询
- codeforces 816C Karen and Game
- 正则表达式之邮箱验证
- 【Redis入门】Redis概述
- linux下c语言入门02 c语言特性及函数入门
- JMeter中各种请求格式实现》》》原文链接:
- WebUI设计命名规范
- LintCode-A + B 问题
- Java线程池实例解析
- codeforces 701A Cards