[恢]hdu 2141
2011-12-30 19:52:14
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2141
题意:给一串ai、bj、ck,一串x,问x是否能表示成ai+bj+ck。
mark:不会,搜了一下。把ai+bj存起来。然后二分x-ck。。。复杂度应该是O(500*500 + lg(250000)*500)好极限。
代码:
# include <stdio.h>
# include <stdlib.h>
int a[510], b[510], c[510] ;
int ab[510*510] ;
int cmp(const void *a, const void *b)
{
return *(int*)a - *(int*)b ;
}
int main ()
{
int l, n, m, s ;
int i, j, x, key ;
int cnt, nCase = 1 ;
void *p ;
while (~scanf ("%d%d%d", &l, &n, &m))
{
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]) ;
cnt = 0 ;
for (i = 0 ;i < l ; i++)
for (j = 0 ; j < n ; j++)
ab[cnt++] = a[i]+b[j] ;
qsort (ab, cnt, 4, cmp) ;
printf ("Case %d:\n", nCase++) ;
scanf ("%d", &s) ;
while (s--)
{
scanf ("%d", &x) ;
p = NULL ;
for (i = 0 ; i < m ; i++)
{
key = x - c[i] ;
p = bsearch (&key, ab, cnt, 4, cmp) ;
if (p != NULL) break ;
}
puts (p == NULL ? "NO" : "YES") ;
}
}
return 0 ;
}