/ zzf / 讨论 / 题解 /

聪明的质检员

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

const int N=200005,maxn=1000005;
long long n,m,s,w[N],v[N],l[N],r[N],f[N],g[N],ri=0,mid;
long long ans=0x3f3f3f3f3f3f;

bool check(long long W)
{
    long long res=0;
    for (int i=1;i<=n;i++)
    {
        if (w[i]>=W){
            f[i]=f[i-1]+1;
            g[i]=g[i-1]+v[i];
        }
        else{
            f[i]=f[i-1];
            g[i]=g[i-1];
        }
    }
    for (int i=1;i<=m;i++)
    {
        int a=l[i],b=r[i];
        res+=((f[b]-f[a-1])*(g[b]-g[a-1]));
    }
    if (ans>abs(s-res)) ans=abs(s-res); 
    if (s>res) return true;
    return false;
}
int main()
{
    scanf("%lld%lld%lld",&n,&m,&s);
    for (int i=1;i<=n;i++) 
    {
        scanf("%lld%lld",&w[i],&v[i]);
        if (w[i]>ri) ri=w[i];
    }
    ri++;
    for (int i=1;i<=m;i++) scanf("%lld%lld",&l[i],&r[i]);
    int left=0,right=ri;
    while (left<right)
    {
        mid=(left+right)/2;
        if(check(mid))
        {
            right=mid;
        } 
        else left=mid+1;
    }
    printf("%lld\n",ans);
    return 0;
} 

2 条评论

  • 1