做法:二分+差分
思路:
最小值最大化-->二分
然后利用差分来进行二分操作,具体见check函数代码
代码
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp(aa,bb) make_pair(aa,bb)
#define _for(i,b) for(int i=(0);i<(b);i++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define X first
#define Y second
#define lowbit(a) (a&(-a))
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1.0);
int n,m;
ll w,a[N],l=INF,r,ans,d[N];
bool check(ll mid){
rep(i,1,n) d[i]=a[i]-a[i-1];
ll x=0,cnt=0; //x:处理的数 cnt:加的次数
rep(i,1,n){
x+=d[i];
if(x<mid){
ll k=mid-x;//加上的数
cnt+=k;
x+=k;
d[i]+=k;
if(i+w<=n) d[i+w]-=k;//越界
}
}
return cnt<=m;
}
void solve(){
cin>>n>>m>>w;
rep(i,1,n) cin>>a[i],l=min(a[i],l);
r=l+m;
while(l<=r){
ll mid=(l+r)>>1;
if(check(mid)){
ans=mid;
l=mid+1;
}
else r=mid-1;
}
cout<<ans<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
#ifdef DEBUG
freopen("F:/laji/1.in", "r", stdin);
// freopen("F:/laji/2.out", "w", stdout);
#endif
// int t;cin>>t;while(t--)
solve();
return 0;
}
京公网安备 11010502036488号