您的位置:首页 > 其它

Can you find it? (二分)

2017-03-03 17:16 141 查看
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. 

InputThere 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. 

OutputFor 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
1 2 3
1 2 3
1 2 3
3
1
4
10


Sample Output
Case 1:
NO
YES
NO

n三次方的复杂度会超时。
三个数组,把第一个和第二个数组组合起来,构成sum数组保存所有和的可能(有重复无所谓),然后再在sum数组中
二分根据 sum[i]+c[j]=X,在sum数组二分X-c[j]的值,一旦某一个j找到一个对应的sum[i],就YES。时间复杂度下降
为n^2+nlogn

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
#define INF 100000000

using namespace std;
int L,N,M,S;
int a[505];
int b[505];
int c[505];
int sum[250005];
int k;
int x;
int tmp;

int main()
{
int kase=1;
while(scanf("%d%d%d",&L,&N,&M)==3)
{
for(int i=0;i<L;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<N;i++)
{
scanf("%d",&b[i]);
}
for(int i=0;i<M;i++)
{
scanf("%d",&c[i]);
}
k=0;
for(int i=0;i<L;i++)
{
for(int j=0;j<N;j++)
{
sum[k++]=a[i]+b[j];
}
}
sort(sum,sum+k);
scanf("%d",&S);
printf("Case %d:\n",kase++);
while(S--)
{
scanf("%d",&x);
int flag=0;
for(int j=0;j<M;j++)
{
tmp=x-c[j];
int left=0,right=k-1;
int mid;
while(left<=right)
{
mid=(left+right)/2;
if(sum[mid]>tmp)
{
right=mid-1;
}
else if(sum[mid]<tmp)
{
left=mid+1;
}
else
{
flag=1;
break;
}
}
if(sum[left]==tmp&&left<k || sum[right]==tmp&&right>=0)
{
flag=1;
}
if(flag==1)
{
printf("YES\n");
break;
}
}
if(flag==0)
{
printf("NO\n");
}
}
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: