样例总是输出15,求dalao差错_(:зゝ∠)_

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

Dog_Two @ 2017-12-24 20:51:10

#include<bits/stdc++.h>
#define l(i) lr[i].first
#define r(r) lr[i].second
using namespace std;
const int maxn=2e5+10;
int n,m,S;
int w[maxn],v[maxn];
pair<int,int>lr[maxn];
int cnt[maxn];
long long sum[maxn];
int ans=0x3f3f3f3f;
//重量&价值 
bool check(int mid){
    memset(cnt,0,sizeof(cnt));memset(sum,0,sizeof(sum));
    long long res=0;
    for(int i=1;i<=n;i++){
        cnt[i]=cnt[i-1]+(w[i]>=mid);
        sum[i]=sum[i-1]+(long long)((w[i]>=mid)*(v[i]));
//        printf("debug:cnt[%d]=%d sum[%d]=%lld\n",i,cnt[i],i,sum[i]);
    }
    for(int i=i;i<=m;i++){
        res+=(cnt[r(i)]-cnt[l(i)-1])*(sum[r(i)]-sum[l(i)-1]);
//        printf("debug:res=%lld\n",res);
    }
    ans=min(ans,(int)abs((long long)(res-S)));
    return res>=S;
}
int main(){
    cin>>n>>m>>S;
    int tmp=0;
    for(int i=1;i<=n;i++) 
        scanf("%d%d",&w[i],&v[i]),tmp=max(tmp,w[i]);
    int l,r;
//    for(int i=1;i<=n;i++) printf("debug:%d %d\n",w[i],v[i]);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&l,&r);
        lr[i]=make_pair(l,r);
    }
//    for(int i=1;i<=m;i++) printf("debug:%d %d\n",l(i),r(i));
    l=0,r=tmp;
    while(l<=r){
        int W=(l+r)/2;
        if(check(W)) l=W+1;
        else r=W-1;        
    }
    printf("%d",ans);
    return 0;
}

/* 给出n个有序数值 m个区间 S作为基数

要求 给出一个最接近S的Y

二分:W

当W偏小时,Y偏大——W取较大

当W偏大时,Y偏小——W取较小

*/


by miemieQWQ @ 2017-12-24 21:58:21

check里, 第二个for, i = i 什么鬼?


by miemieQWQ @ 2017-12-24 21:58:47

@Dog_Two


by Dog_Two @ 2017-12-24 22:12:11

@yxr811740686 _(:зゝ∠)_大佬眼尖!我跟队友看了半天才看到。%%%大佬


|