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): 35836    Accepted Submission(s): 8845


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 
 
题意:给你三个数组,再给你一些数,判断三个数组内是否有Ai+Bi+Ci==x.如果有输出YES,否则输出NO、
思路:先将前两个数组两两相加构成一个新数组,快排。然后新数组和第三个数组相加,二分,不断用中间的和第三个数组的元素相加,直到得x或者跳出循环为止。
 
 1 #include<cstdio>  
 2 #include<iostream>  
 3 #include<algorithm>  
 4 #include<cmath>  
 5 #define xps 1e-12  
 6 using namespace std;
 7 long long a[510], b[510], c[510], d[250010];
 8 int main()
 9 {
10     long long l, m, n, o = 1;
11     while (cin >> l >> m >> n)
12     {
13         for (int i = 0; i<l; i++)//输入三个数组   
14             cin >> a[i];
15         for (int i = 0; i<m; i++)
16             cin >> b[i];
17         for (int i = 0; i<n; i++)
18             cin >> c[i];
19         int k = 0;
20         for (int i = 0; i<l; i++)
21             for (int j = 0; j<m; j++)
22                 d[k++] = a[i] + b[j];//前两个数组两两相加得到新数组   
23         sort(d, d + k);//从小到大排序   
24         int s;
25         cin >> s;
26         cout << "Case " << o++ << ":" << endl;//输出第几个测试实例   
27         while (s--)
28         {
29             long long x;
30             cin >> x;
31             bool sign = false;
32             for (int i = 0; i<n; i++)//第三个数组和新数组mid相加判断   
33             {
34                 int l = 0, r = k - 1;
35                 while (l <= r)
36                 {
37                     int mid = (l + r) / 2;
38                     if (d[mid] + c[i] == x)
39                     {
40                         sign = 1;
41                         break;
42                     }
43                     else if (d[mid] + c[i] < x)
44                     {
45                         l = mid+1;
46                     }
47                     else r = mid-1;
48                 }
49                 if (sign) break;
50             }
51             if (sign)
52                 cout << "YES" << endl;
53             else
54                 cout << "NO" << endl;
55         }
56     }
57     return 0;
58 }

 

posted on 2018-02-13 14:53  蔡军帅  阅读(133)  评论(0编辑  收藏  举报