hdu 2141

来源:互联网 发布:pdf锐化软件 编辑:程序博客网 时间:2024/05/22 18:07
  
分组+二分 emmm....要sort 啊!!!zz.......
 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 31 2 31 2 31 2 331410
Sample Output
Case 1:NOYES

NO

#include<iostream>#include<algorithm>using namespace std;int sum1[250005];int sum2[500005];int l,n,m,d,s,a,pos;int A[505],B[505],C[505];int main(){    int d=0;    while(cin>>l>>n>>m)    {        d++;        int k=0;        for(int i=0; i<l; i++)cin>>A[i];        for(int i=0; i<n; i++)cin>>B[i];        for(int i=0; i<m; i++)cin>>C[i];        for(int i=0; i<l; i++)        {            for(int j=0; j<n; j++)            {                sum1[k++]=B[j]+A[i];            }        }        sort(sum1,sum1+k);        cin>>s;        cout<<"Case "<<d<<":"<<endl;        while(s--)        {            cin>>a;            int flag=0;            for(int i=0; i<m; i++)            {                pos=a-C[i];                int l=0,r=k-1,mid;                while(r-l>=0)                {                    mid=(l+r)/2;                    if(sum1[mid]==pos)                    {                        flag=1;                        break;                    }                    if(sum1[mid]<pos)l=mid+1;                    else r=mid-1;                }            }            if(flag==1)cout<<"YES"<<endl;            else cout<<"NO"<<endl;        }    }    return 0;}


原创粉丝点击