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的优惠券需要被 ...
随机推荐
- 《Spring技术内幕》笔记-第二章 IoC容器的实现
简单介绍 1,在Spring中,SpringIoC提供了一个主要的JavaBean容器.通过IoC模式管理依赖关系.并通过依赖注入和AOP切面增强了为JavaBean这样子的POJO提供事务管理,生命 ...
- velocity简单样例
velocity简单样例整体实现须要三个步骤,详细例如以下: 1.创建一个Javaproject 2.导入须要的jar包 3.创建须要的文件 ============================= ...
- HDU5567/BestCoder Round #63 (div.2) A sequence1 水
sequence1 Given an array a with length n, could you tell me how many pairs (i,j) ( i < j ) for a ...
- yum install mysql(转载)
linux下使用yum安装mysql 1.安装查看有没有安装过: yum list installed mysql* rpm -qa | grep mysql* 查 ...
- luogu1991 无线通讯网
题目大意 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络:每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话.任意两个配备了一条卫星电话线路的哨所(两边都ᤕ有 ...
- 0.0.0.0 IPAddress.Any 【】127.0.0.1 IPAddress.Loopback 【】localhost
0.0.0.0 IPAddress.Any https://msdn.microsoft.com/en-us/library/system.net.ipaddress.any(v=vs.110).a ...
- LESS2CSS for sumlime text2
Windows下的安装 Less2Css插件依赖lessc这个工具,在windows下可以下载或者用git cloneless.js-windows到本地目录.然后把目录地址加入到环境变量PATH的中 ...
- @Transaction 无效
上班的时候碰到这个问题,看了一些博客写的,都试了一遍解决方案,发现结果还是不行, 最后突然发现我的配置顺序和网上的有些许不同,就改了下,发现成功了,特此打桩纪念一下. 一.先说一下基本用法: 1. @ ...
- 第2章 安装Nodejs Nodejs基础 课程介绍
因为你做任何Nodejs应用,底层无非都是通过调用这些既有的开放的接口,来完成相应的功能.这个要注意,不同版本的Nodejs,接口不一定相同.甚至是相同的接口,使用规范也有区别.我们以这个版本来过这些 ...
- mysql重设root的密码 mac
创建: 2017/09/14 第一步: 关闭已开启的mysql服务器 mysql.server stop 第二步: 关闭密码识别模式 /usr/local/bin/mysqld_safe ...