Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 23851 Accepted Submission(s): 6046
Problem 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 many 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 A, 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 fifth 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
Sample Output
Case 1: NO YES NO
就是给三组序列,L个A,N个B,M个C,下面给出一系列数X,问能不能从三组序列各取一个加在一起等于这个数,这个主要是超时,三重循环,每重500次,每个数要循环10的8次方,肯定会爆的(我试了一下,机房电脑嗡嗡响,吓得我赶紧关了),先把任意两个序列两两相加得出一个新的序列S,大概500*500大小,减少了500倍,虽然还是很大,但还算可以接受。然后sort一下,用给出让判断的数X循环减去剩下那个序列E中的元素,再用二分搜索这个差是否存在S序列中
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[555], b[555], c[555], q[250010];
int main(){
int L, M, N, S, x, t = 1, v, u;
while(~scanf("%d%d%d", &L, &N, &M)){
for(int i = 0; i < L; i++){
scanf("%d", &a[i]);
}
v = 0;
for(int i = 0; i < N; i++){
scanf("%d", &b[i]);
for(int j = 0; j < L; j++){
q[v++] = a[j] + b[i];
}
}
for(int i = 0; i < M; i++){
scanf("%d", &c[i]);
}
sort(q, q + v);
scanf("%d", &S);
printf("Case %d:\n", t++);
while(S--){
scanf("%d", &x);
int cas = 0;
for(int i = 0; i < M; i++){
int ans = x - c[i];
int e, s, m, ok = 0;
e = 0;
s = v-1;
while(e <= s){
m = (e + s) >> 1;
if(q[m] == ans){
ok = 1;
break;
}
if(q[m] > ans){
s = m-1;
}
else{
e = m+1;
}
}
if(ok){
printf("YES\n");
cas++;
break;
}
}
if(!cas){
printf("NO\n");
}
}
}
return 0;
}