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. 【故障处理】ORA-12162: TNS:net service name is incorrectly specified

    本文将给大家阐述一个因未设置系统环境变量ORACLE_SID导致ORA-12162错误的案例.希望大家有所思考. 1.获得有关ORA-12162报错信息的通用表述信息[oracle@asdlabdb0 ...

  2. SourceInsight支持Python代码阅读

    这个话题,很简单,主要是要有一个插件Python.CLF,这个文件可以从我的GitHub上下载.然后,参照下面的图片显示的步骤,就很快搞定! 具体的步骤,看下面的三张图片,顺序编号了,从1到9,对照着 ...

  3. EF LEFT JON 关联查找

    var query = (from a in context.OQC_INSPECTION_SAMPLE.Where(expression).Where(a => context.OQC_INS ...

  4. KB975517 "The update does not apply to your system"

    https://www.manageengine.com/products//desktop-central/patch-management/Windows-Vista-Ultimate-Editi ...

  5. erlang的escript脚本

    参考霸爷的博客 测试例子 #!/usr/bin/env escript %%! -smp enable -sname mmcshadow -mnesia debug verbose[/color] m ...

  6. /proc 文件系统

    linux提供了一种特殊的文件系统procfs,通常以/proc目录的形式呈现.该目录中包含了许多特殊文件用来对驱动程序和内核信息进行更高层的访问.只要应用程序有正确的访问全息,就可以通过读写这些文件 ...

  7. virtual修饰符

    virtual(C# 参考) virtual 关键字用于修饰方法.属性.索引器或事件声明,并使它们可以在派生类中被重写. 例如,此方法可被任何继承它的类重写. public virtual doubl ...

  8. 黄聪:如何开启IIS7以上的“IIS6管理兼容性”

    护卫神PHP套件的安装,需要开启“IIS6管理兼容性”, 那么,如何开启IIS7.IIS7.5.IIS8.0的IIS6兼容模式呢? 设置的时候,请参照如下截图:

  9. (C/C++ interview) Static 详解

    C Static http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program Static could ...

  10. working copy locked 问题

    解法 1 :  右键svn-->clean up 解法 2 :  被lock的文件夹进入控制台 del lock /q/s [转载解法] SVN 本地更新时,由于一些操作中断更新,如磁盘空间不够 ...