Can you find it?(二分)

来源:互联网 发布:投资软件 编辑:程序博客网 时间:2024/06/05 17:41
http://acm.hdu.edu.cn/showproblem.php?pid=2141

Can you find it?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 13563    Accepted Submission(s): 3483


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 31 2 31 2 31 2 331410
 

Sample Output
Case 1:NOYESNO
 

Author
wangye
 

Source
HDU 2007-11 Programming Contest
 

Recommend
威士忌
 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

ll a[510],b[510],c[510],ab[502*502];
int num;
int Find(ll x)
{
int low=0,high=num;
while(low<=high)
{
int mid=(low+high)/2;
if(ab[mid]==x) return 1;
else if(ab[mid]<x)low=mid+1;
else
{
high=mid-1;
}
}
return 0;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m,q,cas=0;
while(cin>>n>>m>>q)
{
cas++;
printf("Case %d:\n",cas);
for(int i=0;i<n;i++)
scanf("%I64d",&a[i]);
for(int j=0;j<m;j++)
scanf("%I64d",&b[j]);
for(int k=0;k<q;k++)
scanf("%I64d",&c[k]);
num=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
ab[num++]=a[i]+b[j];
sort(ab,ab+num);
sort(c,c+q);
int d;
ll t;
num--;
scanf("%d",&d);
while(d--)
{
scanf("%I64d",&t);
if(ab[0]+c[0]>t||ab[num]+c[q-1]<t)
printf("NO\n");
else
{
int p;
for(p=0;p<q;p++)
if(Find(t-c[p]))
{printf("YES\n");break;}
if(p==q)
printf("NO\n");
}
}
}
return 0;
}


没想到。。。合并两个数组在进行二分查找。
0 0