STUDENT00 @ 2022-09-25 11:09:24
简单的二分+前缀和,结果我崩了!!!
#include<bits/stdc++.h>
using namespace std;
int n,m,s,l,r=1e8,ans=1e9,ql[200010],qr[200010],sum1[200010],sum2[200010];
struct node{
int w,v;
} a[200010];
bool cmp(node a,node b){
return a.w<b.w;
}
int main(){
scanf("%d%d%d",&n,&m,&s);
for(int i=1;i<=n;i++) scanf("%d%d",&a[i].w,&a[i].v);
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++) sum1[i]=sum1[i-1]+a[i].w;
for(int i=1;i<=n;i++) sum2[i]=sum2[i-1]+a[i].v;
for(int i=1;i<=m;i++) scanf("%d%d",&ql[i],&qr[i]);
while(l<=r){
int mid=(l+r)>>1;
int y=0,x=1;
while(x<=n&&a[x].w<mid) x++;
for(int i=1;i<=m;i++){
int ll=ql[i],rr=qr[i];
if(x<=rr){
if(x<ll) y+=(sum1[rr]-sum1[ll-1])*(sum2[rr]-sum2[ll-1]);
else y+=(sum1[rr]-sum1[x-1])*(sum2[rr]-sum2[x-1]);
}
}
ans=min(ans,abs(y-s));
if(y>=s) l=mid+1;
else r=mid-1;
}
printf("%d",ans);
return 0;
}
by zyzccc @ 2022-10-13 21:35:26
按题目的意思好像不能sort