您的位置:首页 > 其它

HDU - 2141 Can you find it? 二分

2018-02-11 22:07 435 查看

Can you find it?

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


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 3
1 2 3
1 2 3
1 2 3
3
1
4
10 
Sample Output
Case 1:
NO
YES
NO 
Author wangye 
Source HDU 2007-11 Programming Contest

题目大意:先输入三列数字,分别标记为Ai,Bi,Ci,判断是否存在Ai,Bj,Ck使得其相加为后面所输入的数。思路:先将前两列的数相加,用一个数组储存,然后使用sort将其排序,最后二分查找。
#include<stdio.h>
#include<algorithm>
using namespace std;
int L_[505], N_[505], M_[505];
int sum_[250005];
int main()
{
int L, N, M;
int S;
int x,num=0,temp;
while (~scanf("%d %d %d", &L, &N, &M))
{
num++;
int k = 0;
for(int i = 0; i < L; i++)
{
scanf("%d", &L_[i]);
}
for (int i = 0; i < N; i++)
{
scanf("%d", &N_[i]);
}
for (int i = 0; i < M; i++)
{
scanf("%d", &M_[i]);
}
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
sum_[k++] = N_[i] + M_[j];
sort(sum_, sum_ + k);
scanf("%d", &S);
printf("Case %d:\n", num);
while (S--)
{
int flag = 0;
scanf("%d", &x);
for (int i = 0; i < L; i++)
{
temp = x - L_[i];
int left = 0,right=k-1;
int mid;
while (left <= right)
{
mid = (left + right) / 2;
if (sum_[mid] > temp)
{
right=mid-1;
}
else if (sum_[mid]<temp)
{
left=mid+1;
}
else
{
flag = 1;
break;
}
}
if (flag==1)
{
printf("YES\n");
break;
}
}
if (flag == 0)
{
printf("NO\n");
}
}
}
return 0;
}

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