zoukankan      html  css  js  c++  java
  • hdu 2141:Can you find it?(数据结构,二分查找)

    Can you find it?

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


    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
     
    Recommend
    威士忌   |   We have carefully selected several similar problems for you:  2199 2899 2289 1597 1551 
     
      数据结构:二分(折半)查找。
      这道题一开始还在纳闷怎么用二分查找,后来看人家的思路才发现需要用巧办法,做法是先将前两个数列相加产生sab数列,这个时候sab+c = x,那么sab= x-c,每次询问x时,就用x减去c中的所有数,依次在sab数列中查找。
      没想到这样的方法,脑子还是太木,不甘心啊 >_<
      本题代码:
     
     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 int qn;    //sab数组总数 
     5 int sab[250005];
     6 int binsearch(int q[],int n,int k)    //二分查找 
     7 {
     8     int left=1,right=n,mid;
     9     while(left<=right){
    10         mid = (left+right)/2; 
    11         if(q[mid]==k)
    12             return mid;
    13         if(q[mid]>k)
    14             right = mid - 1;
    15         else
    16             left = mid + 1;
    17     }
    18     return 0;
    19 }
    20 int main()
    21 {
    22     int l,n,m;
    23     int count = 1;
    24     while(cin>>l>>n>>m){
    25         qn = 1;
    26         int A[501],B[501],C[501];
    27         for(int i=1;i<=l;i++){
    28             cin>>A[i];
    29         }
    30         for(int i=1;i<=n;i++){
    31             cin>>B[i];
    32         }
    33         for(int i=1;i<=m;i++){
    34             cin>>C[i];
    35         }
    36         for(int i=1;i<=l;i++)
    37             for(int j=1;j<=n;j++)
    38                 sab[qn++] = A[i] + B[j];    //产生sab数列 
    39         sort(sab+1,sab+qn-1);    //对sab数列进行排序 
    40         int s;
    41         cin>>s;
    42         cout<<"Case "<<count++<<":"<<endl;
    43         while(s--){
    44             int t;
    45             cin>>t;
    46             int i;
    47             for(i=1;i<=m;i++){
    48                 int tt = t - C[i];
    49                 if(binsearch(sab,qn-1,tt)){    //查找有没有 x-c 
    50                     cout<<"YES"<<endl;
    51                     break;
    52                 }
    53             }
    54             if(i>m)
    55                 cout<<"NO"<<endl;
    56         }
    57     }
    58     return 0;
    59 }

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    模糊查询的like '%$name$%'的sql注入避免
    在VS2010中使用Git(转)
    android WebView解析 调用html5
    git
    推荐!手把手教你使用Git
    羽毛拍十大品牌
    乒乓球拍板和皮子世界排名
    足球小记
    centos 网络启动 在/etc/sysconfig/network-scripts/ifcfg-eth1onboot=yes即可
    MyEclipseGen--------生成
  • 原文地址:https://www.cnblogs.com/yym2013/p/3548711.html
Copyright © 2011-2022 走看看