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的优惠券需要被 ...
随机推荐
- Eclipse集成Resinserver
因为Resin在Eclipse下的表现丝毫不亚于Tomcat,小编决定带领众小弟一起学习使用Resin.虽然小编身边也没有什么大牛在使用Resin,但看到Resin的广告已经吹到天边了.所以还 ...
- LeetCode 234 Palindrome Linked List(回文链表)(*)(?)
翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 Given a singly linked list, determine if it is ...
- hdu 4932 Miaomiao's Geometry(暴力枚举)
pid=4932">Miaomiao's Geometry ...
- 将byte[]转为各种进制的字符串
/** * 将byte[]转为各种进制的字符串 * @param bytes byte[] * @param radix 基数可以转换进制的范围(2-36),从Chara ...
- 没有终结点在侦听可以接受消息的 http://192.168.1.63:8085/LoginService。这通常是由于不正确的地址或者 SOAP 操作导致的
2016-04-08 09:15:05,581 [8] ERROR System.Threading.Thread - ErrorSystem.ServiceModel.EndpointNotFoun ...
- 把一个文件夹下的多个csv文件合并到一个excel的多个sheet
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import pandas as pd import os import re if __name__ = ...
- Struts2标签库常用标签
转自:https://blog.csdn.net/q547550831/article/details/53326042
- [Apple开发者帐户帮助]三、创建证书(4)创建Safari签名证书
您的Safari扩展程序必须由Apple颁发的证书签名,您可以在开发者帐户中创建和下载该证书. 在“ 证书”,“标识符和配置文件”中,从左侧的弹出菜单中选择“Safari扩展”. 在“证书”下,选择“ ...
- 消除svn选定(checkout)桌面上文件显示一大堆问号。
图片: 解决方法一: 桌面右键选择TortoiseSVN——>点击Settings,如下图,选中Icon Overlays(图标覆盖),去勾选Fixed drives(本地磁盘),点击确定,按F ...
- Android 应用安装成功之后删除apk文件
问题: 在应用开发中遇到需要这样的需求:在用户下载我们的应用安装之后删除安装包. 解决: android会在每个外界操作APK的动作之后发出系统级别的广播,过滤器名称: android.intent. ...