標籤:複雜 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