首页 > 代码库 > POJ2785 4 Values whose Sum is 0 【枚举】
POJ2785 4 Values whose Sum is 0 【枚举】
4 Values whose Sum is 0
Time Limit: 15000MS | Memory Limit: 228000K | |
Total Submissions: 16000 | Accepted: 4625 | |
Case 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 -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).
Source
Southwestern Europe 2005
/* ** Problem: POJ2785 ** Status: Accepted ** Running Time: 6766ms ** Author: Changmu ** ** 题解:将解空间分成两部分分别枚举。 */ #include <cstdio> #include <cstring> #include <algorithm> #define maxn 4010 typedef __int64 LL; using namespace std; int A[4][maxn], CD[maxn * maxn]; int main() { LL ret = 0; int i, j, n, id = 0, tmp; scanf("%d", &n); for(i = 0; i < n; ++i) { for(j = 0; j < 4; ++j) scanf("%d", &A[j][i]); } for(i = 0; i < n; ++i) for(j = 0; j < n; ++j) CD[id++] = A[2][i] + A[3][j]; sort(CD, CD + id); for(i = 0; i < n; ++i) for(j = 0; j < n; ++j) { tmp = A[0][i] + A[1][j]; ret += upper_bound(CD, CD + id, -tmp) - lower_bound(CD, CD + id, -tmp); } printf("%I64d\n", ret); }
POJ2785 4 Values whose Sum is 0 【枚举】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。