HDU 2141 Can you find it? [二分]

Can you find it?

Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.

你以为我是因为被$world final$打击到了而在做水题吗?
没错我就是在做水题.........mhy12345课件上的.....
每个询问枚举$A$然后二分$B+C$
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=505,M=N*N;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}
int a[N],b[N],c[N];
int w[M],n;
int Bin(int v){//printf("Bin %d\n",v);
    int l=1,r=n;
    while(l<=r){
        int mid=(l+r)>>1;//printf("%d %d %d  %d %d\n",l,r,mid,w[mid],v);
        if(w[mid]==v) return mid;
        else if(v<w[mid]) r=mid-1;
        else l=mid+1;
    }
    return 0;
}
int main(){
    freopen("in","r",stdin);
    int cas=0;
    while(scanf("%d%d%d",a,b,c)!=EOF){
        printf("Case %d:\n",++cas);
        for(int i=1;i<=(*a);i++) a[i]=read();
        for(int i=1;i<=(*b);i++) b[i]=read();
        for(int i=1;i<=(*c);i++) c[i]=read();
        n=0;
        for(int i=1;i<=(*b);i++)
            for(int j=1;j<=(*c);j++) w[++n]=b[i]+c[j];
        sort(w+1,w+1+n);
        int Q=read();
        while(Q--){
            int x=read(),flag=0;
            for(int i=1;i<=(*a);i++) if(Bin(x-a[i])) {flag=1;break;}
            if(flag) puts("YES");
            else puts("NO");
        }
    }
}

 

 
 
 
posted @ 2017-02-22 18:38  Candy?  阅读(211)  评论(0编辑  收藏  举报