【题目链接】:http://codeforces.com/contest/816/problem/C

【题意】



给你一个n*m的矩阵;

一开始所有数字都是0;

每次操作,你能把某一行,或某一列的数字全部加上1;

问你到达目标矩阵最少需要进行多少次操作;

【题解】



从目标矩阵开始减;

直接枚举每一列需要减多少次;

每一行需要减多少次即可;

但有技巧;

比如以下矩阵

1 1 1

1 1 1

1 1 1

1 1 1

应该一列一列地减比较快;



1 1 1 1

1 1 1 1

一行一行地删比较快;

所以对n和m的大小关系分类讨论一下;

看是先删行还是先删列;



【Number Of WA】



4



【反思】



一开始不知道在想什么,光想着快点写完了,结果写了个没任何把握的贪心,一直瞎试。

没想到多试几个输入hack一下自己;太着急了;

列的英文是colum/xk



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110; int n,m,ans = 0;
int g[N][N],srow[N],sclu[N];
vector <int> row,col; void subrow(){
rep1(i,1,n){
int mi = 1000;
rep1(j,1,m){
mi = min(mi,g[i][j]);
}
rep1(j,1,m)
g[i][j]-=mi;
srow[i] = mi;
ans+=mi;
}
} void subclu(){
rep1(j,1,m){
int mi = 1000;
rep1(i,1,n){
mi = min(mi,g[i][j]);
}
ans+=mi;
rep1(i,1,n)
g[i][j]-=mi;
sclu[j] = mi;
}
} int main(){
//Open();
Close();
cin >> n >> m;
rep1(i,1,n){
rep1(j,1,m)
cin >> g[i][j];
}
if (n < m){
subrow();
subclu();
}else {
subclu();
subrow();
}
rep1(i,1,n)
rep1(j,1,m)
if (g[i][j])
return cout <<-1<<endl,0;
cout << ans << endl;
rep1(i,1,n){
while (srow[i]--){
cout <<"row "<<i<<endl;
}
}
rep1(j,1,m){
while (sclu[j]--){
cout <<"col "<<j<<endl;
}
}
return 0;
}

【codeforces 816C】Karen and Game的更多相关文章

  1. 【codeforces 816B】Karen and Coffee

    [题目链接]:http://codeforces.com/contest/816/problem/B [题意] 给你很多个区间[l,r]; 1<=l<=r<=2e5 一个数字如果被k ...

  2. 【codeforces 816A】Karen and Morning

    [题目链接]:http://codeforces.com/contest/816/problem/A [题意] 让你一分钟一分钟地累加时间; 问多长时间以后是个回文串; [题解] reverse之后如 ...

  3. 【Codeforces 815C】Karen and Supermarket

    Codeforces 815 C 考虑树型dp. \(dp[i][0/1][k]\)表示现在在第i个节点, 父亲节点有没有选用优惠, 这个子树中买k个节点所需要花的最小代价. 然后转移的时候枚举i的一 ...

  4. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  5. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  6. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  7. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  8. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  9. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

随机推荐

  1. Chrome Is The New C Runtime

    出处:https://www.mobilespan.com/content/chrome-is-the-new-c-runtime Chrome Is The New C Runtime Date:  ...

  2. 树莓派搭建 Google TV

    出处:http://my.oschina.net/funnky/blog/142067 树莓派搭建 Google TV 目录:[ - ] Google TV是啥玩意 ? 搭建我们自己的Google T ...

  3. Vue学习之路第二篇:插值表达式

    要开始写Vue的功能了,是不是很激动呢!开始吧! 1.首先建立一个html页面,导入Vue js包 <script type="text/javascript" src=&q ...

  4. [洛谷P3391]【模板】文艺平衡树(Splay)

    题目大意:给定一个$1\sim n$的序列,每次翻转一个区间,输出最后的序列. 解题思路:Splay的区间翻转操作.我借此打了个Splay的模板(运用内存池,但有些功能不确定正确,例如单点插入). 大 ...

  5. 2019-03-28 Python SQL 的注释

    SQL Server 多行注释 : ctrl + k + c SQL Server 单行注释:  -- Python 单行注释:# Python多行注释:''' '''

  6. js数组并集,交集,差集

    js数组并集,交集,差集的计算方式汇总 一. new Set 方式实现 这种方式实现起来比较简单,原理就是参考new Set可以去重的功能 ,关于去重可以点击 https://www.haorooms ...

  7. 关于使用动态语言运行时 (. net)

    AutoCAD Managed .NET API允许您使用使用. NET 4.0 引入的动态语言运行时 (DLR). 使用DLR可以直接访问对象, 而无需: 打开一个对象进行读取或写入, 然后在完成后 ...

  8. 【codeforces 500E】New Year Domino

    [题目链接]:http://codeforces.com/problemset/problem/500/E [题意] 有n个多米诺骨牌; 你知道它们的长度; 然后问你,如果把第i骨牌往后推倒,然后要求 ...

  9. Nginx 项目部署和配置

    nginx 作为代理服务器,需要代理多个项目的话配置如下: server { listen       80; server_name  localhost; #charset koi8-r; #ac ...

  10. C#-WebService基础02

    WebService WSDL是web service的交换格式 跨平台数据交互 什么是web服务 SOA 面向服务的体系结构  service-Oriented Architecture Servi ...