Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 23679 Accepted Submission(s): 5982
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直接三个循环必定会超时,所以用二分算法即可,a+b各种情况算出来,然后x-c二分查找就行了。看了半天,原来j写成i了。代码如下:#include <cstdio> #include <cstring> #include <math.h> #include <algorithm> #include <iostream> using namespace std; int l,m,n,t,flag,sum[510*510],a[510],b[510],c[510]; void cmp(int x) { int mid,z=0,y=t-1; while(z<=y) { mid=(z+y)>>1; if(x>sum[mid]) z=mid+1; else if(x<sum[mid]) y=mid-1; else {flag=1;return ;} } return ; } int main() { int i,j,cnt=1,x,s; while(scanf("%d%d%d",&l,&m,&n)!=EOF) { for(i=0;i<l;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]); t=0; for(i=0;i<l;i++) for(j=0;j<m;j++) sum[t++]=a[i]+b[j]; sort(sum,sum+t); scanf("%d",&s); printf("Case %d:\n",cnt++); while(s--) { scanf("%d",&x); flag=0; for(i=0;i<n;i++) { cmp(x-c[i]); if(flag) { printf("YES\n"); break; } } if(!flag) printf("NO\n"); } } return 0; }