zoukankan      html  css  js  c++  java
  • UVA 12382 Grid of Lamps 贪心

    题目链接:

    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 < 3, 5, 2, 3, 1 >. A permutation of them could be < > 1, 2, 5, 3, 3 >, and the inexact values you’d be given could be < 1, 1, 4, 2, 3 >.

    输入

    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
    ", sum - cnt);
    	}
    	return 0;
    }
  • 相关阅读:
    PHP递归复制文件夹以及传输文件夹到其他服务器。
    CSS中的disable,hidden,readonly
    linux下用scp命令在两个服务器之间传输文件,利用php_scp函数进行文件传输
    在PHP中调用php_ssh实现远程登陆linux服务器并执行shell脚本。
    Centos7搭建lamp环境
    Golang 标准库构建 http.Requests 请求
    go modules 的使用
    Golang 自用第三方库
    《Go 语言并发之道》读后感-第五章
    《Go 语言并发之道》读后感
  • 原文地址:https://www.cnblogs.com/fenice/p/5679178.html
Copyright © 2011-2022 走看看