POJ 2785 4 Values whose Sum is 0二分入门
2016-07-24 10:30
323 查看
4 Values whose Sum is 0
题目
本题链接Time Limit:15000MS Memory Limit:228000KB
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 2 28 ) 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 -46
26 -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).
分析
显而易见四组数据一一枚举肯定会超时,故本题应采用二分法,将四组数据分别合并成两组数据的和(两个4000*4000数组),进行排序,去重,可二分查找到结果并输出,另外本题也可采用hash做法。代码
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; int a[4005]; int b[4005]; int c[4005]; int d[4005]; int tmp1[16000005]; int tmp2[16000005]; int re1[16000005][2]; int re2[16000005][2]; int bs(int key, int num) { int lo=0,hi=num; int mi; while(lo<=hi) { mi=((hi-lo)>>1)+lo; if(re1[mi][0]==key) return mi; else if(re1[mi][0]<key) lo=mi+1; else hi=mi-1; } return -1; } int main() { int n; int cnt1 = 0, cnt2 = 0; int sum = 0; scanf("%d",&n); 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++){ tmp1[i*n+j] = a[i] + b[j]; tmp2[i*n+j] = c[i] + d[j]; } sort(tmp1,tmp1+n*n); sort(tmp2,tmp2+n*n); re1[0][0] = tmp1[0]; re2[0][0] = tmp2[0]; for(int i=1; i<n*n; i++) { if(tmp1[i-1] != tmp1[i]) { cnt1++; re1[cnt1][0] = tmp1[i]; } else if(tmp1[i-1] == tmp1[i]) { re1[cnt1][1]++; } if(tmp2[i-1] != tmp2[i]) { cnt2++; re2[cnt2][0] = tmp2[i]; } else if(tmp2[i-1] == tmp2[i]) { re2[cnt2][1]++; } } for(int i=0; i<cnt2+1; i++) { int t = bs(-re2[i][0],cnt1); if(t+1) sum += (re2[i][1]+1) * (re1[t][1]+1); } printf("%d\n", sum); return 0; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 2015年acm国内排名
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 快速排序里的学问:从猜数字开始
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833