UVA 12382 Grid of Lamps ZOJ 3732 Graph Reconstruction 可圖判定性,12382zoj

來源:互聯網
上載者:User

UVA 12382 Grid of Lamps ZOJ 3732 Graph Reconstruction 可圖判定性,12382zoj

Uva 12382 題目連結:點擊開啟連結


ZOJ 3732題解:點擊開啟連結

Uva12382

題意:

給定n*m的地板,每個地板上有一盞燈,要麼亮要麼暗。

下面n個數字表示每行至少亮的燈盞數

下面m個數字表示每列至少亮的燈盞數

求:開始燈都是暗的,最少點亮幾盞燈能滿足上述條件

思路:

先給行和列大到小排序,然後每次用行減掉列。減完後再排個序即可。

即:即時排序。

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>using namespace std;const int N = 1005;typedef long long ll;int a[N], b[N];bool cmp(const int x, const int y) {return x>y;}int main() {int T, n, m;scanf("%d", &T);while(T-- > 0) {        scanf("%d%d", &n, &m);        int suma = 0, sumb = 0;        for(int i = 0; i < n; i ++) {        scanf("%d", &a[i]);        suma += a[i];        }        for(int i = 0; i < m; i ++) {        scanf("%d", &b[i]);        sumb += b[i];        }        int ans = 0;        if(suma < sumb) {        for(int i = 0; i < n; i ++) {        sort(b, b+m, cmp);        for(int j = 0; j < m && a[i]; j ++) {        if(b[j]) {        ans ++;        a[i]--;        b[j]--;        suma --;        sumb --;        } else break;        }        }        } else {        for(int j = 0; j < m; j ++) {        sort(a, a+n, cmp);        for(int i = 0; i < n && b[j]; i ++) {        if(a[i]) {        ans ++;        a[i]--;        b[j]--;        suma --;        sumb --;        } else break;        }        }        }        printf("%d\n", ans + suma + sumb);}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.