HDU 4334 trouble

Source: Internet
Author: User
Trouble

Time Limit: 10000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 4375 accepted submission (s): 1297


Problem descriptionhassan is in trouble. His mathematics teacher has given him a very difficult problem called 5-sum. Please help him.
The 5-sum problem is defined as follows: given 5 sets S_1 ,..., s_5 of N integer numbers each, is there A_1 in S_1 ,..., a_5 in S_5 such that a_1 +... + A_5 = 0?

 

Inputfirst line of input contains a single integer N (1 ≤ n ≤ 50 ). n test-cases follow. first line of each test-case contains a single integer N (1 <=n <= 200 ). 5 lines follow each containing N integer numbers in range [-10 ^ 15, 1 0 ^ 15]. i-th line denotes set s_ I for 1 <= I <= 5.

 

Outputfor each test-case output "yes" (without quotes) if there are A_1 in S_1 ,..., a_5 in S_5 such that a_1 +... + A_5 = 0, otherwise output "no ".

 

Sample input221-11-11-11-11-131 2 3-1-2-34 5 6-1 3 2-4-10-1

 

Sample outputnoyes
 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 #include <string.h> 5 #include <set> 6 using namespace std; 7 #define ll long long 8 ll a[5][300],s1[90000],s2[90000],s3[90000],sn1,sn2,sn3; 9 int main()10 {11     int n,t,i,j,k;12     cin>>t;13     while(t--)14     {15         cin>>n;16         for(i=0; i<5; i++)17             for(j=0; j<n; j++)18                 scanf("%I64d",&a[i][j]);19         sn1=sn2=sn3=0;20         for(i=0; i<n; i++)21             for(j=0; j<n; j++)22                 s1[sn1++]=a[0][i]+a[1][j];23         for(i=0; i<n; i++)24             for(j=0; j<n; j++)25                 s2[sn2++]=a[2][i]+a[3][j];26         for(i=0; i<n; i++)27             s3[sn3++]=-a[4][i];28         sort(s1,s1+sn1);29         sort(s2,s2+sn2);30         sort(s3,s3+sn3);31         int ok=0;32         for(i=0;!ok&&i<sn3;i++)33         {34             j=0,k=sn2-1;35             while(j<sn1&&k>=0)36             {37                 if(s1[j]+s2[k]==s3[i])38                 {39                     ok=1;40                     break;41                 }42                 while(j<sn1&&k>=0&&s1[j]+s2[k]>s3[i])k--;43                 while(j<sn1&&k>=0&&s1[j]+s2[k]<s3[i])j++;44             }45         }46         if(ok)printf("Yes\n");47         else printf("No\n");48     }49 }
View code

 

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.