C language sorting -- Can you find it? (Hdu 2141)

Source: Internet
Author: User

C language sorting -- Can you find it? (Hdu 2141)
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 invalid 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, 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 same th 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 31 2 31 2 31 2 331410
Sample Output
Case 1:NOYESNO

The first row of input data is three numbers, indicating the size of the three arrays. (the maximum size of the three arrays is 500.) The next three rows are the numbers of the three arrays. then, enter an integer n in the fourth row to indicate the next n rows of test data. each test data is an integer m. it is required that the sum of the numbers (three in total) of each array in the preceding three arrays is equal to the m value of the test data. if YES is found, NO is output. If NO such three numbers exist, NO is output.

General idea:
Because each array of the data given by the question has a maximum of 500 elements, if the three for loops are 500*500*500, it will definitely time out, (Let's call the first, second, and third arrays a, B, and c arrays here.) At this time, we can sort the c array and enumerate a with two for loops, B is used to search for all the combined k and k in the array c. in this way, the maximum time is 500*500 * log500 ≈ 2250000 or in, the number of m-c [I] in an array composed of k in the array 'B' is searched in binary form (the code below is ).
The Code is as follows:
#include 
 
  #include using namespace std;int a[501],b[501],m[250001],c[501],flag;void find(int a[],int b,int n){    int left,right,mid;    left=0,right=n-1;    while(left<=right)    {        mid=(left+right)/2;        if(a[mid]==b)        {            flag=1;            break;        }        else if(a[mid]>b)            right=mid-1;        else            left=mid+1;    }}int main(){    int i,j,k,n1,n2,n3,n4,ci=0;    while(scanf("%d%d%d",&n1,&n2,&n3)!=EOF)    {        ci++;        for(i=0;i
  
 

Related Article

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.