http://acm.hdu.edu.cn/showproblem.php?pid=2141
先把A和B合并为1个序列,再用二分查找
我的代码
1 #include <cstdio>
2 #include <algorithm>
3 using namespace std;
4 const int N=510;
5 int a[N],b[N],c[N],d[N*N];
6 int find(int x,int *a,int l,int r)
7 {
8 int m;
9 while (l<r)
10 {
11 m=(l+r)/2;
12 if (x<=a[m]) r=m;
13 else l=m+1;
14 }
15 return x==a[l];
16 }
17 int main()
18 {
19 int l,m,n,o,i,j,t,x,C=0;
20 while (scanf("%d%d%d",&l,&m,&n)!=EOF)
21 {
22 printf("Case %d:\n",++C);
23 for (i=1;i<=l;i++) scanf("%d",&a[i]);
24 for (i=1;i<=m;i++) scanf("%d",&b[i]);
25 for (i=1;i<=n;i++) scanf("%d",&c[i]);
26 for (j=1;j<=m;j++)
27 for (i=1;i<=l;i++) d[(j-1)*l+i]=a[i]+b[j];
28 o=l*m;
29 sort(d+1,d+o+1);
30 sort(c+1,c+n+1);
31 scanf("%d",&t);
32 while (t--)
33 {
34 scanf("%d",&x);
35 int flag=0;
36 for (i=1;i<=n;i++)
37 if (flag=find(x-c[i],d,1,o)) break;
38 if (flag) printf("YES\n");
39 else printf("NO\n");
40 }
41 }
42 }