Educational Codeforces Round 52 (Rated for Div. 2) C. Make It Equal
阿新 • • 發佈:2018-11-09
題目:https://codeforces.com/contest/1065/problem/C
這道題是服了,我的演算法是貪心模擬,貪心的找切片,寫的比較慢,因為是模擬,所以考慮的比較多,而且變數比較多。。。。。。
賽後居然被hack了。。。好氣,為什麼呢,就是少考慮了初始時高度都一樣,每次都是不考慮特殊情況。。。希望自己能夠吃個教訓。。。
還有我覺得自己腦子裡都是暴力,模擬,看了大佬的程式碼,人家的演算法根本 不用寫那麼長時間,思路還比較清晰。。。。。
我的垃圾程式碼和思路:
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int inf = 0x3f3f3f3f; const double PI = acos(-1); const double eps = 1e-8; #define pb push_back #define mp make_pair #define fi first #define se second const int N = 2 * 1e5 + 5; LL a[N]; LL n,k; int main() { scanf("%lld %lld",&n,&k); LL sum = 0; for(int i = 0;i < n;++i){ scanf("%lld",&a[i]); sum += a[i]; } sort(a,a + n); sum -= a[0] * n; int h = a[0]; LL ans = 0; int res = 0; //cout << sum << endl; for(LL i = 1;i < n;++i){ if(sum == 0){ break; } if(sum <= k){ res++; break; } ans += (a[i] - h) * (n - i); if(ans >= k){ res++; ans -= (a[i] - h) * (n - i); int x = h; while((ans + (h - x) * (n - i)) <= k){ h++; } h--; sum -= (ans + (h - x) * (n - i)); ans = 0; i--; }else{ h = a[i]; } } printf("%d\n",res); return 0; }
他把每一個高度有的立方體塊雜湊到一個數組裡,然後開始從末尾開始計算在哪個高度需要進行切
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int inf = 0x3f3f3f3f; const double PI = acos(-1); const double eps = 1e-8; #define pb push_back #define mp make_pair #define fi first #define se second const int N = 2 * 1e5 + 5; int a[N]; int main() { int n,k; memset(a,0,sizeof(a)); scanf("%d %d",&n,&k); for(int i = 1;i <= n;++i){ int x; scanf("%d",&x); a[x]++; } for(int i = 200000;i >= 0;--i) a[i] += a[i + 1]; LL sum = 0; int res = 0; for(int i = 200000;i >= 0;--i){ if(a[i] == n) break; sum += a[i]; if(sum > k){ sum = 0;res++;i++; } } if(sum) res++; printf("%d\n",res); return 0; }