Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others) Total Submission(s): 45356 Accepted Submission(s): 10985 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
Recommend 威士忌
|
题目大意:现在给你L,N,M,也就是三个数组的大小,下面就问你每个数组内取一个数,问你能不能:Ai+Bi+Ci=x,
下面给出的数据就是X,能组成就输出YES,不然就是NO
思路:这个题目的话,最简单的想法当然是直接三重循环的,但是如果这样去找的话,无疑超时的(没尝试。。)
我们可以先加起来前面的两个,然后对加起来的两个弄一个二分查找,这样会使时间优化不少的。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
using namespace std;
const int maxn=510;
int a[maxn],b[maxn],c[maxn];
int d[maxn*maxn];
int main()
{
int kase=1,L,N,M;
while(scanf("%d%d%d",&L,&N,&M)!=EOF)
{
for(int i=1;i<=L;i++)
scanf("%d",&a[i]);
for(int i=1;i<=N;i++)
scanf("%d",&b[i]);
for(int i=1;i<=M;i++)
scanf("%d",&c[i]);
int cnt=0;
for(int i=1;i<=L;i++)
for(int j=1;j<=N;j++)
d[cnt++]=a[i]+b[j];
sort(d,d+cnt);
int q;
scanf("%d",&q);
printf("Case %d:\n",kase++);
while(q--)
{
bool flag=false;
int x;scanf("%d",&x);
for(int i=1;i<=M;i++)
{
int l=0,r=cnt-1;
while(l<=r)
{
int mid=(l+r)/2;
if(d[mid]+c[i]==x)
{
flag=true;break;
}
else if(d[mid]+c[i]<x)
{
l=mid+1;
}
else
r=mid-1;
}
if(flag) break;
}
if(flag) printf("YES\n");
else
printf("NO\n");
}
}
}