题目链接:

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行,因为最大的行是最有可能消不完的。

用一个优先队列来维护,或者消完一次行,就对行重新排一下序也可以。

代码

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. using namespace std;
  5. const int maxn = 1010;
  6. int a[maxn], b[maxn];
  7. int n, m;
  8. int main() {
  9. int tc;
  10. scanf("%d", &tc);
  11. while (tc--) {
  12. scanf("%d%d", &n, &m);
  13. int sum = 0;
  14. for (int i = 0; i < n; i++) scanf("%d", &a[i]), sum += a[i];
  15. for (int j = 0; j < m; j++) scanf("%d", &b[j]), sum += b[j];
  16. int cnt = 0;
  17. priority_queue<int> pq;
  18. for (int i = 0; i < n; i++) pq.push(a[i]);
  19. int st = 0;
  20. vector<int> tmp;
  21. while (st < m&&pq.top()) {
  22. tmp.clear();
  23. while (!pq.empty()&&pq.top() && b[st]) {
  24. cnt++;
  25. b[st]--;
  26. tmp.push_back(pq.top() - 1);
  27. pq.pop();
  28. }
  29. for (int i = 0; i < tmp.size(); i++) {
  30. pq.push(tmp[i]);
  31. }
  32. st++;
  33. }
  34. printf("%d\n", sum - cnt);
  35. }
  36. return 0;
  37. }

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. 在远程系统上开发 SharePoint 应用程序

    适用范围: apps for SharePoint | Office 365 | SharePoint Foundation 2013 | SharePoint Server 2013 使用远程安装的 ...

  2. websphere中由于实际应用没有卸载干净,导致安装不了。以下是完全卸载应用程序的方法

     出现此问题的原因之一:操作界面上没有卸载完成.进行一下操作:1.删除 $WAS_HOME/profiles/AppSrv01/config/cells/...cell/applications下对应 ...

  3. hdu 2544 最短路 Dijkstra

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...

  4. Google Maps投影在ArcGIS中的设置

    Google Maps采用的地图投影为Web Mercator,其优点为不同维度其形状保持不变,当然面积要发生变化. ArcGIS9.3中可以直接设置为WGS 1984 Web Mercator,操作 ...

  5. 在 linux x86-64 模式下分析内存映射流程

    前言 在上一篇中我们分析了 linux 在 x86-32 模式下的虚拟内存映射流程,本章主要继续分析 linux 在 x86-64 模式下的虚拟内存映射流程. 讨论的平台是 x86-64, 也可以称为 ...

  6. ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK

    看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, 加入一个表10W数据,另一个表也是10万数据,当你用linq建立一个连接查询 ...

  7. win7 IIS 7.5 HTTP 错误 404.3 - Not Found

    HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 解决这个问题你只需要,打开控制面 ...

  8. C#各种常用开源框架-支持开源!分享!

    下面罗列了开发及学习过程中所涉及的开源类库的列表! AForge.NET Accord.NET NAudio nVLC Speex C# WebServer FFmpeg FFmpeg.NET Flo ...

  9. Hbase 0.98集群搭建的详细步骤

    准备工作 Hbase的搭建是依赖于Hadoop的,Hbase的数据文件实际上存储在HDFS文件系统中,所以我们需要先搭建hadoop环境,之前的博文中已经搭建过了(详见http://www.cnblo ...

  10. delphi图形图像开发相关

    ①delphi的图形处理(doc) http://wenku.baidu.com/view/519df09951e79b89680226ee.html ②delphi的图形图像处理(ppt) http ...