hdu-2141

Can you find it?

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

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

题意:给出三个数列ABC,从ABC各取一个整数求是否能够等于X,是则输出YES,否NO。

三组500直接查找会超时

可将A+B+C=X转换为A+B=X-C

之后即可将等式两边看做两个整体进行二分查找。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 int a[510],b[510],c[510];
 5 int num[250010];
 6
 7 int bin(int q[],int x,int y){
 8     int left=0,right=x,mid;
 9     while(left<=right){
10         mid=(left+right)/2;
11         if(q[mid]==y)
12         return 1;
13         if(q[mid]>y)
14         right=mid-1;
15         else
16         left=mid+1;
17     }
18     return 0;
19 }
20
21 int main(){
22     int l,n,m,s,ans=0;
23     while(cin>>l>>n>>m){
24     ans++;
25     for(int i=0;i<l;i++){
26         cin>>a[i];
27     }
28     for(int i=0;i<n;i++){
29         cin>>b[i];
30     }
31     for(int i=0;i<m;i++){
32         cin>>c[i];
33     }
34     cin>>s;
35     int x,cnt=0;
36
37     for(int i=0;i<l;i++){
38         for(int j=0;j<n;j++){
39             num[cnt++]=a[i]+b[j];
40         }
41     }
42     sort(num,num+cnt);
43     printf("Case %d:\n",ans);
44     while(s--){
45         cin>>x;
46         int flag=0;
47         for(int i=0;i<m;i++){
48             int temp=x-c[i];
49             if(bin(num,cnt-1,temp)){
50                 cout<<"YES"<<endl;
51                 flag=1;
52                 break;
53             }
54         }
55         if(!flag)
56         cout<<"NO"<<endl;
57     }
58 }
59     return 0;
60 } 
时间: 2024-12-05 22:23:13

hdu-2141的相关文章

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

hdu 2141 Can you find it?

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2141 Can you find it? 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 formu

hdu 2141 (二分)

链接: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): 11503    Accepted Submission(s): 3021 Problem Description Give you three seque

HDU 2141(二分&amp;三分 _B题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 ----------------------------------------------------------------------------------- 题意:三个数组,找到每个数组中加和为M的值的组数. 思路:首先将后两个数组加和 O(N^2),排序 O(NlogN),然后利用二分求解. 代码: #include<cstdio> #include<cstring>

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 - 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 2141~ ??Can you find it? 还是二分法~~??????

Can you find it? Time Limit : 10000/3000ms (Java/Other)   Memory Limit : 32768/10000K (Java/Other) Total Submission(s) : 274   Accepted Submission(s) : 87 Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. N

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

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

Can you find it? Time Limit:3000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u 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

hdu 2141 二分

题意是给你三个数组   分别有n m k个数     从三个数组里分别拿一个数能不能等于s 刚开始没注意数可以为负数    wa了好多次 别想直接暴力  肯定超时 现将两个数组合并  暴力枚举数少的      二分枚举数多的(非常省时) #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int main() { int num1[510],num2[510],num3