Hdu oj 2141

Source: Internet
Author: User

Question address: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2141

The key to solving the problem is to convert the judgment A + B + C = x to a + B = x-C, calculate all the combinations of A + B, and then quickly rank and perform binary search.

After optimization, C is also sorted, which can reduce the search space continuously in the search, but it does not seem to be much faster. BelowCode

# Include <cstdio> <br/> # include <cstdlib> <br/> # include <iostream> <br/> using namespace STD; </P> <p> // # define test </P> <p> int a_array [500]; <br/> int a_array_len; </P> <p> int B _array [500]; <br/> int B _array_len; </P> <p> int c_array [500]; <br/> int c_array_len; </P> <p> long d_array [500]; <br/> int d_array_len; </P> <p> long sum_array [250000]; <br/> int sum_array_len; </P> <p> int CMP (const void * a, const voi D * B) <br/>{< br/> long s = * (int *) A-* (int *) B; <br/> If (S> 0) return 1; <br/> If (S = 0) return 0; <br/> If (S <0) return-1; <br/>}</P> <p> int cmpc (const void * a, const void * B) <br/> {<br/> long s = * (int *) B-* (int *) A; <br/> If (S> 0) return 1; <br/> If (S = 0) return 0; <br/> If (S <0) Return-1; <br/>}</P> <p> void prepare () <br/>{< br/> sum_array_len = 0; </P> <p> for (int I = 0; I <a_array_len; ++ I) {<br/> for (Int J = 0; j <B _array_len; ++ J) {<br/> sum_array [sum_array_len ++] = a_array [I] + B _array [J]; <br/>}</P> <p> # ifdef test </P> <p> printf ("test log: \ n "); <br/> for (INT I = 0; I <sum_array_len; ++ I) cout <sum_array [I] <","; <br/> printf ("\ n"); </P> <p> # endif </P> <p> qsort (sum_array, sum_array_len, sizeof (sum_array [0]), CMP); <br/> qsort (c_array, c_array_len, siz EOF (c_array [0]), cmpc); <br/> # ifdef test </P> <p> printf ("test log: \ n "); <br/> for (INT I = 0; I <sum_array_len; ++ I) cout <sum_array [I] <","; <br/> printf ("\ n"); <br/> for (INT I = 0; I <c_array_len; ++ I) printf ("% d ,", c_array [I]); <br/> printf ("\ n "); </P> <p> # endif </P> <p >}</P> <p> bool judge (const Int & X) <br/> {<br/> for (INT I = 0; I <c_array_len; ++ I) {<br/> d_array [I] = x-c_array [I]; <br/>}</P> <p> Int low = 0; <br/> int high = sum_array_len-1; <br/> int center; </P> <p> for (INT I = 0; I <d_array_len; ++ I) {<br/> while (high> = low) {<br/> center = (low + high)/2; <br/> If (sum_array [center] = d_array [I]) return true; <br/> If (sum_array [center]> d_array [I]) {<br/> high = center-1; <br/>} else {<br/> low = center + 1; <br/>}</P> <p> low = center; <br/> high = sum_array_len-1; <B R/>}</P> <p> return false; <br/>}</P> <p> int maxinarray (INT array [], const Int & Len) <br/>{< br/> int max = array [0]; </P> <p> for (INT I = 1; I <Len; ++ I) {<br/> If (array [I]> MAX) max = array [I]; <br/>}</P> <p> return Max; <br/>}</P> <p> int mininarray (INT array [], const Int & Len) <br/>{< br/> int min = array [0]; </P> <p> for (INT I = 1; I <Len; ++ I) {<br/> If (array [I] <min) min = array [I]; <br/>}</P> <p> Return min; <br/>}</P> <p> inline void Yes () <br/> {<br/> printf ("Yes \ n "); <br/>}</P> <p> inline void no () <br/>{< br/> printf ("NO \ n "); <br/>}</P> <p> int main () <br/> {<br/> int case_count = 0; </P> <p> while (scanf ("% d", & a_array_len, & B _array_len, & c_array_len )! = EOF) {<br/> printf ("case % d: \ n", ++ case_count); <br/> d_array_len = c_array_len; </P> <p> for (INT I = 0; I <a_array_len; ++ I) {<br/> scanf ("% d", a_array + I ); <br/>}</P> <p> for (INT I = 0; I <B _array_len; ++ I) {<br/> scanf ("% d ", B _array + I); <br/>}</P> <p> for (INT I = 0; I <c_array_len; ++ I) {<br/> scanf ("% d", c_array + I); <br/>}</P> <p> int x_count; <br/> scanf ("% d", & x_count); <br/>/* bool prepared = false; <Br/> long max_sum = maxinarray (a_array, a_array_len) + maxinarray (B _array, B _array_len) <br/> + maxinarray (c_array, c_array_len ); <br/> long min_sum = mininarray (a_array, a_array_len) + mininarray (B _array, B _array_len) <br/> + mininarray (c_array, c_array_len ); */<br/> prepare (); <br/> for (INT I = 0; I <x_count; ++ I) {<br/> int X; <br/> scanf ("% d", & X); <br/>/* If (x> max_sum) {<br/> NO (); <Br/> continue; <br/>} else if (x <min_sum) {<br/> NO (); <br/> continue; <br/>}< br/> If (! Prepared) {<br/> prepare (); <br/> prepared = true; <br/>}*/<br/> bool result = judge (X ); <br/> If (result) {<br/> Yes (); <br/>}else {<br/> NO (); <br/>}</P> <p> return 0; <br/>}
In my opinion, the comments are "obviously correct" pruning, But wa does not understand why.

The bad thing is that the judge function once forgot to write return false, which wasted a lot of time, and GCC didn't report warning! I don't know how to raise the warning level in the command line. I 'd like to hear it.

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.