Trouble HDU - 4334

Posted Fitz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Trouble HDU - 4334相关的知识,希望对你有一定的参考价值。

Hassan 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 Input

2
2
1 -1
1 -1
1 -1
1 -1
1 -1
3
1 2 3
-1 -2 -3
4 5 6
-1 3 2
-4 -10 -1

Sample Output

No
Yes

给你5个数组每个都有N个元素,然后在每一个数组里面取一个数看能否为0
这题5个for不用想肯定TEL

分治的思想 将第一个数组和第二个数组合并 第三个和第四个合并
这样就只有3个数组了。
下面的上代码 ,细节在代码里面体现

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 long long  cnt[6][205];
 6 long long  a[40005],b[40005],c[40005];
 7 
 8 int main() {
 9     int t;
10     scanf("%d",&t);
11     while(t--){
12         int n;
13         scanf("%d",&n);
14         for (int i=0 ;i<5 ;i++ ){
15             for (int j=0 ;j<n ;j++){
16                 scanf("%lld",&cnt[i][j]);
17             }
18         }
19         int k=0;
20         for (int i=0 ;i<n ;i++ ){
21             for (int j=0 ;j<n ;j++ ){
22                 a[k]=cnt[1][i]+cnt[2][j];
23                 k++;
24             }
25         }
26         k=0;
27         for (int i=0 ;i<n ;i++ ){
28             for (int j=0 ;j<n ;j++ ){
29                 b[k]=cnt[3][i]+cnt[4][j];
30                 k++;
31             }
32         }
33         int len=0;
34         for (int i=0 ;i<n ;i++){
35             c[len++]=cnt[0][i];
36         }
37         sort(a,a+k);
38         sort(b,b+k);
39         sort(c,c+len);
40         int flag=1,temp1,temp2;
41         for (int i=0 ;i<n ;i++){
42             temp1=k-1;
43             temp2=0;
44             while(temp1>=0 &&temp2<k) {
45                 if (a[temp1]+b[temp2]+c[i]==0) {
46                     flag=0;
47                     break;
48                 }else if (a[temp1]+b[temp2]+c[i]>0) {
49                     temp1--;
50                 }else temp2++;
51             }
52             if (flag==0) break;
53         }
54         if (flag==0) printf("Yes\n");
55         else printf("No\n");
56     }
57     return 0;
58 }

 




以上是关于Trouble HDU - 4334的主要内容,如果未能解决你的问题,请参考以下文章

hdu4334:Trouble(two pointers, 思维)

hdu 3591 The trouble of Xiaoqian

HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)

背包专题C - The trouble of Xiaoqian hdu3591混合背包:多重背包+完全背包

小草的Trouble学生信息管理系统

实训-第四天 Never trouble trouble till trouble troubles you