hdu 2141 can you find it?

一句话,将a+b排序后,再用二分查找是否有x-c。注意二分的写法,left<right还是<=,right=mid+1还是mid。


//freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin);
//freopen("input.txt",  "r", stdin);  //读数据
//	freopen("output.txt", "w", stdout); //注释掉此句则输出到控制台
#include<stdio.h>
#include<stdlib.h>

#define NUM 505
int a[NUM],b[NUM],c[NUM];
int ab[NUM*NUM],l,n;
int cmp(const void*a, const void *b)
{
	return *(int *)a - *(int *)b;
}
int find(int x,int num)
{
	int left=0,right=num-1,mid=0;//因为之前zz多加了一次
	while(right>left){
		mid=(left+right)/2;
		if(ab[mid]==x)
			return 1;
		if(ab[mid]<x)
			left=mid+1;
		else
			right=mid;
	}


	return 0;
}
int main()
{
	int m,i,j,k,cases=1;
	int x,num,flag,zz;
	freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin);
	while(scanf("%d%d%d",&l,&n,&m)!=EOF){
		for(i=0;i<l;i++)
			scanf("%d",&a[i]);
		for(j=0;j<n;j++)
			scanf("%d",&b[j]);
		for(k=0;k<m;k++)
			scanf("%d",&c[k]);
		zz=0;
		for(i=0;i<l;i++)
		{
			for(j=0;j<n;j++)
				ab[zz++]=a[i]+b[j];//不能用ab[i+j];
		}
		qsort(ab,zz,sizeof(int),cmp);
		scanf("%d",&num);
		printf("Case %d:\n",cases++);
		while(num--){
			flag=0;
			scanf("%d",&x);
			for(k=0;k<m;k++){
				if(find(x-c[k],zz)==1){
					flag=1;
					break;
				}

			}
			if(flag==1) printf("YES\n");
			else printf("NO\n");
		}
	}
	return 0;
}


你可能感兴趣的:(hdu 2141 can you find it?)