UVA1152-4 Values whose Sum is 0(分块)

Problem UVA1152-4 Values whose Sum is 0

Accept: 794  Submit: 10087
Time Limit: 9000 mSec

Problem 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×B×C×D are such that a+b+c+d = 0. In the following, we assume that all lists have the same size n.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The ?rst line of the input ?le 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 test case, your program has to write the number quadruplets whose sum is zero. The outputs of two consecutive cases will be separated by a blank line.

 Sample Input

1
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

题解:这个题主要是太陈了,觉得是个大水题,但是第一次见的时候不是太容易想。思想很深刻,分块,明明都是暴力枚举,但即便不加二分查找这个方法也在数量级上碾压四重for循环,感觉上有一点不可思议,想想莫队算法是不是也利用了这个思想(分块真的可以出奇迹)。

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4
 5 const int maxn = 4000 + 10;
 6
 7 int a[maxn], b[maxn], c[maxn], d[maxn];
 8 int sum[maxn*maxn];
 9 int n;
10
11 int main()
12 {
13     //freopen("input.txt", "r", stdin);
14     int iCase;
15     scanf("%d", &iCase);
16     while (iCase--) {
17         scanf("%d", &n);
18         for (int i = 0; i < n; i++) {
19             scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
20         }
21
22         int cnt = 0;
23         for (int i = 0; i < n; i++) {
24             for (int j = 0; j < n; j++) {
25                 sum[cnt++] = a[i] + b[j];
26             }
27         }
28         sort(sum, sum + cnt);
29         long long ans = 0;
30         for (int i = 0; i < n; i++) {
31             for (int j = 0; j < n; j++) {
32                 ans += upper_bound(sum, sum + cnt, -c[i] - d[j]) - lower_bound(sum, sum + cnt, -c[i] - d[j]);
33             }
34         }
35
36         printf("%lld\n", ans);
37         if (iCase) printf("\n");
38     }
39     return 0;
40 }

原文地址:https://www.cnblogs.com/npugen/p/9611070.html

时间: 2024-10-09 12:19:37

UVA1152-4 Values whose Sum is 0(分块)的相关文章

uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)

用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度.可是我不会写...看了紫书作者刘汝佳老师的代码,真是太美了!简单明了,就像看吕钦下的棋一样.我就模仿的写了一下: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set>

uva1152 - 4 Values whose Sum is 0(hash或STL技巧ac)

题目大意:给定4个n(1 <= n <= 4000)元素集合A, B, C, D,要求分别从中选取一个元素a, b, c, d,使得a+b+c+d = 0,问有多少种选法. method 1: 这里用到一个很实用的技巧: 求长度为n的有序数组a中的数k的个数num? num=upper_bound(a,a+n,k)-lower_bound(a,a+n,k); #include <iostream> #include <cstring> #include <algo

UVa1152 4 Values whose Sum is 0 (中途相遇法)

链接:http://vjudge.net/problem/36014 分析:先枚举a和b,把所有a+b记录下来放在一个有序数组中,然后枚举c和d,查一查-c-d有多少种方法写成a+b的形式(二分查找). 1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int maxn = 4000 + 5; 6 7 int a[maxn], b[maxn], c[maxn], d[maxn];

UVa1152 - 4 Values whose Sum is 0(hash)

hash结构体 struct Hash_map { static const int mask=0x7fffff; int p[8388608],q[8388608]; void clear(){ for(int i=0;i<=mask;++i) q[i]=0; } int & operator [] (int k){ int i; for(i=k&mask; q[i]&&p[i]!=k; i=(i+1)&mask); p[i]=k; return q[i];

POJ 2785 4 Values whose Sum is 0(折半枚举)

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 17088   Accepted: 4998 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

POJ 2785 4 Values whose Sum is 0

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 22691   Accepted: 6869 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

4 Values whose Sum is 0(二分)

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 21370   Accepted: 6428 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

[poj2785]4 Values whose Sum is 0(hash或二分)

4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: 5778 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 man

POJ 2785 4 Values whose Sum is 0 (对半分解 二分搜索)

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 17658   Accepted: 5187 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