Can you find it?

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

by talk:不想说话,心好累,wa许久后的ac。题意就是说让数列A,B,C里的数相加如果等于x输出YES,否则输出NO。这里相加是任意的。比如例子中x=1,三个最小的数1+1+1=3,相加都不等于1,输出no.x=4,取1+1+2=4 输出yes。x=10,三个最大的数相加3+3+3=9 都不为10 输出no.按照我们的思路就是定义一个sum[500*500*500]的数组将所有元素相加的情况存入数组中,再判断sum[i]=x但这样明显会超时。那么我们不妨数列A,B所有元素相加的情况存入一个数组里sum[500*500],然后再判断sum[p]是否=x-c[j]。因为p最大是500*500,我们对p二分,进而提高效率。

#include<cstdio>

#include<iostream>

#include<algorithm>
using namespace std;
int a[503],b[503],c[503];
long long sum[503*503];
int main()
{
    int l,n,m,i,j,kase=0,s,p,x,t;
    while(scanf("%d%d%d",&l,&n,&m)!=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]);
        p=0;
        for(i=0;i<l;i++)
       {
        for(j=0;j<n;j++)
        {
            sum[p++]=a[i]+b[j];
        }


       }
       sort(sum,sum+p);
       scanf("%d",&s);
       printf("Case %d:\n",++kase);
       while(s--)
     {
       int flag;
       scanf("%d",&x);
       flag=0;
       for(j=0;j<m;j++)
       {
            t=x-c[j];
          int left,right,mid;
           left=0;right=p-1;
           while(right-left>=0)
           {
            mid=(left+right)/2;
            if(sum[mid]>t)
            right=mid-1;
            else if(sum[mid]<t)
            left=mid+1;
            else
            {
                flag=1;
                break;
            }
         }
          if(flag)
              break;
    }
    if(flag==1)
        printf("YES\n");
    else
        printf("NO\n");
   }
    }
   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值