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. Servlet Listener

    需要继承ServletContextListener接口. 代码: package com.my; import java.io.*; import javax.servlet.*; import j ...

  2. 【转】Asp.Net MVC3 简单入门详解过滤器Filter

    原文地址:http://www.cnblogs.com/boruipower/archive/2012/11/18/2775924.html 前言 在开发大项目的时候总会有相关的AOP面向切面编程的组 ...

  3. android学习笔记43——图形图像处理3——Path

    Path类 Path类可以预先在View上将N个点连成一条“路径”,然后调用Canavas的drawPath(path,paint)即可沿着路径绘制图形. android还为路径绘制提供了PathEf ...

  4. 水晶报表(web)表格信息展示

    一.环境安装 开发工具使用VS2010+SAP Crystal Reports13_0+.NETformwork4.0 因为vs2010已经不再集成水晶报表,所以需要我们去找合适的版本下载http:/ ...

  5. linux系统中实现mongodb3.0.5数据库自动备份

    最近两天,因公司业务需要,要定期备份mongodb数据库中的数据. 查了很多资料后,发现mongodb似乎并没有自带的定时备份功能,于是只好转移目标到linux系统的定时任务上,于是学习并使用了cro ...

  6. 黄聪:wordpress工作原理

    WP初始化的过程:当你输入<yourlink>/wordpress对wordpress进行初始化时,wordpress默认会找根目录下的index.php页面,看一下index.php页面 ...

  7. JavaScript如何获得昨天明天等日期

    <script type="text/javascript"> function GetDateStr(AddDayCount) { var dd = new Date ...

  8. studio--如何将Eclipse中的项目迁移到Android Studio 中

    如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的是生成Build Gradle的文件.因为Android Studi ...

  9. Android NDK 构建 以及一些错误

    首先下载NDK 然后在最外层的 local.properties文件夹中 写上  ndk的路径 ndk.dir=/Users/wyj/Desktop/androidStudioSdk/android- ...

  10. Redis中7种集合类型应用场景&redis常用命令

    Redis常用数据类型 Redis最为常用的数据类型主要有以下五种: String Hash List Set Sorted set 在具体描述这几种数据类型之前,我们先通过一张图了解下Redis内部 ...