给定五个集合。问是否能从五个集合各取一个元素,使得元素之和为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(哈希|线性查找)的更多相关文章

  1. PKU 4334 Trouble(哈希)

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

  2. HDU 4334 Trouble (暴力)

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

  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 (数组合并)

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

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

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

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

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

  7. List<T>线性查找和二分查找BinarySearch效率分析

    今天因为要用到List的查找功能,所以写了一段测试代码,测试线性查找和二分查找的性能差距,以决定选择哪种查找方式. 线性查找:Contains,Find,IndexOf都是线性查找. 二分查找:Bin ...

  8. hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. 数组查找算法的C语言 实现-----线性查找和二分查找

    线性查找  Linear Search 用户输入学生学号的成绩 二分查找  Binary Search 要求数据表是已经排好序的 程序存在小的瑕疵

随机推荐

  1. chromedriver 下载

    https://sites.google.com/a/chromium.org/chromedriver/downloads   百度网盘链接:https://pan.baidu.com/s/1nwL ...

  2. [转]Python 之 使用 PIL 库做图像处理

    Python 之 使用 PIL 库做图像处理 1. 简介. 图像处理是一门应用非常广的技术,而拥有非常丰富第三方扩展库的 Python 当然不会错过这一门盛宴.PIL (Python Imaging ...

  3. WPF ProgressBar 样式

    <ProgressBar Grid.Row="2" Foreground="#45d207" IsIndeterminate="True&quo ...

  4. .NET重构(五):存储过程、触发器和函数的区别

    导读:在触发器的学习过程中,师傅讲了它的耦合性高,建议我能用存储过程,那到底什么是存储过程呢,自己也不是特别了解,还有就是,触发器也算是一种特殊的存储过程,为什么就不建议多用呢?接下来,就谈谈触发器. ...

  5. [AHOI2009]维护序列 (线段树)

    题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,-,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一 ...

  6. 【单调队列优化dp】uestc 594 我要长高

    http://acm.uestc.edu.cn/#/problem/show/594 [AC] #include<bits/stdc++.h> using namespace std; t ...

  7. 【基础操作】FFT / DWT / NTT / FWT 详解

    1. 2. 点值表示法 假设两个多项式相乘后得到的多项式 的次数(最高次项的幂数)为 $n$.(这个很好求,两个多项式的最高次项的幂数相加就得到了) 对于每个点,要用 $O(n)$ 的时间 把 $x$ ...

  8. spring security 登录、权限管理配置

    登录流程 1)容器启动(MySecurityMetadataSource:loadResourceDefine加载系统资源与权限列表)  2)用户发出请求  3)过滤器拦截(MySecurityFil ...

  9. AtCoder Regular Contest 095E - Symmetric Grid

    $n \leq 12,m \leq 12$,$n$行$m$列小写字母,现可做无数次操作:交换两行:交换两列.问是否有可能把他变成中心对称的. 没有去想分组枚举的复杂度QAQ 行和列的操作顺序是随意的. ...

  10. bigdata related

    hive: http://lxw1234.com/archives/2015/07/413.htm 搜狗实验室数据集: https://www.sogou.com/labs/resource/list ...