Educational Codeforces Round #52 C. Make It Equal

來源:互聯網
上載者:User

標籤:複雜   int()   bit   現在   scanf   i++   nal   next   math   

http://codeforces.com/contest/1065/problem/C

晚上狀態比較奇怪,因為一些非常蠢的錯誤 wa 了好幾次。

問題

有 \(N\) 個積木搭成的塔,這些塔的高度計為 \(H_i\),表示它是由 \(H_i\) 塊積木搭成的。每次你可以指定一個高度 \(h\),把那些高度大於 \(h\) 的塔多出來的那一部分移除,代價為移除的積木數量。現在要求對這些積木進行若干次移除操作使所有塔最後的高度相同,問每次的操作代價不超過 \(K\) 的情況下,最少要進行幾次操作。

題解

容易發現最後所有塔的高度應該是高度最小的塔的高度,只需要枚舉 \(h\) 從上向下貪心地移除就好了。複雜度 \(O(N)\)。

#include <bits/stdc++.h>#ifdef LOCAL    #define debug(...) fprintf(stderr, __VA_ARGS__)#else    #define debug(...) 0#endifusing namespace std;typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;typedef pair<int, int> pii;int rint() {    int n, c, sgn = 0;    while ((c = getchar()) < ‘-‘);    if (c == ‘-‘) n = 0, sgn = 1;    else n = c - ‘0‘;    while ((c = getchar()) >= ‘0‘) {        n = 10 * n + c - ‘0‘;    }    return sgn ? -n : n;}const int N = 200010;int n, K;int hei[N];int cnt[N];int main() {    scanf("%d %d", &n, &K);    int mi = N, ma = 0;    for (int i = 0; i < n; i++) {        scanf("%d", &hei[i]);        cnt[hei[i]]++;        mi = min(mi, hei[i]);        ma = max(ma, hei[i]);    }    int ans = 0;    ll last = 0;    int cumu = 0;    for (int h = ma; h > mi; h--) {        // next        if (last + cumu + cnt[h] <= K) {            last += cumu + cnt[h];            cumu += cnt[h];            continue;        }        // must        ans++;        last = cumu + cnt[h];        cumu += cnt[h];    }    if (last > 0) ans++;    printf("%d\n", ans);    return 0;}
總結

以後打代碼前還是得先想好具體的細節。

Educational Codeforces Round #52 C. Make It Equal

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.