70分代码求救!!

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

xhxxwcr @ 2024-05-17 12:31:52

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int Sw[MAXN],Sv[MAXN],w[MAXN],v[MAXN],l[MAXN],r[MAXN],n,m;
long long s,sum,ans;
int mx = -1,mn = 2147483647;
bool F(int x){
    memset(Sw,0,sizeof(Sw));
    memset(Sv,0,sizeof(Sv));
    sum = 0;
    ans = 0;
    for(int i = 1;i <= n;i++){
        Sw[i] = Sw[i - 1] + (w[i] >= x ? 1 : 0);
        Sv[i] = Sv[i - 1] + ((w[i] >= x ? 1 : 0) * v[i]);
    }
    for(int i = 1;i <= n;i++){
        ans += (Sw[r[i]] - Sw[l[i] - 1]) * (Sv[r[i]] - Sv[l[i] - 1]);
    }
    sum = llabs(ans - s);
    return (ans > s);
}
int main(){
    cin >> n >> m >> s;
    for(int i = 1;i <= n;i++){
        cin >> w[i] >> v[i];
        mx = max(mx,w[i]);
        mn = min(mn,w[i]);
    }
    for(int i = 1;i <= m;i++){
        cin >> l[i] >> r[i];
    }
    int l = mn - 1,r = mx + 2,mid;
    long long rex = 0x3f3f3f3f3f3f3f3f;
    while(l <= r){
        mid = (l + r) >> 1;
        if(F(mid))  l = mid + 1;
        else  r = mid - 1;
        if(sum < rex)  rex = sum;
    }
    cout << rex << endl;
    return 0;
}

by Lawrenceling @ 2024-06-12 21:44:09

long long


|