杭电 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): 17174    Accepted Submission(s): 4362


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三个数列,再给出S个X,问在数列中是否有元素满足Ai+Bj+Ck=X。

程式需优化,即A+B=X-C。

另外:输出一定要注意都是大写啊啊啊啊啊。。。WA了一下午快哭了。。。


#include<stdio.h>
#include<algorithm>
#define max 500
using namespace std;

int a[max+10],b[max+10],c[max+10],ab[max*max+10];

int main(){
	int l,m,n,t=0,s,x;
	while(~scanf("%d%d%d",&l,&n,&m)){
		int i,j,k=0,flag=0;
		for(i=0;i<l;i++) scanf("%d",&a[i]);
		for(i=0;i<n;i++) scanf("%d",&b[i]);
		for(i=0;i<m;i++) scanf("%d",&c[i]);
		for(i=0;i<l;i++){
			for(j=0;j<n;j++){
				ab[k++]=a[i]+b[j];
			}
		}
		sort(ab,ab+k);
		sort(c,c+m);
		scanf("%d",&s);
		printf("Case %d:\n",++t);
		while(s--){
			int flag=0;
			scanf("%d",&x);
			int p,q;
			for(i=0;i<m;i++){
			p=x-c[i];
			q=lower_bound(ab,ab+k,p)-ab;
			if(p==ab[q]) flag=1;
			}
			if(flag) printf("YES\n");
		    else printf("NO\n");
		    }
		}
	return 0;
} 



你可能感兴趣的:(杭电 2141 Can you find it?二分法+暴力查找)