C. Present(思维+二分+差分/线段树/树状数组)

https://codeforces.com/problemset/problem/460/C


思路:

考虑维护到i时候,i前面的已经维护好了,因此对其后面进行区间的加减

由于每个数如果没有>=x,就要添加,同时给后面区间++。差分的过程中对单纯的下一个已更新的值可以边扫边维护。

当然也可以树状数组/线段树维护区间和以及单点查询

强行1e16爆ll了。换成了128

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL cnt[maxn],a[maxn],b[maxn];
bool check(LL x,LL n,LL m,LL w){
    for(LL i=1;i<=n;i++) cnt[i]=a[i]-a[i-1];
    __int128 sum=0;
    for(LL i=1;i<=n;i++){
       cnt[i]+=cnt[i-1];
       if(cnt[i]<x){
          __int128 k=x-cnt[i];
          sum+=k;
          cnt[i]+=k;
          if(i+w<=n) cnt[i+w]-=k;
       }
    }
    if(sum<=m) return true;
    else return false;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m,w;cin>>n>>m>>w;
  for(LL i=1;i<=n;i++){
    cin>>a[i];
    b[i]=a[i];
  }
  LL l=0;LL r=1e16;
  while(l<r){
    LL mid=(l+r+1)>>1;
    if(check(mid,n,m,w)) l=mid;
    else r=mid-1;
    for(LL i=1;i<=n;i++) a[i]=b[i];
  }
  cout<<l<<"\n";
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/114945544