您的位置:首页 > 其它

HDU--杭电--2141--Can you find it?--二分

2013-11-25 21:57 381 查看

Can you find it?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)

Total Submission(s): 8570    Accepted Submission(s): 2240

[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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".

 

[align=left]Sample Input[/align]

3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10

 

[align=left]Sample Output[/align]

Case 1:
NO
YES
NO

题意就是给你三个数列,分别是a、b、c的,从中选取组合看能不能搞成a+b+c=X,能就yes不能就no


思路:三个都是最大可以能为500的数组,所以直接暴力会超时,这时就二分查找,还要加上一个小技巧,就是合并其中任意两个数组,也就是把所有a+b的和装起来,这样就成了ab+c的运算了


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
__int64 a[555],ab[255555],c[555];
bool double_kill(__int64 l,__int64 r,__int64 s)	//二分查找
{
__int64 mid;
while(l<=r)
{
mid=(l+r)/2;
if(ab[mid]==s)return 1;
if(ab[mid]<s)l=mid+1;
else r=mid-1;
}
return 0;
}
int main (void)
{
__int64 i,j,k,l,n,m,cas=1,q,w,e;
while(scanf("%I64d%I64d%I64d",&q,&w,&e)!=EOF)
{
for(i=0;i<q;i++)
scanf("%I64d",&a[i]);
for(i=l=0;i<w;i++)
{
scanf("%I64d",&k);
for(j=0;j<q;j++)ab[l++]=a[j]+k;	//把a+b存在ab数组里
}
for(i=0;i<e;i++)
scanf("%I64d",&c[i]);
sort(ab,ab+l);	//排序,二分的需求
scanf("%I64d",&n);
printf("Case %I64d:\n",cas++);
while(n--&&scanf("%I64d",&m))
{
for(i=0;i<e;i++)
if(m>=ab[0]+c[i]&&m<=ab[l-1]+c[i]&&double_kill(0,l-1,m-c[i]))
break;
if(i==e)puts("NO");
else puts("YES");
}
}
return 0;
}


  这里用__int64是因为最大是32位,而且还要进行运算, 32位+32位 在32位里面放不下,所以用64位了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: