poj 2785 双向搜索+哈希
来源:互联网 发布:娱乐圈的水有多深知乎 编辑:程序博客网 时间:2024/05/16 23:51
4 Values whose Sum is 0
Time Limit: 15000MS Memory Limit: 228000KTotal Submissions: 16271 Accepted: 4718Case Time Limit: 5000MS
Description
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .
Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 228 ) that belong respectively to A, B, C and D .
Output
For each input file, your program has to write the number quadruplets whose sum is zero.
Sample Input
6-45 22 42 -16-41 -27 56 30-36 53 -37 77-36 30 -75 -4626 -38 -10 62-32 -54 -6 45
Sample Output
5
Hint
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
Source
Southwestern Europe 2005
先枚举出所有a[i]+b[j]的值,存到哈希表中。
再枚举所有tmp=c[i]+d[j]的值,检查-tmp是否在哈希表中,返回个数。
3200+ms.如果用排序加二分查找5000+ms。。。
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;#define maxn 4001#define M 21331131const int inf =(1<<29);int a[maxn], b[maxn],c[maxn],d[maxn];int n;struct node{ node(){} node(int v, int n, int c){val = v; next =n; cnt = c;} int val; int next; int cnt;};node h[maxn*maxn];int first[M], top; //邻接表写法,first为表头指针void insert(int v){ int pos = (v+inf)%M; int tmp = -1; for(int i = first[pos]; i!=-1; i=h[i].next) if(h[i].val == v){ h[i].cnt++; return; } h[top] = node(v, first[pos], 1); first[pos] = top++;}int find(int v){ int pos = (v+inf)%M; for(int i = first[pos]; i != -1; i=h[i].next) if(v == h[i].val) return h[i].cnt; return 0;}int main(){ while(scanf("%d", &n)!=EOF){ memset(first, -1, sizeof(first)); top = 0; for(int i = 0; i < n; i++) scanf("%d%d%d%d", a+i, b+i, c+i, d+i); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) insert(a[i]+b[j]); long long ans = 0; int tmp; for(int i = 0; i < n; i++) for(int j = 0; j < n; j++){ tmp = c[i]+d[j]; ans += find(-tmp); } printf("%lld\n", ans); } return 0;}
0 0
- poj 2785 双向搜索+哈希
- poj 2549 双向搜索
- POJ 1840 双向搜索 +Hash
- poj 3131 双向搜索+hash判重
- POJ 1915 Knight Moves 双向搜索
- (搜索,双向BFS)POJ.1915.Knight Moves
- POJ 3131 双向搜索 解题报告
- HDU 3095 哈希+双向搜索
- poj 2785 4 Values whose Sum is 0(双向搜索)
- POJ 2785 4 Values whose Sum is 0(双向搜索+二分)
- 160_折半枚举(双向搜索) 4 Values whose sum is 0 (POJ No.2785)
- POJ 2785 4 Values whose Sum is 0 折半枚举(双向搜索)
- POJ 2785 4 Values whose Sum is 0【双向搜索/折半枚举】
- HDU 1043 && POJ 1077 Eight bfs || 双向bfs || A*搜索
- 双向搜索算法
- 双向广度优先搜索
- 双向搜索算法
- 双向搜索 --- 字串变换
- Cocos2d-x源码阅读1 UI树1(第一次系统而有成效的阅读源码的感悟)
- sqlsever
- JAVA中对一维数组排序的方法(在快速排序上进行的优化)
- Android输出Log的包装
- [leetcode][arrat]Search Insert Position
- poj 2785 双向搜索+哈希
- 向Android系统中添加模块及产品流程
- Android学习:Eclipse、SDK开发环境配置记录
- Swift基础知识(二)
- c语言编程基础------0.5.3 c应用程序之argc argv以及return 和exit,abort,assert的解释
- positon 总结之登入背景玻璃透明效果扩展
- 回家前的周六晚上
- C++11的模版类array
- IOS 开发,调用打电话,发短信,打开网址