Can you find it?
Time Limit:3000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
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
Sample Input
Sample Output
#include<cstdio> #include<cctype> #include<iostream> #include<stack> #include<map> #include<cstring> #include<string> #include<sstream> #include<queue> #include<set> using namespace std ; int a[505],b[505],c[505]; set<int>sum; int main() { int l,n,m; int cou=1; while(~scanf("%d%d%d",&l,&n,&m)){ sum.clear(); for(int i=0;i<l;i++) scanf("%d",&a[i]); for(int i=0;i<n;i++) scanf("%d",&b[i]); for(int j=0;j<m;j++) scanf("%d",&c[j]); for(int i=0;i<n;i++) for(int j=0;j<m;j++){ sum.insert(b[i]+c[j]); } int t; scanf("%d",&t); printf("Case %d:\n",cou++); while(t--){ int tp; scanf("%d",&tp); int flag=0; set<int>::iterator f; for(int i=0;i<l;i++){ f=sum.find(tp-a[i]);//利用find函數來搜索值,其實set集合類的find函數內部就是用二分來實現的,所以想要快速AC的同學,可以使用這種方法. if(f!=sum.end()){ flag=1; break; } } if(!flag) printf("NO\n"); else printf("YES\n"); } } return 0 ; }
#include<cstdio> #include<cctype> #include<iostream> #include<stack> #include<map> #include<cstring> #include<string> #include<sstream> #include<queue> #include<set> #include<algorithm> using namespace std ; int a[505],b[505],c[505]; int sum[250005]; int k=0,flag=0; void bs(int tp,int i) { int l=0,r=k-1; while(l<=r) { int m=(r+l)>>1; if(sum[m]+a[i]==tp) { //printf("%d\n",m); flag=1; break; } else if(sum[m]+a[i]>tp) r=m-1; else l=m+1; } } int main() { int l,n,m; int cou=1; while(~scanf("%d%d%d",&l,&n,&m)) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); memset(sum,0,sizeof(sum)); for(int i=0; i<l; i++) scanf("%d",&a[i]); for(int i=0; i<n; i++) scanf("%d",&b[i]); for(int j=0; j<m; j++) scanf("%d",&c[j]); k=0; for(int i=0; i<n; i++) for(int j=0; j<m; j++) { sum[k++]=b[i]+c[j]; } sort(sum,sum+k); int t; scanf("%d",&t); printf("Case %d:\n",cou++); while(t--) { int tp; scanf("%d",&tp); flag=0; for(int i=0; i<l; i++) { bs(tp,i); } if(!flag) printf("NO\n"); else printf("YES\n"); } } return 0 ; }
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。