HDU2141-Can you find it?

来源:互联网 发布:递归算法反汇编 编辑:程序博客网 时间:2024/06/17 18:06

Can you find it?

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 29478 Accepted Submission(s): 7353

Problem Description
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.

Input
There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.

Output
For each case, firstly you have to print the case number as the form “Case d:”, then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print “YES”, otherwise print “NO”.

Sample Input
3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10

Sample Output
Case 1:
NO
YES
NO

Author
wangye

Source
HDU 2007-11 Programming Contest

题目大意:给出ABC三个数组,判断能否找到一个三元组满足Ai+Bi+Ci=X
解题思路:求出A+B,并排序,二分查找X-Ci

#include<iostream>#include<cstring>#include<cmath>#include<ctime>#include<cstdlib>#include<algorithm>#include<iomanip>#include<fstream>#include<map>using namespace std;const int MAXN=510;const int MAXM=250005;int a[MAXN],b[MAXN],c[MAXN];int d[MAXM];int k;bool bs(int key){    int L=1,R=k;    int M;    while(L<=R)    {        M=(L+R)/2;        if(key==d[M]) return true;        else if(key<d[M]) R=M-1;        else L=M+1;    }    return false;}int main(){    int L,N,M;    int s;    int cas=1;    while(cin>>L>>N>>M)    {        for(int i=1;i<=L;i++)        {            cin>>a[i];        }        for(int i=1;i<=N;i++)        {            cin>>b[i];        }        for(int i=1;i<=M;i++)        {            cin>>c[i];        }        k=0;        for(int i=1;i<=L;i++)        {            for(int j=1;j<=N;j++)            {                k++;                d[k]=a[i]+b[j];            }        }        sort(d+1,d+k+1);        /*for(int i=1;i<=k;i++)            cout<<d[i]<<" ";        cout<<endl;*/        cin>>s;        int x;        cout<<"Case "<<cas++<<":"<<endl;        for(int i=1;i<=s;i++)        {            cin>>x;            bool flag=false;            for(int i=1;i<=M;i++)            {                if(bs(x-c[i]))                {                    flag=true;                    break;                }            }            if(flag) cout<<"YES"<<endl;            else cout<<"NO"<<endl;        }    }}
0 0
原创粉丝点击