【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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- Xcode10适配——Error:Multiple commands produce
今天苹果正式推送了iOS12,今天上午就更新了最新的iOS,及Xcode10.这次更新还行,不需要我们对以前的项目紧急修复,大动手术. 用Xcode10跑之前的项目,也就报了一种类型的错误:Multi ...
- ArchLinux出现ACPI ERROR的解决方法
ArchLinux关机.重启时出现ACPI错误: ACPI Error:Method parse/execution failed \_SB.PCI0.PGON,AE_AML_LOOP_TIMEOUT ...
- PHP动态函数处理
public class Student{ public function speek($name){ echo 'my name is '.$name; } } $method='speek'; $ ...
- Lvs+heartbeat高可用高性能web站点的搭建
这是我们公司在实际的生产环境当中使用的一套东西,希望对大家有所帮助(实际的公网ip,我已经做了相应的修改): 说明:每台服务器需要有两块网卡:eth0连接内网的交换机,用私网ip,实现服务器间内部访问 ...
- Redis:基础知识及其常用数据类型和关键字
Redis: Redis是什么: REmote DIctionary Server(远程字典服务器) 是完全开源免费的,用C语言编写的,遵守BSD协议,是一个高性能的(Key-Value)分布式内存数 ...
- Linux下MySql数据库常用操作
1.显示数据库 show databases; 2.选择数据库 use 数据库名; 3.显示数据库中的表 show tables; 4.显示数据表的结构 describe 表名; 5.显示表中记录 S ...
- POJ——T2352 Stars
http://poj.org/problem?id=2352 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46592 ...
- spring-boot-maven-plugin 插件的作用(转)
OM 文件中添加了“org.springframework.boot:spring-boot-maven-plugin”插件.在添加了该插件之后,当运行“mvn package”进行打包时,会打包成一 ...
- hibernate配置数据库连接池三种用法
三种连接都是以连接MySQl为例. <!-- JDBC驱动程序 --> <property name="connection.driver_class">o ...
- (插播)关于使用jenkins + unity +Xcode 来进行自己主动打包的处理。
近期了解了下jenkins流程化服务的东西,个人感觉jenkins是一个非常方便的工具.主要是方便.设置好流程性得命令.仅仅需确定下就能够达到自己主动化. 减轻了错误得发生和简化了带来的复杂得步骤.今 ...