POJ 2785 (二分)
来源:互联网 发布:网络延迟单位 编辑:程序博客网 时间:2024/06/06 02:37
4 Values whose Sum is 0
Time Limit: 15000MS Memory Limit: 228000KTotal Submissions: 18785 Accepted: 5579Case 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
题意是给出4个数列,每个数列n个数,要求从每个数列中取出一个数使得4个数的和为0。
先取两组记录两两的和,然后枚举另外两组两两的和从已知的和里面去找。
可能map常数比较大过不了,二分就可以了。
#include <iostream>#include <cmath>#include <cstdlib>#include <time.h>#include <vector>#include <cstdio>#include <cstring>#include <map>#include <algorithm>using namespace std;#define maxn 4111int n;long long a[maxn][4];long long sum[maxn*maxn];int main () {while (scanf ("%d", &n) == 1) {for (int i = 1; i <= n; i++) {scanf ("%lld%lld%lld%lld", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);}int cnt = 0;for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {long long num = a[i][2]+a[j][3];sum[cnt++] = num;}}long long ans = 0;sort (sum, sum+cnt);for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {long long num = -1 * (a[i][0] + a[j][1]);ans += upper_bound (sum, sum+cnt, num) - lower_bound (sum, sum+cnt, num);}}printf ("%lld\n", ans);}return 0;}
0 0
- poj 2785 二分暴力
- POJ 2785 (二分)
- 二分二分(POJ 2785 + HDU 2289)
- poj 2785 排序+二分搜索
- poj 2785 二分或者hash
- poj 2785 二分的方法
- poj (3343)二分匹配+二分
- POJ 3233 二分二分矩阵
- poj 3685 二分套二分
- poj 3685 二分套二分
- POJ 二分算法
- POJ 3258 二分算法
- poj 2060 二分匹配
- poj 3273 (二分)
- poj 3273(二分。。。很水。。。。)
- poj 3258(二分。。。)
- poj 1693 二分图
- POJ 3122 Pie 二分
- PHP的CI框架集成Smarty的最佳方式
- Java中String创建原理深入分析
- (4) linux shell 命令 -- mkdir
- TestNG英文全称
- php 中用户触发错误与记录错误日志及异常处理
- POJ 2785 (二分)
- [JNI] C++如何调用Java代码
- poj 2711 Leapin' Lizards 拆点构图最大流
- 为了"魔兽世界默认高特效" — 时隔近6年的一次主机大升级
- 《第五项修炼》阅读笔记
- Servlet
- 流媒体平台框架开源EasyDarwin
- Paxos算法-维基百科,自由的百科全书
- js判断字符串中出现次数最多的字符 并统计出现次数