【CodeForces - 460C】Present(二分+树状数组)
生活随笔
收集整理的這篇文章主要介紹了
【CodeForces - 460C】Present(二分+树状数组)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題干:
給定N朵花的原先的高度,從左到右排列,最多澆水m天,每天只能澆一次,每次使得連續的w朵花的高度增長1,問最后最矮的花的高度最高是多少。
Examples
Input
6 2 3 2 2 2 2 1 1Output
2Input
2 5 1 5 8Output
9Note
In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test.
解題報告:
? 直接二分即可。樹狀數組差分維護區間更新,復雜度O(nlognlogn),其實可以優化到nlogn,直接用一個變量維護增量即可。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 4e5 + 5; ll n,m,w,a[MAX]; ll c[MAX]; int lowbit(int x) {return x&-x;} ll sum(int x) {ll res = 0;while(x) {res += c[x];x -= lowbit(x);} return res; } void update(int x,ll val) {while(x < MAX) {c[x] += val;x += lowbit(x);} } bool ok(ll x) {ll cnt = 0,tmp;for(int i = 1; i<=n+w+1; i++) c[i]=0;for(int i = 1; i<=n; i++) {tmp = sum(i);if(a[i] + tmp < x) {cnt += (x-a[i]-tmp);update(i,x-a[i]-tmp);update(i+w,-(x-a[i]-tmp));}} return cnt <= m; } int main() {cin>>n>>m>>w; for(int i = 1; i<=n; i++) scanf("%lld",a+i);ll l = 0,r = 2e9,mid,ans;while(l<=r) {mid = (l+r)>>1;if(ok(mid)) l = mid+1,ans = mid;else r = mid-1;}printf("%lld\n",ans);return 0 ; }?
總結
以上是生活随笔為你收集整理的【CodeForces - 460C】Present(二分+树状数组)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十年前,我国GDP超过日本成为全球第二,
- 下一篇: 厉害了!液态金属也可以有磁性 中国科学家