15 pts求助

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

xzabc123 @ 2022-07-01 23:01:53

#include <bits/stdc++.h>
using namespace std;
#define MAXN 200200
FILE *fin,*fout;
int n,m,i,j,max_n= -1,min_n=1e6;
int le[MAXN],ri[MAXN],nn[MAXN],v[MAXN];
long long pre_n[MAXN],pre_v[MAXN],s;
long long Y,sum,ans;

bool check(int w){
    Y=0,sum=0;
    memset(pre_n,0,sizeof(pre_n));
    memset(pre_v,0,sizeof(pre_v));

    for(j=1;j<=n;j++){
        if(nn[j]>=w) {  // 
            pre_v[j]=pre_v[j-1]+v[j]; pre_n[j]=pre_n[j-1]+1;
        }
        else{
            pre_v[j]=pre_v[j-1]; pre_n[j]=pre_n[j-1];
        }
    }
    for(j=1;j<=m;j++){

       Y+= (pre_n[ri[j]] - pre_n[le[j]-1]) * (pre_v[ri[j]] - pre_v[le[j]-1]);
    }
    sum= llabs(Y - s);
    if(Y>s)  return 1;
    else return 0;
} 
int main(){
    fin=stdin;
    fout=stdout;

    fscanf(fin,"%d%d%lld",&n,&m,&s);
    for(i=1;i<=n;i++){
        fscanf(fin,"%d%d",&nn[i],&v[i]); //nn表示重量 
        max_n=max(max_n,nn[i]); 
        min_n=min(min_n,nn[i]);

    }
    for(i=1;i<=m;i++){
        fscanf(fin,"%d%d",&le[i],&ri[i]);

    }
    //int mid= 1<<32; //初赋值  
    int l=min_n - 1,r=max_n + 2,mid;
    ans = 1e6;
    while(l<=r){
        mid=(l+r)>>1;

        if(check(mid)) 
        {  //mid就是  W 的最小值,当 | 较大时,增加 l ,mid增加,y减小 | 增加 
        l=mid+1; }
        else {
            r=mid-1;
        }
        if(sum<ans) {
        ans=sum;} 

    }

    fprintf(fout,"%lld",ans);
    fclose(fin);fclose(fout);
    return 0;
}

by pyx20091202 @ 2023-01-18 12:57:55

ans小了


by pyx20091202 @ 2023-01-18 12:58:55

我把你的ans改成1e8得45


by pyx20091202 @ 2023-01-18 12:59:35

其他就不知道了我蒟蒻


|