Codeforces 816C/815A - Karen and Game
传送门:http://codeforces.com/contest/816/problem/C

本题是一个模拟问题。
有一个n×m的矩阵。最初,这个矩阵为零矩阵O。现有以下操作:
a.行操作“row i”:对第i(1≤i≤n)行的所有元素加一;
b.列操作“col j”:对第j(1≤j≤m)列的所有元素加一。
经过有限次操作,矩阵变为$G=(g_{i,j})_{m*n}$。
对于给定的矩阵G,试判断G是否可以由零矩阵O通过有限次的“行操作”和“列操作”生成?若可以,则求一个操作步数最小的方案;否则,返回-1。
考虑一个矩阵。假定其首先进行“行操作”,再进行“列操作”。设对第i行的操作次数为row[i],对第j行的操作次数为col[j],则有g[i][j]=row[i]+col[j]。如此,求解row[]和col[]数组即可。
假设零矩阵O经过“行操作”后变为矩阵T,再由矩阵T经过“列操作”变为矩阵G。则row[i]取矩阵G中第i行的最小元素,col[j]取矩阵G-T中第j行的最小元素。若零矩阵O可以通过row[]和col[]数组对应的操作变为矩阵G,则row[]和col[]数组对应的操作方案为最优操作方案;否则,可行的操作方案不存在。
row[]和col[]数组的求解在程序实现上可以通过逆向模拟的方法。
值得注意的是,对于一个给定行列数目的矩阵,若其行数不大于列数,则首先进行“行操作”,再进行“列操作”是最佳选择;否则,首先进行“列操作”,再进行“行操作”是最佳选择。
参考程序如下:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
#define MAX_VAL 1000 int n, m, cnt = ;
int g[SIZE][SIZE];
int row[SIZE], col[SIZE]; void row_operate(void)
{
for (int i = ; i < n; i++) {
row[i] = MAX_VAL;
for (int j = ; j < m; j++)
if (g[i][j] < row[i]) row[i] = g[i][j];
for (int j = ; j < m; j++)
g[i][j] -= row[i];
cnt += row[i];
}
} void col_operate(void)
{
for (int j = ; j < m; j++) {
col[j] = MAX_VAL;
for (int i = ; i < n; i++)
if (g[i][j] < col[j]) col[j] = g[i][j];
for (int i = ; i < n; i++)
g[i][j] -= col[j];
cnt += col[j];
}
} int main(void)
{
scanf("%d%d", &n, &m);
for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
scanf("%d", &g[i][j]);
if (n <= m) {
row_operate();
col_operate();
}
else {
col_operate();
row_operate();
}
for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
if (g[i][j]) {
printf("-1\n");
exit();
}
printf("%d\n", cnt);
for (int i = ; i < n; i++)
for (int k = ; k < row[i]; k++)
printf("row %d\n", i + );
for (int j = ; j < m; j++)
for (int k = ; k < col[j]; k++)
printf("col %d\n", j + );
return ;
}
Codeforces 816C/815A - Karen and Game的更多相关文章
- 【codeforces 816C】Karen and Game
[题目链接]:http://codeforces.com/contest/816/problem/C [题意] 给你一个n*m的矩阵; 一开始所有数字都是0; 每次操作,你能把某一行,或某一列的数字全 ...
- 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 ...
- CodeForces - 816C Karen and Game(简单模拟)
Problem Description On the way to school, Karen became fixated on the puzzle game on her phone! The ...
- 【Codeforces 815C】Karen and Supermarket
Codeforces 815 C 考虑树型dp. \(dp[i][0/1][k]\)表示现在在第i个节点, 父亲节点有没有选用优惠, 这个子树中买k个节点所需要花的最小代价. 然后转移的时候枚举i的一 ...
- CodeForces 816C 思维
On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as fo ...
- Codeforces 815 C Karen and Supermarket
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...
- 【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 816 E. Karen and Supermarket(树形dp)
题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...
随机推荐
- pytest 失败用例重试
https://www.cnblogs.com/jinzhuduoduo/articles/7017405.html http://www.lxway.com/445949491.htm https: ...
- 卡尔曼滤波(Kalman Filter) 的进一步讨论
我们在上一篇文章中通过一个简单的样例算是入门卡尔曼滤波了.本文将以此为基础讨论一些技术细节. 卡尔曼滤波(Kalman Filter) http://blog.csdn.net/baimafujinj ...
- alsa 用户空间编程【转】
本文转载自:http://blog.csdn.net/sjin_1314/article/details/12872581 /**alsa play test *ALSA用户空间编译,ALSA驱动的声 ...
- Swift版本UIWebView长按保存图片
起因 最近需要做个IOS的壳子,用到长按保存图片的功能,发现百度出来的全是OC语法的例子,很多都不是全面,只能自己写一份Swift版本的,图片下面附上Github地址 效果图 Github地址:htt ...
- C# Path 有关于文件路径等问题类(转)
C# Path 标签:C#, Path C-Sharp 0 Path handles file path processing. The .NET Framework provides effect ...
- springboot的登录拦截机制
转自:https://blog.csdn.net/qq_26555463/article/details/78296103 如果是一个后台的管理项目的,有些东西是不能直接就可以访问的,必须要登录才可以 ...
- 3.2 手机中的数据库——SQLite
http://www.sqlite.org/download.html 截至我安装SQLite数据库为止的时间,最新的版本可以下载sqlite-dll-win64-x64-3200000.zip和sq ...
- 关于Vue.js去掉#号路由
正常启动后访问路由: 中间会自动加入一个#号 去掉#号: 在route文件夹下的index.js中加入mode: 'history', ①: ②: 关于mode说明: 默认值: ‘hash‘(浏览器) ...
- 跳出双重for循环的案例__________跳出了,则不再执行标签ok下的for循环代码
ok: for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { System.out.print("*" ...
- spring 九种设计模式
spring中常用的设计模式达到九种,我们举例说明: 第一种:简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. 简单工厂模式的实质是由一 ...