码迷,mamicode.com
首页 > 其他好文 > 详细

Trouble HDU - 4334

时间:2018-03-06 00:53:14      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:一个   class   scanf   ace   help   include   number   lag   分治   

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

标签:一个   class   scanf   ace   help   include   number   lag   分治   

原文地址:https://www.cnblogs.com/qldabiaoge/p/8511855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!