HDU 4334 Trouble(哈希|线性查找)
给定五个集合。问是否能从五个集合各取一个元素,使得元素之和为0.
这道题有两种做法,一种是哈希,然而之前没写过哈希.....比赛后从大神那copy了一份。
这里说还有一种。
对于这五个集合分为三组。1,2组求和为一组,3,4组求和分为一组,5为一组。
那么如今转化为了是否能从前两组中各取一个元素。使得这两个值和为第三组一个元素的相反数。
那么对于第一组我们升序排序。第二组我们降序排序。
对于第三组里的任一元素,假如第一组队首加第二组队首之和大于第三组的元素。那么第二组游标往后移一位,反之第一组移一位,
那么这个查找时间就为O(m),m为数组元素个数。
那么总的时间复杂度为O(n*n*n).
<pre class="cpp" name="code">#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#define eps 1e-6
#define LL long long
#define pii pair<int,int>
using namespace std; //const int maxn = 100 + 5;
//const int INF = 0x3f3f3f3f; LL s[6][205];
LL s12[200*202], s34[200*202];
int n;
bool check(LL t) {
int y12 = 0, y34 = n*n-1;
// cout << y12 << endl << y34 << endl;
while(y12<n*n && y34>=0) {
LL tmp = s12[y12] + s34[y34];
// cout << s12[y12] << endl << s34[y34] << endl;
// cout << tmp << endl;
if(tmp == t) return true;
else if(tmp < t) y12++;
else y34--;
}
return false;
} int main() {
// freopen("input.txt", "r", stdin);
int t; cin >> t;
while(t--) {
cin >> n;
for(int i = 0; i < 5; i++) {
for(int j = 0; j < n; j++) scanf("%I64d", &s[i][j]);
}
int y1 = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
LL tmp = s[0][i]+s[1][j];
s12[y1++] = tmp;
}
}
int y2 = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
LL tmp = s[2][i]+s[3][j];
s34[y2++] = tmp;
}
}
// cout << y1 << y2 << endl;
sort(s12, s12+n*n);
sort(s34, s34+n*n);
int tag = 0;
for(int i = 0; i < n; i++) if(check(-s[4][i])) {
tag = 1; break;
}
// cout << check(4) << endl;
if(tag) puts("Yes");
else puts("No");
}
return 0;
}
HDU 4334 Trouble(哈希|线性查找)的更多相关文章
- PKU 4334 Trouble(哈希)
原题链接 思路:哈希存入相反数 注意:HDU不支持long long要使用__int64 #include<cstdio> #include<cstring> #define ...
- 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——————【贪心&水题】
Trouble Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU OJ 4334 Trouble 2012 Multi-University Training Contest 4
题目:click here 题意: 给定5组数据,每组数据选择一个数,看是否能找到5个数的和为零. 分析: 千万不要~~T~~ 普通线性查找: #include <iostream> #i ...
- List<T>线性查找和二分查找BinarySearch效率分析
今天因为要用到List的查找功能,所以写了一段测试代码,测试线性查找和二分查找的性能差距,以决定选择哪种查找方式. 线性查找:Contains,Find,IndexOf都是线性查找. 二分查找:Bin ...
- hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 数组查找算法的C语言 实现-----线性查找和二分查找
线性查找 Linear Search 用户输入学生学号的成绩 二分查找 Binary Search 要求数据表是已经排好序的 程序存在小的瑕疵
随机推荐
- 关于MongoDB分布式高可用集群实现
一.环境准备 1.本例使用3台Linux主机,IP地址如下: 点击(此处)折叠或打开 Server B Server C 2.根据需要,开启相应主机防火墙的相关端口.本次需要用到3台主机,所以开启这3 ...
- spring cloud 学习资源
1.https://blog.csdn.net/column/details/17737.html 2.https://blog.csdn.net/column/details/15197.html? ...
- Ribbitmq
rittbiMQ: 连接远程rabbitmq server sudo rabbitmqctl addser mihon mihon123 sudo rabbitmqctl set_permission ...
- mysql数据库修改字段类型
修改字段类型: alter table 表名 modify column 字段名字 decimal(18, 4) ;
- TOJ 3134: 渊子赛马修改版
3134: 渊子赛马修改版 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 458 ...
- ajax原生post请求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- iOS学习笔记21-推送证书与秘钥申请
---恢复内容开始--- 一.iOS证书 iOS常用的证书包括开发证书和发布证书,无论是真机调试还是最终发布应用到App Store这两个证书都是必须的,它是iOS开发的基本证书.Xcode7以后真机 ...
- Luogu【P3609】蹄子剪刀布(DP+滚动数组)
题目链接 (突然高兴 又一次瞬间想出转移方程并一遍A掉!!233333(虽然从二叉苹果树那题开始我就发现我的方程好像跟别人不大一样 (所以这样就可以名正言顺的水题解了 设f[i][j][k]表示考虑F ...
- Python GUI 之 Treeview 学习
例子1 from tkinter import *import tkinter.ttk as ttk win = Tk()win.title("Treeview 学习") col ...
- hdu 4819 Mosaic 树套树 模板
The God of sheep decides to pixelate some pictures (i.e., change them into pictures with mosaic). He ...