HDU 4334 Trouble
Trouble
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3526 Accepted Submission(s): 1113
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?
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
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的更多相关文章
- HDU 4334 Trouble (暴力)
Trouble Time Limit: 5000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- HDU 4334 Trouble (数组合并)
Trouble Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 4334——Trouble——————【贪心&水题】
Trouble Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 4334 Trouble(哈希|线性查找)
给定五个集合.问是否能从五个集合各取一个元素,使得元素之和为0. 这道题有两种做法,一种是哈希,然而之前没写过哈希.....比赛后从大神那copy了一份. 这里说还有一种. 对于这五个集合分为三组.1 ...
- [hdu P4334] Trouble
[hdu P4334] Trouble Hassan is in trouble. His mathematics teacher has given him a very difficult pro ...
- Trouble HDU - 4334
Hassan is in trouble. His mathematics teacher has given him a very difficult problem called 5-sum. P ...
- HDU OJ 4334 Trouble 2012 Multi-University Training Contest 4
题目:click here 题意: 给定5组数据,每组数据选择一个数,看是否能找到5个数的和为零. 分析: 千万不要~~T~~ 普通线性查找: #include <iostream> #i ...
- PKU 4334 Trouble(哈希)
原题链接 思路:哈希存入相反数 注意:HDU不支持long long要使用__int64 #include<cstdio> #include<cstring> #define ...
- HDU 4334 5-sum
题目大意: 从5个集合中个选取一个数出来,使5个数相加之和为0 , 判断是否存在这种可能 因为集合数目最多200,那么200^3 = 8000000 , 那么这里很明显要把5个数拆成2个和3个计算,因 ...
随机推荐
- 不包含适合于入口点的静态"Main"方法
学习新建项目.此问题做为笔记. 错误 1 程序“admin.exe”不包含适合于入口点的静态“Main”方法 原因:原来创建项目的时候,用的是“空项目”,我以为这样就会生成类库,实际上,一开始准备运行 ...
- Android 启动模拟器是出现“Failed to allocate memory: 8”错误提示的原因及解决办法
某天,Android 启动模拟器是出现“Failed to allocate memory: 8”错误,模拟器无法启动,如下图: 原因:设置了不正确AVD显示屏模式,4.0版默认的模式为WVGA800 ...
- 99. Recover Binary Search Tre
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- for语句的用法
#!/bin/bashfor i in 1 2 3 4 5 6do echo $idone 看文件 #!/bin/bashdir=$(ls /etc) for i in $dirdo echo $id ...
- iphone dev 入门实例4:CoreData入门
The iPhone Core Data Example Application The application developed in this chapter will take the for ...
- DBA_Oracle Sort排序处理空间耗用(概念)
2014-12-18 Created By BaoXinjian
- UVa 297 Quadtrees(树的递归)
Quadtrees 四分树就是一颗一个结点只有4个儿子或者没有儿子的树 [题目链接]UVa 297 Quadtrees [题目类型]树的递归 &题意: 一个图片,像素是32*32,给你两个先序 ...
- POJ 1611 The Suspects(并查集,简单)
为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...
- iis7设置404页面不生效的原因
打开web.config <httpErrors errorMode="Custom" existingResponse="PassThrough"> ...
- M2M协议
1, M2M协议 M2M是Machine-to-Machine/Man的简称,是一种以机器终端智能交互为核心的.网络化的应用与服务.M2M协议规定了人机和机器之间交互需要遵从的通信协议.随着科学技术的 ...