日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 460C】Present(二分+树状数组)

發(fā)布時(shí)間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 460C】Present(二分+树状数组) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

給定N朵花的原先的高度,從左到右排列,最多澆水m天,每天只能澆一次,每次使得連續(xù)的w朵花的高度增長(zhǎng)1,問(wèn)最后最矮的花的高度最高是多少。

Examples

Input

6 2 3 2 2 2 2 1 1

Output

2

Input

2 5 1 5 8

Output

9

Note

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.

解題報(bào)告:

? 直接二分即可。樹狀數(shù)組差分維護(hù)區(qū)間更新,復(fù)雜度O(nlognlogn),其實(shí)可以優(yōu)化到nlogn,直接用一個(gè)變量維護(hù)增量即可。

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 ; }

?

總結(jié)

以上是生活随笔為你收集整理的【CodeForces - 460C】Present(二分+树状数组)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。