HDU-2141-Can you find it?(二分)
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=2141
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 Sample Output Case 1: |
題目大意:多組輸入,l,n,m個數,從然後q次訪問,找出分別從三個陣列中挑出一個數滿足a+b+c=x,能找到輸出YES,否則輸出NO
看一下範圍,兩個陣列加一下,然後對於一個x二分一個數組a,找到答案key輸出YES否則輸出NO
是二分總結的情況:1.對於查詢目標值的二分查詢
ac:
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<stdio.h> #include<string.h> #include<math.h> #include<map> //#include<set> #include<deque> #include<queue> #include<stack> #include<bitset> #include<string> #include<fstream> #include<iostream> #include<algorithm> using namespace std; #define ll long long //#define max(a,b) (a)>(b)?(a):(b) //#define min(a,b) (a)<(b)?(a):(b) #define clean(a,b) memset(a,b,sizeof(a))// 水印 //std::ios::sync_with_stdio(false); const int MAXN=510; const ll INF=1e16; const ll mod=1e9+7; const double PI=acos(-1.0); ll A[MAXN],B[MAXN],C[MAXN],arr[MAXN*MAXN]; int main() { std::ios::sync_with_stdio(false); int Case=1; int l,n,m; while(cin>>l>>n>>m) { for(int i=1;i<=l;++i) cin>>A[i]; for(int i=1;i<=n;++i) cin>>B[i]; for(int i=1;i<=m;++i) cin>>C[i]; int k=0; for(int i=1;i<=n;++i) { for(int j=1;j<=m;++j) arr[k++]=B[i]+C[j]; } sort(arr,arr+k); int s; cin>>s; cout<<"Case "<<Case++<<":"<<endl; //500*1000*log(500*500) //5e5*log(3e5) => nlog(n) ll x; while(s--)//1e3 { int f=0; cin>>x; for(int i=1;i<=l;++i)//500 {//找符合要求的mid==key int l=0,r=k-1,mid; ll key=x-A[i],ans=INF; while(l<=r)//log(500*500) { mid=(l+r)>>1; if(arr[mid]==key) { ans=mid; break; } else if(arr[mid]>key) r=mid-1; else l=mid+1; } if(ans!=INF) { f=1; break; } } if(f) cout<<"YES"<<endl; else cout<<"NO"<<endl; } } } /* 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 */