http://acm.hdu.edu.cn/showproblem.php?pid=2141
#include<iostream> #include<algorithm> using namespace std; __int64 a[505],b[505],c[505]; __int64 temp[250005];
bool bsearch(__int64 v,int low,int high) //二分查找 { int mid; while(low <= high) { mid = (high + low)/2;
if(temp[mid] == v) return true; if( temp[mid] < v) low = mid+1; else high = mid-1; } return false; }
int partition(__int64 r[],int i,int j) { __int64 pivot = r[i]; while(i<j) { while(i<j && r[j] >= pivot) j--; if(i<j) r[i++] = r[j]; while(i<j && r[i] <= pivot) i++; if(i<j) r[j--] = r[i]; } r[i] = pivot; return i; }
void quickSort(__int64 r[],int low,int high) //快排 { int pivotpos; if(low < high) { pivotpos = partition(r,low,high); quickSort(r , low , pivotpos-1); quickSort(r , pivotpos+1 , high); } }
int main() { __int64 lena,lenb,lenc,cas=1;
while(scanf("%I64d%I64d%I64d",&lena,&lenb,&lenc)!=EOF) { __int64 i,j,n;
for(i=0 ; i < lena ; i++)
scanf("%I64d",&a[i]);
for(i=0 ; i < lenb ; i++)
scanf("%I64d",&b[i]);
for(i=0 ; i < lenc ; i++)
scanf("%I64d",&c[i]);
__int64 k=0;
for(i=0 ; i< lena; i++)
for(j=0 ; j< lenb; j++)
temp[k++] = a[i] + b[j];
//sort(temp+0,temp+k); //,comp quickSort(temp,0,k-1); printf("Case %I64d:\n",cas++); scanf("%I64d",&n); while(n--) { int ok = 0; __int64 sum; scanf("%I64d",&sum);
for(i=0; ok==0 && i<lenc ; i++) { if( bsearch( sum - c[i] ,0,k-1) )//if(binary_search(temp+0,temp+k,sum-c[i])) ok = 1; } if(ok == 1) printf("YES\n"); else printf("NO\n"); } } return 0; }
|