您的位置:首页 > 其它

HDOJ 2141 Can you find it?

2016-07-27 21:32 302 查看

Can you find it?

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

Total Submission(s): 23857 Accepted Submission(s): 6049

[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


三个for肯定是要超时的,(别问我为什么,因为题出在二分专题)。所以可以先让第一第二组结合在对第三组二分。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
int a[505],b[505],c[505];
int sum[505*505];
int l,n,m,k;
int flag;
void binary(int x)
{
int left,right,mid;
left=0,right=k-1;
while(left<=right)
{
mid=(left+right)/2;
if(sum[mid]>x)
right=mid-1;
else if(sum[mid]<x)
left=mid+1;
else
{flag=1;return ;}//retrn ;用来跳出子函数,在这里不能少。
}
return ;
}

int main()
{
int i,j,q,x,cnt=1;
while(scanf("%d%d%d",&l,&m,&n)!=EOF)
{
for(i=0;i<l;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<m;i++)
scanf("%d",&c[i]);
k=0;
for(i=0;i<l;i++)
for(j=0;j<n;j++)
{
sum[k++]=a[i]+b[j];
}
sort(sum,sum+k);
scanf("%d",&q);
printf("Case %d:\n",cnt++);
while(q--)
{
scanf("%d",&x);
flag=0;
for(i=0;i<m;i++)
{
binary(x-c[i]);
if(flag){
printf("YES\n");
break;
}
}
if(!flag)
printf("NO\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: