HDU 2141 (二分法)

来源:互联网 发布:js中使用contextpath 编辑:程序博客网 时间:2024/06/03 10:16

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 31 2 31 2 31 2 331410
Sample Output
Case 1:NOYESNO


先把a和b数组全部组合存起来,每次询问只需要对于c数组枚举,二分查找a和b数组的组合是否存在需要的数。


代码:


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <queue>#define mem(p,k) memset(p,k,sizeof(p));#define rep(i,j,k) for(int i=j; i<k; i++)#define pb push_back#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define inf 0x6fffffff#define ll long longusing namespace std;const int mod=1e9+7;struct SD{    int x,y;    bool operator <(const struct SD &a)const{       return x<a.x;    }}num[1100];int bk[1100];int n,m,k,maxx,ans,cur,sum,x,y;int minn;int nex[4][2]={0,1,1,0,0,-1,-1,0};ll a[510],b[510],c[510];ll mm[300000];int main(){    int l;    int cu=1;    while(cin>>l>>n>>m){            rep(i,0,l)scanf("%lld",a+i);            rep(i,0,n)scanf("%lld",b+i);            rep(i,0,m)scanf("%lld",c+i);            cin>>k;            int le=0;            for(int i=0;i<l;i++){                for(int j=0;j<n;j++){                    mm[le++]=a[i]+b[j];                }            }            sort(mm,mm+le);            cout<<"Case "<<cu++<<":"<<endl;            ll ans;            while(k--){                scanf("%lld",&ans);                cur=0;                for(int i=0;i<m;i++){                    int l=0,r=le-1;                    while(l<=r){                        int m=(l+r)>>1;                        if(mm[m]==ans-c[i]){cur=1;break;}                        if(mm[m]>ans-c[i])r=m-1;                        else l=m+1;                    }                    if(cur)break;                }                if(cur)cout<<"YES"<<endl;                else cout<<"NO"<<endl;            }    }    return 0;}//freopen("C:\\Users\\LENOVO\\Desktop\\read.txt","r",stdin);



0 0
原创粉丝点击