CSU-ACM2016 summer training Training 1-binary search A, csu-acm20161-

Source: Internet
Author: User

CSU-ACM2016 summer training Training 1-binary search A, csu-acm20161-
Time Limit:3000 MSMemory Limit:10000KB64bit IO Format:% I64d & % I64u

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 too 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 problem-solving ideas: give three strings of integers A, B, C, and integer X. If there is A group of numbers Ai, Bj, Ck, if Ai + Bj + Ck = X is true, "YES" is output; otherwise, "NO" is output ". Use int for the data type. First, we can see that the question is about brute-force cracking. There are three for loops, and the time complexity is O (n3 ). This method is not applicable when n reaches a certain scale. It is easy to think of binary search, which is more efficient. Train of Thought: sum the last two sequences and de-duplicate the results. The time complexity is O (n2 ). Enumeration of the first sequence, binary retrieval and sequence. The time is nlgn. Therefore, the total time complexity is O (n2, which is feasible. Gains: As I learned the binary search application in my class yesterday, I am still familiar with it. array deduplication wastes a little time. Because I saved two arrays and created a new array, if it is unnecessary to create an array to save the result after deduplication, you can directly use the original array. Due to the order, as long as you compare the first two numbers, starting from the first number, the position where the counter appears for the first time, and then traverse it until the new number appears, move it forward, overwrite the duplicate value, and Add 1 to the counter.
# Include <iostream> # include <algorithm> using namespace std; int main () {int A [501], B [501], C [501], temp [250001]; int L, N, M, S, I; int d = 1; while (cin> L> N> M) {if (L <1 | L> 500 | N <1 | N> 500 | M <1 | M> 500) return-1; // input for (I = 0; I <L; I ++) cin> A [I]; for (I = 0; I <N; I ++) cin> B [I]; for (I = 0; I <M; I ++) cin> C [I]; cin> S; if (S <1 | S> 1000) return-1; // calculate the sum of the last two groups and save them to temp. int tp = 0; for (I = 0; I <N; I ++) for (int t = 0; t <M; t ++) {temp [tp] = B [I] + C [t]; tp ++;} sort (temp, temp + M * N); // array deduplication int tp1 = 0; for (I = 1; I <M * N; I ++) {if (temp [I] = temp [I-1]) continue; else {tp1 ++; temp [tp1] = temp [I] ;}} cout <"Case" <d <":" <endl; while (S --) {int flag = 1; int X; cin> X; for (I = 0; I <L; I ++) {int lo = 0, hi = tp1; int mi; while (lo <= hi) {mi = (hi-lo)> 1) + lo; // int if (A [I] + temp [mi]) = X) {flag = 0; break ;} else if (A [I] + temp [mi] <X) lo = mi + 1; else hi = mi -1 ;}} if (! Flag) cout <"YES" <endl; else cout <"NO" <endl;} d ++;} return 0 ;}

 

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.