三分!!95分!WA 3#!求助

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

Roviky @ 2019-10-30 20:10:37

#include <bits/stdc++.h>
using namespace std;
const int maxn=211111;
long long read(){
    long long res=0;int f=1,c=getchar();
    while(c!='-'&(c>'9'||c<'0'))c=getchar();
    if(c=='-'){f=-1,c=getchar();}
    while(c>='0'&&c<='9'){res=(res<<3)+(res<<1)+(c^48);c=getchar();}
    return res*f;
}
long long n,m,ans=2e12,s,a[maxn],cnt[maxn],w[maxn],v[maxn],li[maxn],ri[maxn];
long long check(long long x){
    for(int i=1;i<=n;++i){
        if(w[i]>=x){
            a[i]=a[i-1]+v[i];
            cnt[i]=cnt[i-1]+1;
        }else{
            a[i]=a[i-1];
            cnt[i]=cnt[i-1];
        }
    }
    long long res=0;
    for(int i=1;i<=m;++i){
        int ll=li[i],rr=ri[i];
        res+=(cnt[rr]-cnt[ll-1])*(a[rr]-a[ll-1]);
    }
    return abs(res-s);
}
long long l=999999999,r=-999999,lmid,rmid;
int main(){
    //freopen("1.cpp","r",stdin);
    n=read();m=read();s=read();
    for(int i=1;i<=n;++i){
        w[i]=read();
        v[i]=read();
        r=max(r,w[i]);
        l=min(l,w[i]);
    }l--;r+=3;
    for(int i=1;i<=m;++i){
        li[i]=read();
        ri[i]=read();
    }
    while(l+2<r){
        rmid=(l+2*r)/3;
        lmid=(2*l+r)/3;
        //cout<<l<<" "<<lmid<<" "<<rmid<<" "<<r<<endl;
        //cout<<check(lmid)<<" with "<<check(rmid)<<endl;
        long long lc=check(lmid);
        long long rc=check(rmid);
        ans=min(ans,lc);
        ans=min(ans,rc);
        ans=min(ans,check(l));
        ans=min(ans,check(r));
        if(lc>rc){
            l=lmid;
        }else{
            r=rmid;
        }
    }
    ans=min(ans,min(min(check(l),check(lmid)),min(check(rmid),check(r))));
    //for(int i=1;i<=n;++i)ans=min(ans,check(w[i]));
    printf("%lld",ans);
    return 0;
} 

by aRenBigFather @ 2019-10-31 07:48:22

Leave My Name


by 雪颜 @ 2019-11-07 09:12:23

窝三分只有90 QAQ


by yangshurong @ 2019-11-12 17:29:50

我也WA的这个点


by Butane @ 2022-05-06 22:01:33

因为函数不是严格的单谷函数,即对于不同的三分值可能存在相同的函数值,所以不能用三分


by coroutines @ 2023-12-04 23:04:09

@Butane 你是真正的英雄,这怎么可能想得到。。。


|