题目链接:

C - Grid of Lamps

Time Limit:1000MS
Memory 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 2

sample 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 贪心的更多相关文章

  1. UVA 12382 Grid of Lamps --贪心+优先队列

    题意:给出每行每列至少有的灯泡数,问最少有的灯泡数. 解法:要使灯泡数尽量小,说明要使交叉点尽量多,这样即抵了行,又抵了列,为最优的.所以可以用行来消去列,也可以用列来消去行,我这里是列来消去行.首先 ...

  2. uva 10671 - Grid Speed(dp)

    题目链接:uva 10671 - Grid Speed 题目大意:给出N,表示在一个N*N的网格中,每段路长L,如今给出h,v的限制速度,以及起始位置sx,sy,终止位置ex,ey,时间范围st,et ...

  3. 【NOIP合并果子】uva 10954 add all【贪心】——yhx

    Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...

  4. 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 ...

  5. UVa 11134 (区间上的贪心) Fabled Rooks

    这道题真是WA得我心力交瘁,好讨厌的感觉啊! 简直木有写题解的心情了 题意: n×n的棋盘里,放置n个车,使得任意两车不同行且不同列,且第i个车必须放在给定的第i个矩形范围内.输出一种方案,即每个车的 ...

  6. uva 993 Product of digits (贪心 + 分解因子)

      Product of digits  For a given non-negative integer number N , find the minimal natural Q such tha ...

  7. 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 ...

  8. uva 714 - Copying Books(贪心 最大值最小化 二分)

    题目描写叙述开头一大堆屁话,我还细致看了半天..事实上就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,假设最小值同样情况下有多种分配方案,输出前面份数小的,就 ...

  9. UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】

    UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...

随机推荐

  1. yarn.resourcemanager.ha.id设置

    resourcemanager启动报错,其中一个启动成功,另一个启动报8088端口被成功启动的rm占用 2016-11-18 17:08:49,478 INFO org.apache.zookeepe ...

  2. SQL Server2000安装

    本篇文章介绍了安装SQL Server 2000各版本的软硬件配置要求,企业版安装过程的详细步骤,以及需要注意的事项.其他版本可以参考安装 注意:Windows XP不能装企业版.win2000\wi ...

  3. windows使用技巧

    使用命令行工具(cmd)并总是要切换到同一个文件夹   ◆通常的办法 打开命令行工具 切换到指定盘符 切换到目录 Microsoft Windows [Version 10.0.14393] (c) ...

  4. linux中重置服务器的mysql用户密码

    本文章前提条件是自己经把mysql登录密码给忘记了,这个时间我们解决方法有很多,重新安装mysql数据库一切重来,另一种是通过下面文章重新设置root密码,下面我们一起来看看方法二吧.     最 近 ...

  5. JDBC连接MySQL数据库及示例

      JDBC是Sun公司制定的一个可以用Java语言连接数据库的技术. 一.JDBC基础知识         JDBC(Java Data Base Connectivity,java数据库连接)是一 ...

  6. android ListView_显示数据库数据

    xml <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...

  7. seajs构建方法

    标准构建 如果项目遵循推荐的标准目录结构: foo-module/   |-- dist                    //存放构建好的文件   |-- src                 ...

  8. IOS_问题: Xcode8 安装KSImageName插件, 编代码就崩了

    Xcode 8之后, KSImageName插件就不能用了,如果安装了,就会导致一写英文代码Xcode 就崩了. 解决方法: 把这个插件删除,重启了一下xcode就可以了, 如果重启没用, 可以尝试下 ...

  9. Nginx启动SSL功能,并进行功能优化,你看这个就足够了

    一:开始Nginx的SSL模块 1.1 Nginx如果未开启SSL模块,配置Https时提示错误 nginx: [emerg] the "ssl" parameter requir ...

  10. Unity3d 动态批处理的问题

    这段时间做unity3d的优化,主要的入手是减少draw call.    1.代码上主要是把一些零碎的同材质的合并成一个大的mesh.    2.减少不必要的全屏后期处理.把摄像机的renderin ...