Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 24455 Accepted Submission(s): 6191
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
water problem 感觉奇奇怪怪的 G++ MLE C++跑了1400+ms 在web-diy里 GUN C++ 0MS…
把Ai+Bj+Ck全放set里 O(n*m*l)+O(s*log(n*m*l))的复杂度 不过显然会爆内存
那就把Ai+Bj放set里 时间换点空间
每次遍历C 如果set中存在x-C[k] Yes 否则 No
O(s*m*log(l*n)+O(l*n)的复杂度
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<cmath>
#include<list>
#include<cstring>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
#define sci(a) scanf("%d",&a)
#define scll(a) scanf("%lld",&a)
#define scd(a) scanf("%lf",&a)
#define scs(a) scanf("%s",a)
#define pri(a) printf("%d\n",a);
#define prd4(a) printf("%.4lf\n",a);
#define prd2(a) printf("%.2lf\n",a);
#define prs(a) printf("%s\n",a);
#define N 505
int elem[3][N];
set<ll>exist;
int main(){
//freopen("/home/lu/文档/r.txt","r",stdin);
//freopen("/home/lu/文档/w.txt","w",stdout);
int l,n,m,s,t=1;
ll x;
while(~scanf("%d%d%d",&l,&n,&m)){
exist.clear();
for(int i=0;i<l;++i)
sci(elem[0][i]);
for(int i=0;i<n;++i)
sci(elem[1][i]);
for(int i=0;i<m;++i)
sci(elem[2][i]);
for(int i=0;i<l;++i)
for(int j=0;j<n;++j)
exist.insert((ll)elem[0][i]+elem[1][j]);
sci(s);
printf("Case %d:\n",t++);
for(int i=0;i<s;++i){
scll(x);
bool ex=false;
for(int i=0;i<m&&(!ex);++i)
ex=exist.find(x-elem[2][i])!=exist.cend();
prs(ex?"YES":"NO");
}
}
return 0;
}