表象没问题,实则全WA

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

Ice_Fist @ 2023-08-10 11:47:49

#include<bits/stdc++.h>
using namespace std;
long long n,m,s,w,y;
int a[200010],b[200010],c[200010],d[200010],suml[200010],slq=0,sumz[200010],szq=0,ub[200010];
long long x=0;
int check(int p){
    suml[0]=0;
    sumz[0]=0;
    x=0;
    int i;
    slq=0;
    szq=0;
    for(i=1;i<=n;++i){
        suml[i]=suml[i-1];
        sumz[i]=sumz[i-1];
        if(a[i]>=p)++suml[i],sumz[i]+=b[i];
    }
    for(i=1;i<=m;++i){
        ub[i]=(suml[d[i]]-suml[c[i-1]])*(sumz[d[i]]-sumz[c[i-1]]);
    }
    for(i=1;i<=m;++i){
        x+=ub[i];
    }
    return x>=s;
}
int main(){
    cin>>n>>m>>s;
    for(int i=1;i<=n;++i)cin>>a[i]>>b[i];
    for(int i=1;i<=m;++i)cin>>c[i]>>d[i];
    int w,l=0,r=n;
    while(l==r){
    w=(l+r)/2;
        if(check(w))l=w;
        else r=w;
    }
    printf("%d",w);
    return 0;
}

咋办,有大佬支招吗???


by xxxxxzy @ 2023-08-10 11:55:46

@lmayyds 二分不对,大概长这样:

if(x<s) r=mid;
else if(x==s){
    cout<<x;
    exit(0);
}
else l=mid;

因为左右端点都有可能是答案


by Ice_Fist @ 2023-08-10 14:05:21

@C20252323tzy

请问该如何插入代码中

by xxxxxzy @ 2023-08-10 14:13:45

@lmayyds 看这里


|