hdu2141Can you find it?

来源:互联网 发布:矩阵led大灯 编辑:程序博客网 时间:2024/05/16 09:14

 

Can you find it?

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

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 31 2 31 2 31 2 331410
 

 

Sample Output
Case 1:NOYESNO
、、、、、、、、、、、、、、、
晚上刷了道搜索题,这道题相对简单,可以算水题吧。。找到三个数,使A+B+C=X;只要转换下A+B=X-C,然后二分搜一下就可以了;用到的也就是二分查找和快速排序。。
还是花了点时间,运行结果是对的,提交就是wa,把一个地方去掉了,就ac了。
<code>
#include<iostream>
#include<algorithm>
using namespace std;
int l,m,n,s;
__int64 a[505],b[505],c[505],sum[250050];
__int64 cnt;
bool BinarySearch(__int64 x)
{
     int left=0;
     int right=l*n-1;
     while(left<=right)
     {
           int middle=(left+right)/2;
           if(x==sum[middle])return true;
           if(x>sum[middle])left=middle+1;
           else  right=middle-1;
     }
     return false;
}
int main()
{
    int i,j,k,x;
    int ca=0;
    while(cin>>l>>n>>m)
    {
         for(i=0;i<l;i++)
         cin>>a[i];
         for(i=0;i<n;i++)
         cin>>b[i];
         for(i=0;i<m;i++)
         cin>>c[i];
         cnt=0;
         for(i=0;i<l;i++)
           for(j=0;j<n;j++)
           {
              sum[cnt++]=a[i]+b[j];
           }
         sort(sum,sum+cnt);
         sort(c,c+m);
         cin>>s;
         printf("Case %d:/n",++ca);
         while(s--)
         {
              cin>>x;
              int flag=0;
              for(i=0;i<m;i++)
              {
                  //if(c[i]>x)break;
                  int y=x-c[i];
                  flag=BinarySearch(y);
                  if(flag)break;
              }
              if(flag==1)cout<<"YES"<<endl;
              else       cout<<"NO"<<endl;
         }
    }return 0;
}
                 
           
        
   
原创粉丝点击