O-can you find it? (dichotomy)

Source: Internet
Author: User

Description

Give 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 is many cases. Every data case was described as followed:in the first line there be three integers L, N, M, in the second line there was L integers represent the sequence A, in the third line there is N integers represent the sequences B, in the Forth line There is M integers represent the sequence C. The fifth line there was an integer s represents there was s integers X to be calculated. 1<=l, N, m<=500, 1<=s<=1000. All the integers is 32-integers.

Output

For each case, firstly you has to print the case number as the form ' case D: ', then for the S queries, and you calculate if T He formula can is 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 problem and analysis

Given three array a,b,c, each array has a number of numbers (<=500), and given a number s to determine if there is a s=a[i]+b[j]+c[k], there is a set of numbers on the output Yes, a group does not exist on the output of No.

Because there are only three 500-size arrays, I just started to write three for loop, time out, and then look at the topic more clearly, each set of test data given in S is <=1000, it is easy to timeout, and, the number can be negative, so I sort all sorts, also useless.

Helpless under direct Baidu, only to find that already AC people are first to add a array and b array of numbers into a ab[500x500] array, so that the equivalent of ab[i]+c[j]=s; and then deformation, ab[i]=c[j]+s; so I know what I'm going to do next, As long as in the AB array with the previous two-point search to see if C[j]+s can find out, seems to have had before this kind of data relations to convert the problem.

The code is as follows:

#include<cstdio>
#include<cstdlib>
int a[501],b[501],c[501],d[260000];int kNM;
IntCmp(Constvoid*a,Constvoid*b)
{
Return*(Int*) A-*(Int*) b;
}
IntMain(void)
{
int T, SMm=1;
While(scanf("%d%d%d", &k, &n, &m)!=eof)
{For(int I=0; I<k; 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]);
int cnt=0;
For(int I=0; I<n; I++)
For(Int J=0; j<m; j++)
D[CNT++]=b[I]+c[j];
Qsort(AK,sizeof(A[0]), CMP);
Qsort(dCnt,sizeof(d[0]), CMP);
scanf("%d", &t);
Printf("Case%d:\ n"Mm++);
While(t--)
{
int flag=0;
scanf("%d", &s);
For(int I=0; I<k; I++)
{
int TL=0Tr=cnt-1;
int TT=s-A[I];
While(TL<tr)
{
int mid= (TL+tr)/2;
If(d[TL]==tt|| D[TR]==tt|| D[Mid]==tt){Flag=1;Break;}
ElseIf(d[Mid]<tt) TL=mid+1;
else TR=mid-1;
}
If(Flag==1)Break;
}
If(Flag==1) puts< Span class= "Sh-symbol" > ( "YES"              else if (Flag==0puts ( "NO"           }
      }
     return 0
/span>

O-can you find it? (dichotomy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.