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 ...
随机推荐
- sql语句创建主键、外键、索引、绑定默认值
use Mengyou88_Wuliu --创建公司表 create table dbo.Company2 ( CompanyID ,) not null, CompanyName ) null, A ...
- SQLSERVER中按年月分组
SQLSERVER中按年月分组 一个表有三个字段id,dt,d 分别存放id,时间,数值 id dt d 1 2004-08-11 12:12:00.000 9 2 2005-09- ...
- 程序代码创建IISWEB站点
#region 新建立一个IISWEB站点 ///<summary> ///添加一个站点 ///</summary> public string CreateWebSite() ...
- 20150221—LINQ to SQL 查询数据
LINQ to SQL 可以快捷的查询基于SQL的数据,直接在VS中包括基本的Object/relation映射器,O/R映射器可以快速的将基于SQL的数据源映射为CLR对象,之后就可以使用LINQ查 ...
- 8个3D视觉效果的HTML5动画欣赏
现在的网页中应用了越来越多的3D应用,特别是基于HTML5 Canvas的动画特效,让用户有一种非常震撼的视觉体验.本文收集了8个非常炫酷的3D视觉效果的HTML5动画,都有源代码分享,你可以学习你感 ...
- 7款HTML5的精美应用教程让你立即爱上HTML5
1,HTML5/jQuery雷达动画图表图表配置十分简单 分享一款很特别的HTML5图表,它是利用HTML5和jQuery的雷达动画图表,图表数据在初始化的时候带有一定动画. 在线演示 源码下载 2, ...
- C++ 11 之初始化
1.4中不同初始化的形式 a.string s("zhl").int i(3); //括号初始化 b.string s="zhl".int ...
- Sql 临时表
一个#是只能在当前打开滴查询窗体查询,两个#是能够在其他打开滴查询窗体查询 SELECT 'VR001839003YP' 列名1,'RO512498726DE' 列名2 INTO #临时表 UNION ...
- 使用MongoDB的开源项目
根据谷歌的搜索结果筛选出来的. 统计应用 counlty https://count.ly/ mongopress 开源CMS系统 http://www.mongopress.org/ Rubedo ...
- lighttpd的超时参数详解
今天服务器上传大文件,服务器php一直没有响应,响应为0KB,经排查发现是lighttpd的超时设置问题 server.max-keep-alive-idle = 5server.max-read-i ...