80分求助,请大佬帮帮忙

P1314 [NOIP2011 提高组] 聪明的质监员

oilgz @ 2022-06-22 11:17:57

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
const int N=2e5+10,M=1e6+10;
int n,m,k;
int w[N],v[N],l[N],r[N];
int s[N],sv[N];
int check(int x){
    memset(s,0,sizeof s);
    memset(sv,0,sizeof sv);
    for(int i=1;i<=n;i++){
        if(w[i]>=x)s[i]++,sv[i]+=v[i];
        s[i]+=s[i-1];
        sv[i]+=sv[i-1];
    }
    int ans=0;
    for(int i=1;i<=m;i++){
        ans+=(s[r[i]]-s[l[i]-1])*(sv[r[i]]-sv[l[i]-1]);
    }
    return ans;

}
signed main(){
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)cin>>w[i]>>v[i];
    for(int i=1;i<=n;i++)cin>>l[i]>>r[i];
    int l=0,r=1e9;
    int ans=1e9;
    while(l<r){
        int mid=(l+r)>>1;
        if(check(mid)<=k)r=mid;
        else l=mid+1;
    }
    ans=k-check(l);
    l=0,r=1e9;
    while(l<r){
        int mid=(l+r+1)>>1;
        if(check(mid)>=k)l=mid;
        else r=mid-1;
    }
    cout<<min(ans,check(l)-k);
}

by oilgz @ 2022-06-23 14:41:21

解决了


|