luogu 1314 聪明的质检员

二分答案的边界问题还是要注意

double挨着,int+1-1,

此题用到long long,所以初始化ans要足够大,前缀和优化

依然根据check答案大小左右mid,虽然有s,但是有了+1-1加持所以能够自动推出

#include<bits/stdc++.h>
#define int long long
#define rep(i,x,y) for(register int i=x;i<=y;i++)
using namespace std; const int N=2e6+;
int n,m,s,mi,mx,ans,w[N],v[N],l[N],r[N]; inline int read(){
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+(ch^);ch=getchar();}
return x*f;} inline int aabs(int x){return x>?x:-x;} int sum[N],cnt[N]; bool check(int mid){int y=;
memset(sum,,sizeof sum);
memset(cnt,,sizeof cnt); rep(i,,n){
sum[i]=sum[i-],cnt[i]=cnt[i-];
if(w[i]>=mid)
sum[i]+=v[i],cnt[i]++;
}rep(i,,m)
y+=(cnt[r[i]]-cnt[l[i]-])*(sum[r[i]]-sum[l[i]-]);
ans=min(ans,aabs(y-s));
if(y>s) return ;
else return ;
} signed main(){
n=read(),m=read(),s=read();ans=mi=;
rep(i,,n) w[i]=read(),v[i]=read(),mi=min(mi,w[i]),mx=max(mx,w[i]);
rep(i,,m) l[i]=read(),r[i]=read();
int l=mi,r=mx;
while(l<=r)
{
int mid=(l+r)>>;
bool FG=check(mid); if(FG) l=mid+;
else r=mid-;
}
printf("%lld\n",ans);return ;
}

怀挺

上一篇:Bootstrap中内联单选按钮


下一篇:linux发行版基础目录