hdu 2141

来源:互联网 发布:软件开发规划 编辑:程序博客网 时间:2024/05/24 05:13

//这题的解题思路是将a b  中的元素每种相加可能的值都存放于一个数组中,如a[]={1,2,3};b={1,2} 则 ab[]={2,3,3,4,4,5};
//将ab[]中的元素进行升序排序
//将要查找的值分别减去c[]中的一个元素,然后再在ab[]中用二分查找的方式进行搜索,这样要搜索的值就有c[]元素的个数
//只要有一个值能在ab[]中查找成功,则输出 yes  反之 输出 no


// 468 ms   1940 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;


vector<int>  ab;
int search(int search_num){


   int low=0;
   int high=ab.size()-1;
   int mid;
   while(low<=high){
      mid=(low+high)/2;
     
      if(search_num<ab[mid]) high=mid-1;
     
      if(search_num>ab[mid]) low=mid+1;
     
      if(search_num==ab[mid]) return 1;             
   }
  return 0;
}
int main(){
    int x;
    vector<int> a;
    vector<int> b;


    int l,n,m;


    int s;
    int cases=0;
     a.reserve(500);
     b.reserve(500);
     ab.reserve(250000);
    
     int c[550];
  while(cin>>l>>n>>m){
                     
      a.clear();
      b.clear();
      ab.clear();
      for(int i=0;i<l;i++){
         scanf("%d",&x);
         a.push_back(x);    
      }
      for(int i=0;i<n;i++){
         scanf("%d",&x);
         b.push_back(x);    
      }
      for(int i=0;i<m;i++){
         scanf("%d",&c[i]);    
      }
      for(int i=0;i<l;i++)
        for(int j=0;j<n;j++)
          ab.push_back(a[i]+b[j]);
         
      sort(ab.begin(),ab.end());
     
      cout<<"Case "<<++cases<<":"<<endl;
      cin>>s;
      for(int i=0;i<s;i++){
          scanf("%d",&x);
          int flag;
            for(int j=0;j<m;j++){
               
                flag=search(x-c[j]);
                if(flag) break;      
            }                
             if(flag)
                cout<<"YES"<<endl; 
             else cout<<"NO"<<endl;                       
      }                     
  }
  return 0;   
}

Can you find it?

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


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

原创粉丝点击