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的优惠券需要被 ...
随机推荐
- LeetCode60:Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- js面试题--------JS中数字和字符,布尔类型相加相减问题
JS中数字和字符相加相减问题 <html lang="en"> <head> <meta charset="utf-8" /> ...
- android.os.Process.killProcess(android.os.Process.myPid())与Activity生命周期的影响
如果通过finish方法结束了一个Activity,那么根据Activity的生命周期,则会自动调用Activity的销毁方法onDestory(),但是在项目中遇到这样的一个问题,就是Activi ...
- [NOI2018]归程(80pts)
https://www.zybuluo.com/ysner/note/1219964 题面 题面太长,难以概述,[戳我][1] \(ex10pts\ tree\) \(50pts\ n\leq1500 ...
- idea使用svn
一.在idea中配置svn 二.导出maven项目 连接svn 点击checkout 点击ok就可以 到Commit Changes 这里有几个选项需要了解的: Auto-update after c ...
- Servlet初始化与异步支持
Shared libraries(共享库) / runtimes pluggability(运行时插件能力) 1.Servlet容器启动会扫描,当前应用里面每一个jar包的 ServletContai ...
- 5.30dao-service-controller层,mybatis自动生成。(获取根据id主键获取指定详细数据)
获取权限详细数据:(参考) 1.controller:1.注入Servcie调用方法findConsumerById(参数是id); ...
- Google的网站性能优化最佳实践
网站性能最佳实践 当描述一个web页面的页面速度,评价的一致性遵循许多不同的规则.这些规则是任何阶段的web开发可以应用的前端最佳实践.这个文档的每个规则都陈述于此,无论你是否运行页面测速工具-- ...
- vue中的config配置
在webpack.base.conf文件中配置别名以及扩展名 resolve: { extensions: ['.js', '.vue', '.json', '.styl'], alias: { 'v ...
- 一张图说明DIV盒子距离
虚线的宽高为你实际指定的width和height 虚线外的白色区域为padding 红色区域为border的width 红色外的区域为margin