Time
limit:3000MS
Memory Limit:10000KB
64bit IO Format:%i64d &%i64 U
Description
Give 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 is many cases. Every data case was described as followed:in the first line there be three integers L, N, M, in the second line there was L integers represent the sequence A, in the third line there is N integers represent the sequences B, in the Forth line There is M integers represent the sequence C. The fifth line there was an integer s represents there was s integers X to be calculated. 1<=l, N, m<=500, 1<=s<=1000. All the integers is 32-integers.
Output
For each case, firstly you has to print the case number as the form ' case D: ', then for the S queries, and you calculate if T He formula can is satisfied or not. If Satisfied, you print "YES", otherwise print "NO".
Sample Input
3 3 31 2 31 2 31 2 331410
Sample Output
Case 1:noyesno Problem Solving ideas: The main idea: Give 3 string integer a,b,c and integer x, if there is a set of number Ai,bj,ck, so that ai+bj+ck=x set up, then output "YES", otherwise output "NO". The data type can be used with int. The first thing you see is a brute force hack, 3 for loop, and a time complexity of O (n3)。 This method is not advisable when n reaches a certain size. It is also easy to think of binary retrieval, which is more efficient. Idea: Summing the latter two sequences, and sorting the results to heavy. The time complexity is O (N2). For the first sequence enumeration, binary retrieval and sequence, the time is NLGN. Therefore the total time complexity is O (N2, the scheme is feasible.) Harvest impression: As a result of yesterday's class study to the application of binary search, but also familiar with, where the array to waste a little time, because the save two arrays and when a new array is created, if you create an array to save the result after the weight is not necessary, the direct use of the original array, due to order, As long as the comparison before and after two numbers, starting from the first number, the counter first occurrence of the position, and then traverse backward, until the new number appears, move it forward, overwriting the duplicate value, the Counter plus 1.
#include <iostream>#include<algorithm>using namespacestd;intMain () {inta[501],b[501],c[501],temp[250001]; intL,n,m,s,i;intD=1; while(cin>>l>>n>>M) {if(l<1|| L> -|| n<1|| N> -|| m<1|| M> -)return-1; //input for(i=0; i<l;i++) cin>>A[i]; for(i=0; i<n;i++) cin>>B[i]; for(i=0; i<m;i++) cin>>B[i]; CIN>>S; if(s<1|| S> +)return-1; //after two sets of and save to temp inttp=0; for(i=0; i<n;i++) for(intt=0; t<m;t++) {TEMP[TP]=b[i]+C[t]; TP++; } sort (Temp,temp+m*N); //array de-weight inttp1=0; for(i=1; i<m*n;i++) { if(temp[i]==temp[i-1])Continue; Else{tp1++;temp[tp1]=temp[i];} } cout<<" Case"<<d<<":"<<Endl; while(s--){ intflag=1; intX; CIN>>X; for(i=0; i<l;i++){ intlo=0, hi=TP1; intmi; while(lo<=hi) {mi= ((Hi-lo) >>1) +lo;//when the lo and hi are larger, the sum may explode int if((A[i]+temp[mi]) ==x) {flag=0; Break;} Else if(a[i]+temp[mi]<x) lo=mi+1; Elsehi=mi-1; } } if(!flag) cout<<"YES"<<Endl; Elsecout<<"NO"<<Endl; } d++;} return 0;}
CSU-ACM2016 Summer Camp Training 1-two points search A-can you find it?