Trouble

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3526    Accepted Submission(s): 1113

Problem Description
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?
 
Input
First 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.
 
Output
For 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个数,如果存在满足a1 +a2 + a3 + a4 +a5 = 0

分析:

事实上考虑如下问题,快速求解序列A,序列B中,是否有Ai+Bj=x ,记得是某知名公司的一道面试题吧;

是两个指针的应用,将A,B升序排列,初试 i 指针指向A[1] ,j 指针指向 B[b_size] ,比较Ai + Bj 与 x 的

大小。若A[i]+B[j]<x , i 指针右移 ; 若 A[i]+B[j]>x , j 指针左移 。事实上是一个贪心的思想。

个人感悟:

使用returen 比 break 好吧。

#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
using namespace std ;
typedef long long LL ;
struct Me{
LL N ,N_2 ,a_size ,b_size ,c_size;
LL num[][] ;
LL A[*] ;
LL B[*] ;
LL C[] ;
Me(){}
Me(int n):N(n){}
void read_init(){
for(int i=;i<=;i++)
for(int j=;j<=N;j++)
scanf("%I64d",&num[i][j]) ;
int k ;
k=;
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
A[++k]=num[][i]+num[][j];
k= ;
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
B[++k]=num[][i]+num[][j];
for(int i=;i<=N;i++)
C[i]=num[][i] ;
}
int my_search(LL x){
int i= ;
int j=b_size ;
while(i<=a_size&&j>=){
if(A[i]+B[j]<x)
i++ ;
else if(A[i]+B[j]==x)
return ;
else if(A[i]+B[j]>x)
j-- ;
}
return ;
}
int calc(){
sort(A+,A++N*N) ;
a_size=unique(A+,A++N*N)-(A+) ;
sort(B+,B++N*N) ;
b_size=unique(B+,B++N*N)-(B+) ;
sort(C+,C++N) ;
c_size=unique(C+,C++N)-(C+) ;
for(int i=;i<=c_size;i++){
if(my_search(-*C[i]))
return ;
}
return ;
}
void gao_qi(){
read_init() ;
if(calc())
puts("Yes") ;
else
puts("No") ;
}
};
int main(){
int T ,N ;
cin>>T ;
while(T--){
scanf("%d",&N) ;
Me me(N) ;
me.gao_qi() ;
}
return ;
}

HDU 4334 Trouble的更多相关文章

  1. HDU 4334 Trouble (暴力)

    Trouble Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  2. HDU 4334 Trouble (数组合并)

    Trouble Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. HDU 4334——Trouble——————【贪心&水题】

    Trouble Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

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

    给定五个集合.问是否能从五个集合各取一个元素,使得元素之和为0. 这道题有两种做法,一种是哈希,然而之前没写过哈希.....比赛后从大神那copy了一份. 这里说还有一种. 对于这五个集合分为三组.1 ...

  5. [hdu P4334] Trouble

    [hdu P4334] Trouble Hassan is in trouble. His mathematics teacher has given him a very difficult pro ...

  6. Trouble HDU - 4334

    Hassan is in trouble. His mathematics teacher has given him a very difficult problem called 5-sum. P ...

  7. HDU OJ 4334 Trouble 2012 Multi-University Training Contest 4

    题目:click here 题意: 给定5组数据,每组数据选择一个数,看是否能找到5个数的和为零. 分析: 千万不要~~T~~ 普通线性查找: #include <iostream> #i ...

  8. PKU 4334 Trouble(哈希)

    原题链接 思路:哈希存入相反数 注意:HDU不支持long long要使用__int64 #include<cstdio> #include<cstring> #define ...

  9. HDU 4334 5-sum

    题目大意: 从5个集合中个选取一个数出来,使5个数相加之和为0 , 判断是否存在这种可能 因为集合数目最多200,那么200^3 = 8000000 , 那么这里很明显要把5个数拆成2个和3个计算,因 ...

随机推荐

  1. java mail使用中遇到的550类型错误

    前言 首先,需要说明的是,本错误来自于一个简单的基于java mail的api程序,邮件服务器是163的SMTP,即smtp.163.com. 程序 需要说明一下,下面这个程序,是来自于网络上,本人为 ...

  2. 剑指offer系列44---只出现一次 的数字

    [题目]一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. * [思路]异或性质:数异或自己即为0: * 一个数组中,从头到尾异或的结果为不重复数字异或结果. ...

  3. Redis 宣言(Redis Manifesto)

    Redis 的作者 antirez(Salvatore Sanfilippo)曾经发表了一篇名为 Redis 宣言(Redis Manifesto)的文章,文中列举了 Redis 的七个原则,以向大家 ...

  4. cgic 写CGI程序

    CGIC是C语言CGI库函数,用于编写CGI程序 CGIC 主要完成以下功能: * 对数据进行语法分析 * 接收以 GET 和 PSOT 两种方式发送的数据 * 把 FORM 中的不同域连接成连续的串 ...

  5. svn merge 回滚

    聊一聊 svn merge 命令. svn 是啥就不用介绍了吧,谁用谁知道.有了 svn,开发者只要把代码提交上去,无论山崩地裂.电脑进水.硬盘格式化,哪怕换了一台电脑,都能随时把代码找回来.不过从自 ...

  6. 127 Word Ladder

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  7. Android Please ensure that adb is correctly located at问题解决

    转载于:http://breezylee.iteye.com/blog/2032588 遇到问题描述: 运行android程序控制台输出 [2012-07-18 16:18:26 - ] The co ...

  8. CSS图片垂直居中方法

    让div里面的多行文本垂直居中的方法: div{height:100px;width:100px;border:solid 1px red;text-align:center; display:tab ...

  9. 黄聪:wordpress源码解析-目录结构-文件调用关系(转)

    Wordpress是一个单入口的文件,所有的前端处理都必须经过index.php,这是通过修改web服务器的rewrite规则来实现的.这种做法的好处是显而易见的,这样URL更好看,不必为每一个url ...

  10. (C/C++) memset

    C语言: memset   extern void *memset(void *buffer,int c,int count);   #include <string.h>   功能:把b ...