【codeforces 816C】Karen and Game
【题目链接】: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的更多相关文章
- 【codeforces 816B】Karen and Coffee
[题目链接]:http://codeforces.com/contest/816/problem/B [题意] 给你很多个区间[l,r]; 1<=l<=r<=2e5 一个数字如果被k ...
- 【codeforces 816A】Karen and Morning
[题目链接]:http://codeforces.com/contest/816/problem/A [题意] 让你一分钟一分钟地累加时间; 问多长时间以后是个回文串; [题解] reverse之后如 ...
- 【Codeforces 815C】Karen and Supermarket
Codeforces 815 C 考虑树型dp. \(dp[i][0/1][k]\)表示现在在第i个节点, 父亲节点有没有选用优惠, 这个子树中买k个节点所需要花的最小代价. 然后转移的时候枚举i的一 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- 使用面向对象技术创建高级 Web 应用程序
作者: 出处: 使用面向对象技术创建高级 Web 应用程序 来源:开源中国社区 作者:oschina 最近,我面试了一位具有5年Web应用开发经验的软件开发人员.她有4年半的JavaScript编程经 ...
- elment表格分页
项目的时候遇到了一个分页的bug,经过分析Element源码之后找到了问题所在,现在把这个问题及解决方法记录下来. 项目中要实现的功能是用户选择查看表格的时候在任意页面点击查询,得到结果之后要展示的页 ...
- 【Jim】I am back (ง •_•)ง
其实上周就来考过一次试了,真是啥都忘了 (´ー∀ー`) 下午在写[树网的核],写了一半去吃饭,回来时发现高二机房的门被锁上了,于是他们都被堵在门口. 我就回到我的地方接着写码. 听到外面有个高二的妹子 ...
- HDU 2049 不容易系列之(4)——考新郎( 错排 )
链接:传送门 思路:错排水题,从N个人中选出M个人进行错排,即 C(n,m)*d[m] 补充:组合数C(n,m)能用double计算吗?第二部分有解释 Part 1. 分别求出来组合数的分子和分母然后 ...
- 使用 Xshell 连接 linux 系统
一.下载 Xshell 链接:https://pan.baidu.com/s/1htwqpzm 密码:zau7 二.安装 Xshell 无脑下一步就可以了 三.连接 linux 四.安装 Xftp h ...
- Ubuntu安装keepalived
Ubuntu安装keepalived 一.Keepalived是什么鬼东西: keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障. 二.Ke ...
- HDU 4311 Contest 2
求的是曼哈顿距离.可以把X,Y的距离分开来求.其中,求X.Y的距离可以通过排序后递推的方式求出值的. #include <iostream> #include <algorithm& ...
- NHibernate3剖析:Query篇之NHibernate.Linq增强查询
系列引入 NHibernate3.0剖析系列分别从Configuration篇.Mapping篇.Query篇.Session策略篇.应用篇等方面全面揭示NHibernate3.0新特性和应用及其各种 ...
- 第十五章,读取txt文件(C++)
#include <iostream> #include <fstream> int main(int argc, char** argv) { std::ifstream i ...
- Darwin流媒体server在windows下搭建
简单介绍 主页: http://dss.macosforge.org/ Darwin Streaming Server (DSS) is an open sourceproject intende ...