HDU2141:Can you find it? (二分)

来源:互联网 发布:puppy linux吧 编辑:程序博客网 时间:2024/06/06 06:59

题解:

给定A,B,C三个集合和X,找是否有A+B+C=X,把A+B合并到一个集合中,然后查找是否有X-C即可

代码

#include <cstdio>#include <queue>#include <cstring>#include <iostream>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <string>#include <set>#include <ctime>#include <cmath>#include <cctype>using namespace std;#define maxn 100000#define LL long longint cas=1,T;LL sum[501*501];int main(){    int an,bn,cn;    while (scanf("%d%d%d",&an,&bn,&cn)!=EOF)    {        LL a[501],b[501],c[501];        for (int i = 0;i<an;i++)            scanf("%lld",&a[i]);        for (int i = 0;i<bn;i++)            scanf("%lld",&b[i]);        for (int i = 0;i<cn;i++)            scanf("%lld",&c[i]);        int k = 0;        for (int i = 0;i<an;i++)            for (int j = 0;j<bn;j++)            {                sum[k] = a[i]+b[j];                k++;            }        sort(sum,sum+k);        int xn;        printf("Case %d:\n",cas++);        scanf("%d",&xn);        for (int i = 0;i<xn;i++)        {            LL temp;            scanf("%lld",&temp);            int flag = 0;            for (int j = 0;j<cn;j++)            {                flag = upper_bound(sum,sum+k,temp-c[j])-lower_bound(sum,sum+k,temp-c[j]);                if (flag)                    break;            }            if (!flag)              printf("NO\n");            else              printf("YES\n");        }    }    return 0;}

题目

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

0 0