题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int a[505], b[505], c[505], s[1005], ans[250005] ;
int main(){
int l, n, m, x, mid, low, high, i, j, t=1 ;
bool flag ;
while(scanf("%d%d%d", &l, &n, &m)!=EOF){
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]) ;
scanf("%d", &x) ;
for(i=0; i<x; i++)
scanf("%d", &s[i]) ;
for(i=0; i<l; i++)
for(j=0; j<n; j++)
ans[n*i+j] = a[i] + b[j] ;
sort(ans, ans+n*l) ;
printf("Case %ld:\n",t) ;
t ++ ;
for(i=0; i<x; i++){
flag = false ;
for(j=0; j<m; j++){
low = 0 ;
high = l * n - 1 ;
while(low<=high){
mid = (low+high) / 2 ;
if(c[j]+ans[mid]==s[i]){
flag = true ;
break ;
}
else if(c[j]+ans[mid]>s[i]) high = mid - 1 ;
else low = mid + 1 ;
}
if(flag) break ;
}
if(flag)
printf("YES\n") ;
else
printf("NO\n") ;
}
}
return 0 ;
}
#include<algorithm>
#include<cstdio>
using namespace std;
int a[505], b[505], c[505], s[1005], ans[250005] ;
int main(){
int l, n, m, x, mid, low, high, i, j, t=1 ;
bool flag ;
while(scanf("%d%d%d", &l, &n, &m)!=EOF){
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]) ;
scanf("%d", &x) ;
for(i=0; i<x; i++)
scanf("%d", &s[i]) ;
for(i=0; i<l; i++)
for(j=0; j<n; j++)
ans[n*i+j] = a[i] + b[j] ;
sort(ans, ans+n*l) ;
printf("Case %ld:\n",t) ;
t ++ ;
for(i=0; i<x; i++){
flag = false ;
for(j=0; j<m; j++){
low = 0 ;
high = l * n - 1 ;
while(low<=high){
mid = (low+high) / 2 ;
if(c[j]+ans[mid]==s[i]){
flag = true ;
break ;
}
else if(c[j]+ans[mid]>s[i]) high = mid - 1 ;
else low = mid + 1 ;
}
if(flag) break ;
}
if(flag)
printf("YES\n") ;
else
printf("NO\n") ;
}
}
return 0 ;
}