Recently, I felt that the dual division was under a huge pressure, so I made a dual division problem with my sister. It was a good question, but no one could tell us that we were too weak. Continue to cheer up, and I want to become a big cow, do not be a cainiao.
Description
Give you three sequences of numbers A, B, C, then we give you a number X. now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai + Bj + Ck = X.
Input
There are too cases. every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. in the same th line there is an integer S represents there are S integers X to be calculated. 1 <= L, N, M <= 500, 1 <= S <= 1000. all the integers are 32-integers.
Output
For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. if satisfied, you print "YES", otherwise print "NO ".
Sample Input
3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10
When we look at the data so big, we think of binary. First, we need to convert the two into one. The second time complexity is nlog (n * n), so that it will not time out. We don't need to talk about the binary algorithm. Everyone on Earth knows it.
Code:
[Cpp]
<Span style = "font-family: FangSong_GB2312; font-size: 18px;" >#include <iostream>
# Include <cmath>
# Include <algorithm>
Using namespace std;
Int a [505], B [505], c [505];
Int dp [250025];
Int main ()
{
Int I, j, k, m, n, s, t, cas = 0, p, flag;
While (scanf ("% d", & k, & m, & n )! = EOF)
{
For (I = 0; I <k; I ++)
Scanf ("% d", & a [I]);
For (I = 0; I <m; I ++)
Scanf ("% d", & B [I]);
For (I = 0; I <n; I ++)
Scanf ("% d", & c [I]);
P = 0;
For (I = 0; I <k; I ++)
For (j = 0; j <m; j ++)
Dp [p ++] = a [I] + B [j];
Sort (dp, dp + p );
Printf ("Case % d: \ n", ++ cas );
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & s); flag = 0;
For (I = 0; I <n; I ++)
{
Int l = 0, r = p-1, mid;
While (l <= r)
{
Mid = (l + r)/2;
If (dp [mid] + c [I] = s) {flag = 1; break ;}
Else if (dp [mid] + c [I] <s) l = mid + 1;
Else if (dp [mid] + c [I]> s) r = mid-1;
}
If (flag) break;
}
If (flag) printf ("YES \ n ");
Else printf ("NO \ n ");
}
}
Return 0;
}
</Span>