hdu 2141 Can you find it?(二分查找变例)

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

初看很简单就是判断三个数加起来的和,但是三个for循环速度还是很慢的,所以细想要用到二分查找。

把函数改为:A+B=X-C,然后二分搜一下就可以了。

完全用的是二分查找的模板。

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <queue>
 4 #include <vector>
 5 #include <stack>
 6 #include <map>
 7 #include <string>
 8 #include <string.h>
 9 #include <algorithm>
10 #include <iostream>
11 using namespace std;
12 #define K 505
13 int LN[K*K];
14 int binarysearch(int a[],int l,int r,int k){
15     int mid;
16     while(r-l>1){
17         mid=(r+l)/2;
18         if(a[mid]<=k)
19             l=mid;
20         else
21             r=mid;
22     }
23     if(a[l]==k)
24         return 1;
25     else
26         return 0;
27 }//二分查找
28 int main()
29 {
30     int i,j,count=1,q;
31     int L[K],N[K],M[K],s,n,m,l;
32     while(~scanf("%d%d%d",&l,&n,&m)){
33         int h=0;
34         for(i=0;i<l;i++)
35             scanf("%d",&L[i]);
36         for(i=0;i<n;i++)
37             scanf("%d",&N[i]);
38         for(i=0;i<m;i++)
39             scanf("%d",&M[i]);
40         for(i=0;i<l;i++)
41           for(j=0;j<n;j++)
42              LN[h++]=L[i]+N[j];
43          sort(LN,LN+h);
44          scanf("%d",&s);
45          printf("Case %d:\n",count++);
46         for(i=0;i<s;i++)
47         {
48             scanf("%d",&q);
49             int p=0;
50             for(j=0;j<m;j++)
51             {
52                 int a=q-M[j];
53                 if(binarysearch(LN,0,h,a))
54                 {
55                     printf("YES\n");
56                     p=1;
57                     break;
58                 }
59             }
60             if(!p)
61               printf("NO\n");
62         }
63     }
64     return 0;
65 }
时间: 2025-01-21 21:33:53

hdu 2141 Can you find it?(二分查找变例)的相关文章

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): 21485    Accepted Submission(s): 5446 Problem Description Give you three sequences of numbers A, B, C, then we give you a number

hdu 4938 Seeing People 排序+二分查找

Seeing People Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 241    Accepted Submission(s): 61 Problem Description There are two kinds of people. If person i is the first kind of people, it

hdu 4715 Difference Between Primes (二分查找)

Problem Description All you know Goldbach conjecture.That is to say, Every even integer greater than 2 can be expressed as the sum of two primes. Today, skywind present a new conjecture: every even integer can be expressed as the difference of two pr

hdu 4190 Distributing Ballot Boxes(贪心+二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4190 Distributing Ballot Boxes Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1065    Accepted Submission(s): 536 Problem Description Today, bes

HDU - 2141 Can you find it?(二分)

题目链接:点我点我 题意:给出L个A,N个B,M个C,然后S个X,求能否找出解使得Ai+Bj+Ck = X.成立 题解:TLE了一晚上.这道题其实就是个暴力二分,前把三组数中任意两组数先合并,只不过最后的时候转变一下思想,不要直接去求使X成立的条件, 而是反过去把X当作条件(但是有些大神直接set+set的find()函数过了,当我没说,(捂脸逃....) 1.巧妙暴力二分 1 #include <cstdio> 2 #include <algorithm> 3 using nam

HDU 3280 Equal Sum Partitions(二分查找)

Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 551    Accepted Submission(s): 409 Problem Description An equal sum partition of a sequence of numbers is a grouping of the

HDU 5265 pog loves szh II (二分查找)

[题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog loves szh II ) Judge Status : Accepted RunId : 13961817 Language : G++ Author : javaherongwei Code Render Status : Rendered By HDOJ G++ Code Render Versio

hdu 2141 Can you find it?(二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. 1 /* 2 3 x^2+6*x-7==y 4 输入y 求x 精确度为10^-5 5 0=<x<=10000 6 7 */ 8 #include <iostream> 9 #include <cstdio> 10 using namespace std; 11 int main (void) 1

二分查找 HDOJ 2141 Can you find it?

题目传送门 1 /* 2 题意:给出一个数,问是否有ai + bj + ck == x 3 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAX