zoukankan      html  css  js  c++  java
  • hdu 2141 二分查找,三个数相加

    计算A[i]+B[j]+C[k]=x

    转换为A[i]+B[i]=x-C[k] ,这样在A[i]+B[i]中二分查找,x-C[k] ,不会超时。

    题目来源:

    http://acm.hdu.edu.cn/showproblem.php?pid=2141

    Can you find it?

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


    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
     
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string>
     4 #include<string.h>
     5 #include<algorithm>
     6 
     7 using namespace std;
     8 int a[505],b[505],c[505];
     9 int sab[250005];
    10 bool Bi_Search(int a[],int n,int b)
    11 {
    12     int l=0;
    13     int r=n-1;
    14     int mid;
    15     while(l<=r)
    16     {
    17         mid=(l+r)>>1;
    18         if(a[mid]==b)
    19             return 1;
    20         else if(a[mid]>b)
    21             r=mid-1;
    22         else
    23             l=mid+1;
    24     }
    25     return 0;
    26 }
    27 int main()
    28 {
    29     int L,M,N;
    30         int i,j,k;
    31         int s;
    32         int q,con=1;
    33 
    34     while(cin>>L>>M>>N)
    35     {
    36         for( i=0;i<L;i++)
    37             cin>>a[i];
    38         for( j=0;j<M;j++)
    39             cin>>b[j];
    40         for( k=0;k<N;k++)
    41             cin>>c[k];
    42         for(k=0,i=0;i<L;i++)
    43             for(j=0;j<M;j++)
    44             {
    45                 sab[k++]=a[i]+b[j];
    46             }
    47         sort(sab,sab+k);
    48 
    49         cin>>s;
    50         cout<<"Case "<<con++<<":"<<endl;
    51         while(s--)
    52         {
    53             cin>>q;
    54             for(j=0;j<N;j++)
    55             {
    56                 if(Bi_Search(sab,k,q-c[j]))
    57                    break;
    58             }
    59             if(j==N)
    60                 cout<<"NO"<<endl;
    61             else
    62                 cout<<"YES"<<endl;
    63         }
    64     }
    65     return 0 ;
    66 }
  • 相关阅读:
    网站SEO关键词优化技巧
    SEO操作流程及网站优化技巧
    Linux服务器工作常用命令总结
    【转载】Linux常用命令大全(非常全!!!)
    myBatis出现Mapped Statements collection already contains value for
    maven打成war包之后没有class文件
    查询每个类型最新的一条记录
    关于 MySQL 的 boolean 和 tinyint(1) (转)
    Mac下的eclipse按住ctrl点击无法查看类文件
    Mac 10.10下安装MySQL5.6.21提示安装失败
  • 原文地址:https://www.cnblogs.com/zn505119020/p/3557846.html
Copyright © 2011-2022 走看看