UVA 12382 Grid of Lamps 贪心
题目链接:
C - Grid of Lamps
Time Limit:1000MSMemory Limit: 0KB
#### 问题描述
> We have a grid of lamps. Some of the lamps are on, while others are off. The luminosity of a row/column
> is the number of its lighted lamps. You are given a permutation of the luminosities of the rows and a
> permutation of the columns’. Unfortunately, these values are not accurate but we know at least that
> they are not overestimates. You should tell us the minimum possible number of lighted lamps in this
> grid.
> As an example, consider the following grid. The lighted lamps are shown by 1’s and unlighted ones
> by 0’s.
> 1 0 1 1 0
> 1 1 1 1 1
> 0 0 0 1 1
> 0 1 1 1 0
> 1 0 0 0 0
> The actual luminosities of the rows are . A permutation of them could be 1, 2, 5, 3, 3 >, and the inexact values you’d be given could be .
输入
The first line of input contains an integer T ≤ 100 denoting the number of test-cases. Each test-case
begins with two integers M and N, both in the interval [1, 1000], determining the number of rows and
columns of the grid respectively. The next two lines give the luminosities, the first for rows (M values)
and the second for columns (N values).
输出
For each test-case, on a single line, output the minimum conceivable number of lighted lamps.
样例
sample input
3
2 2
2 0
0 2
1 4
2
1 0 1 1
2 4
3 1
0 2 1 2sample output
3
3
5
题意
在n*m行的矩阵中
给你每行至少要亮的灯泡和每列至少要亮的灯泡,问至少有多少个灯泡要亮。
题解
在交叉点放越多的灯泡,就能使灯泡数越少。
题目相当于是问现在你能够用每一列的值去消行的值(每一列只能消一次行,剩余的无法消的后面也不能用!),问最多能消掉多少数,贪心的,我们每一次肯定优先消掉最大的k行,因为最大的行是最有可能消不完的。
用一个优先队列来维护,或者消完一次行,就对行重新排一下序也可以。
代码
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn = 1010;
int a[maxn], b[maxn];
int n, m;
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
scanf("%d%d", &n, &m);
int sum = 0;
for (int i = 0; i < n; i++) scanf("%d", &a[i]), sum += a[i];
for (int j = 0; j < m; j++) scanf("%d", &b[j]), sum += b[j];
int cnt = 0;
priority_queue<int> pq;
for (int i = 0; i < n; i++) pq.push(a[i]);
int st = 0;
vector<int> tmp;
while (st < m&&pq.top()) {
tmp.clear();
while (!pq.empty()&&pq.top() && b[st]) {
cnt++;
b[st]--;
tmp.push_back(pq.top() - 1);
pq.pop();
}
for (int i = 0; i < tmp.size(); i++) {
pq.push(tmp[i]);
}
st++;
}
printf("%d\n", sum - cnt);
}
return 0;
}
UVA 12382 Grid of Lamps 贪心的更多相关文章
- UVA 12382 Grid of Lamps --贪心+优先队列
题意:给出每行每列至少有的灯泡数,问最少有的灯泡数. 解法:要使灯泡数尽量小,说明要使交叉点尽量多,这样即抵了行,又抵了列,为最优的.所以可以用行来消去列,也可以用列来消去行,我这里是列来消去行.首先 ...
- uva 10671 - Grid Speed(dp)
题目链接:uva 10671 - Grid Speed 题目大意:给出N,表示在一个N*N的网格中,每段路长L,如今给出h,v的限制速度,以及起始位置sx,sy,终止位置ex,ey,时间范围st,et ...
- 【NOIP合并果子】uva 10954 add all【贪心】——yhx
Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...
- uva 11134 fabled rooks (贪心)——yhx
We would like to place n rooks, 1 n 5000, on a n nboard subject to the following restrictions• The i ...
- UVa 11134 (区间上的贪心) Fabled Rooks
这道题真是WA得我心力交瘁,好讨厌的感觉啊! 简直木有写题解的心情了 题意: n×n的棋盘里,放置n个车,使得任意两车不同行且不同列,且第i个车必须放在给定的第i个矩形范围内.输出一种方案,即每个车的 ...
- uva 993 Product of digits (贪心 + 分解因子)
Product of digits For a given non-negative integer number N , find the minimal natural Q such tha ...
- UVA 11729 - Commando War(贪心 相邻交换法)
Commando War There is a war and it doesn't look very promising for your country. Now it's time to ac ...
- uva 714 - Copying Books(贪心 最大值最小化 二分)
题目描写叙述开头一大堆屁话,我还细致看了半天..事实上就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,假设最小值同样情况下有多种分配方案,输出前面份数小的,就 ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
随机推荐
- jQuery中的DOM操作<思维导图>
DOM是Document Object Model的缩写,意思是文档对象模型.DOM是一种与浏览器.平台.语言无关的接口.使用该接口可以轻松地访问页面中所有的标准组件.简单来说,DOM解决了Netsc ...
- ping命令的用法大全!
1)如何查看本机所开端口: 用netstat -an命令查看!再stat下面有一些英文,我来简单说一下这些英文具体都代表什么- LISTEN:侦听来自远方的TCP端口的连接请求 SYN-SENT:再发 ...
- Windows下搭建论坛
Windows下搭建论坛 真正的O基础架构,一步一步走向成功 转载请注明原作者出处 环境准备篇 安装集成包软件 解压后如下 以管理员身份运行setup的批处理 选择推荐的apache版本 选择推荐的m ...
- 实例介绍Cocos2d-x开关菜单
开关菜单是MenuItemToggle类实现的,它是一种可以进行两种状态切换的菜单.它可以通过下面的函数创建: static MenuItemToggle*createWithCallback ( ...
- 【学习笔记】【C语言】指向结构体的指针
1.指向结构体的指针的定义 struct Student *p; 2.利用指针访问结构体的成员 1> (*p).成员名称 2> p->成员名称 3.代码 #include < ...
- How Do I Declare A Block in Objective-C?
As a local variable: returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...}; As a p ...
- 使用DbVisualizer 8 连接Oracle数据库
1. 网上下载一个驱动包ojdbc14.jar,放到oracle目录下:...\DbVisualizer-8.0.1\jdbc\oracle\ojdbc14.jar 2. 打开 DbVisualize ...
- RHEL(RedHat Enterprise Linux)5/6 ISO镜像下载
本文贴出了RHEL(RedHat Enterprise Linux)发行版本中常用的服务器版本的ISO镜像文件,供大家下载学习使用,贴出的版本有RedHat Enterprise Linux(RHEL ...
- background-size 设置背景图片的大小
background-size 设置背景图片的大小,以长度值或百分比显示,还可以通过cover和contain来对图片进行伸缩. 语法: background-size: auto | <长度值 ...
- 无法打开物理文件xxx.mdf 操作系统错误 5:“5(拒绝访问。)” (Microsoft SQL Server,错误: 5120) 的解决方法
问题描述:在附加数据库到sql server时,附加不上,出现如下图所示的错误 解决方法:找到xxx.mdf和xxx_log.ldf文件, 点击“右键”->“属性”->"安全&q ...