Can you find it? Time Limit: 10000/3000 MS (java/Others) Memory Limit: 32768/10000 K (Java/Others) 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 Author wangye Source HDU 2007-11 Programming Contest
/*二分答案.比较巧妙.先将两个数组合并搞成一个n^2大的数组.然后二分的话复杂度就有一个log.二分和合并后的数组即对n^2取log.然后复杂度就大大降低了.*/#include<iostream>#include<algorithm>#include<cstdio>#define MAXN 501#define LL long longusing namespace std;LL s[MAXN*MAXN],a[MAXN],b[MAXN],c[MAXN],n1,n2,n3,n,m;LL read(){ LL x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar(); return x*f;}bool erfen(int l,int r,int i,int x){ int mid; while(l<=r) { mid=(l+r)>>1; if(s[mid]+c[i]==x) return true; if(s[mid]+c[i]>x) r=mid-1; else l=mid+1; } return false;}void slove(){ int x; bool flag; while(m--) { flag=false;x=read(); for(int i=1;i<=n3;i++) if(erfen(1,n,i,x)){flag=true;printf("YES/n");break;} if(!flag) printf("NO/n"); } return ;}int main(){ int t=0; while(~scanf("%d%d%d",&n1,&n2,&n3)) { printf("Case %d:/n",++t);n=0; for(int i=1;i<=n1;i++) a[i]=read(); for(int i=1;i<=n2;i++) b[i]=read(); for(int i=1;i<=n3;i++) c[i]=read(); for(int i=1;i<=n1;i++) for(int j=1;j<=n2;j++) s[++n]=a[i]+b[j]; sort(s+1,s+n+1); m=read();slove(); } return 0;}新闻热点
疑难解答