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 }


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

时间: 2024-12-10 22:12:44

Trouble HDU - 4334的相关文章

HDU 4334 Trouble(hash + 枚举)

HDU 4334 题意: 给五个数的集合,问能否从每个集合中取一个数,使五个数之和为0. 思路: 集合大小是200,直接枚举的复杂度是200^5,一定会超时. 直接枚举的上限是3层,我们可以将枚举剩下两个集合各任取一个元素可能组成的元素和,并将其作hash处理,使我们能很快判断枚举出来的三个集合元素和在剩下的两个集合里是否有相应元素匹配. code: /* * @author Novicer * language : C++/C */ #include<iostream> #include&l

HDU 4334——Trouble——————【贪心&amp;水题】

Trouble Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5388    Accepted Submission(s): 1494 Problem Description Hassan is in trouble. His mathematics teacher has given him a very difficult pro

HDU 4334 Trouble

合并排序再枚举. 题意问五个数组中各抽一个加起来 和是否为 0. 开始想用 200*200*200 和 200*200 比.果然TLE了. 后来知道 得 200*200,200*200 ,200 . 先200*200 的枚举,排序,去重.然后三个元素加起来,微调 两个 200*200 的指针. 这题用 %lld 就WA.得用 %I64d .贡献N多TLE.N多WA.怒刷存在感. #include<cstdio> #include<cstring> #include<strin

HDU 4334 Trouble(哈希|线性查找)

给定五个集合,问能否从五个集合各取一个元素,使得元素之和为0. 这道题有两种做法,一种是哈希,然而之前没写过哈希.....比赛后从大神那copy了一份. 这里说另一种, 对于这五个集合分为三组,1,2组求和为一组,3,4组求和分为一组,5为一组. 那么现在转化为了能否从前两组中各取一个元素,使得这两个值和为第三组一个元素的绝对值. 那么对于第一组我们升序排序,第二组我们降序排序. 对于第三组里的任一元素,假如第一组队首加第二组队首之和大于第三组的元素,那么第二组游标往后移一位,反之第一组移一位,

HDU 4334 Trouble (暴力)

题意:给定五个集合,每个集合有n个数,从每个集合各取一个数使和为0.如果能输出Yes,不能则输出No. 看了网上的题解有很多解法,个人感觉这题暴力就能过. 首先将一二两个集合合并为数组a,再将三四两个集合合并数组b,然后进行从小到大排序, 设第五个数组为数组c,依次遍历c中的每一个数,看在a,b中是否存在两个数的和与c中的数的和为0, 将数组a,b进行sort排序,然后一个正序即从小到大遍历,一个逆序从大到小遍历, 如果三个数的和小于0,a数组后移一位,否则b后移一位. 这题WA了两次,第一次W

!HDU 4334 集合各出一数和为0是否存在-思维、卡时间-(指针的妙用)

题意:有5个集合,集合的大小是n,每一个集合出一个数,问能不能找到五个数的和为0.数据范围:T<=50:n<=200 分析: 暴力枚举是n^5*T,超时,那么就要用一些技巧了. 这里有一个指针的妙用:如何在O(n)的复杂度找A,B,使得A+B==C(A,B分别属于一个数列a,b).做法是先把a,b分别按升序排序,然后一个指针i指向a的首,指针j指向b的尾,判定指针指向的数的和是否==C,若等于则结束查找,若小于,则i++,若大于则 j- -,如果有一个指针已经走到了头还没找到A+B==C,则说

HDU 4334 5-sum

题目大意: 从5个集合中个选取一个数出来,使5个数相加之和为0 , 判断是否存在这种可能 因为集合数目最多200,那么200^3 = 8000000 , 那么这里很明显要把5个数拆成2个和3个计算,因为3个的话有8000000种可能,不好保存 所以只先算前两个数40000种相加的可能性保存到hash表中,然后再后面800000次找hash表中是否存在对应的值 1 #include <cstdio> 2 #include <cstring> 3 #include <iostre

dp题目列表

10271 - Chopsticks 10739 - String to Palindrome 10453 - Make Palindrome 10401 - Injured Queen Problem 825 - Walking on the Safe Side 10617 - Again Palindrome 10201 - Adventures in Moving - Part IV 11258 - String Partition 10564 - Paths through the Ho

hdu 3591 The trouble of Xiaoqian

hdu 3591  The trouble of Xiaoqian 题意:xiaoqi要买一个T元的东西,当前的货币有N种,xiaoqi对于每种货币有Ci个:题中定义了最小数量即xiaoqi拿去买东西的钱的张数加上店家找的零钱的张数(店家每种货币有无限多张,且找零是按照最小的数量找零的):问xiaoqi买元东西的最小数量? 多重背包+完全背包: 思路:这个最小数量是拿去买东西的张数和找零的张数之和,其实我们只需要将这两个步骤分开,开算出能买T元东西的前i最少f[i]张,这里涉及到容量问题:容量只