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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141

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

题目大意:多组输入,l,n,m个数,从然后q次访问,找出分别从三个数组中挑出一个数满足a+b+c=x,能找到输出YES,否则输出NO

看一下范围,两个数组加一下,然后对于一个x二分一个数组a,找到答案key输出YES否则输出NO

是二分总结的情况:1.对于查找目标值的二分查找

ac:

//#pragma comment(linker, "/STACK:1024000000,1024000000") 
 
#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  
 
#define ll long long  
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);
const int MAXN=510;
const ll INF=1e16;
const ll mod=1e9+7;
const double PI=acos(-1.0);


ll A[MAXN],B[MAXN],C[MAXN],arr[MAXN*MAXN];

int main()
{
	std::ios::sync_with_stdio(false);
	int Case=1;
	int l,n,m;
	while(cin>>l>>n>>m)
	{
		for(int i=1;i<=l;++i)
			cin>>A[i];
		for(int i=1;i<=n;++i)
			cin>>B[i];
		for(int i=1;i<=m;++i)
			cin>>C[i];
		int k=0;
		for(int i=1;i<=n;++i)
		{
			for(int j=1;j<=m;++j)
				arr[k++]=B[i]+C[j];
		}
		sort(arr,arr+k);
		int s;
		cin>>s;
		cout<<"Case "<<Case++<<":"<<endl;
		//500*1000*log(500*500)
		//5e5*log(3e5)  =>  nlog(n) 
		ll x;
		while(s--)//1e3
		{
			int f=0;
			cin>>x;
			for(int i=1;i<=l;++i)//500
			{//找符合要求的mid==key 
				
				int l=0,r=k-1,mid;
				ll key=x-A[i],ans=INF;
				while(l<=r)//log(500*500)
				{
					mid=(l+r)>>1;
					if(arr[mid]==key)
					{
						ans=mid;
						break;
					}
					else if(arr[mid]>key)
						r=mid-1;
					else
						l=mid+1;
				}
				if(ans!=INF)
				{
					f=1;
					break;
				}
			}
			if(f)
				cout<<"YES"<<endl;
			else
				cout<<"NO"<<endl;
		}
	}
}

/*
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


*/

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/84716100
今日推荐