03441: 4 Values whose Sum is 0
hash table, http://cs101.openjudge.cn/practice/03441
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 .
输入
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 .
输出
For each input file, your program has to write the number quadruplets whose sum is zero.
样例输入
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样例输出
5提示
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).
思路:利用dict实现,查找是O(1),保证不超时。用两个dict记录a和b的和的组合数,还有c和d的和的组合数,结果空间爆了。只记录a和b之和的组合数,再遍历c和d之和的时候边检验是否有a和b之和的相反数,若有就加对应的组合数。
n = int(input())
a = [0]*(n+1)
b = [0]*(n+1)
c = [0]*(n+1)
d = [0]*(n+1)
for i in range(n):
a[i],b[i],c[i],d[i] = map(int, input().split())
dict1 = {}
for i in range(n):
for j in range(n):
if not a[i]+b[j] in dict1:
dict1[a[i] + b[j]] = 0
dict1[a[i] + b[j]] += 1
ans = 0
for i in range(n):
for j in range(n):
if -(c[i]+d[j]) in dict1:
ans += dict1[-(c[i]+d[j])]
print(ans)# https://docs.python.org/3/library/array.html
import array as arr
n = int(input())
a = arr.array('i', [0]*(n+1))
b = arr.array('i', [0]*(n+1))
c = arr.array('i', [0]*(n+1))
d = arr.array('i', [0]*(n+1))
for i in range(n):
a[i],b[i],c[i],d[i] = map(int, input().split())
dict1 = {}
for i in range(n):
for j in range(n):
if not a[i]+b[j] in dict1:
dict1[a[i] + b[j]] = 0
dict1[a[i] + b[j]] += 1
ans = 0
for i in range(n):
for j in range(n):
if -(c[i]+d[j]) in dict1:
ans += dict1[-(c[i]+d[j])]
print(ans)二分查找解题思路:在这本书的89页。《算法基础与在线实践》郭炜等编著,2017年。
可以先考虑更简单的情况:给定两组整数a、b,求出和为0的二元组的个数。思路是:给定了两数之和为0,对a中每一个数a[i],判断-a[i]是否在b中。这样问题就转化为了一个查找问题,可以使用二分查找加快查找的速度。首先对输入元素进行排序,从小到大枚举每一个元素a[i],使用二分查找判断-a[i]是否在数组中,然后计算出现的次数。
回到本题,面对四组整数之和的问题,可以将问题转化为两组整数的问题。首先枚举出a、b两组所有可能的和sum1,以及c、d两组所有可能的和sum2,将这两组和看成新的数组,然后利用上述思路,使用二分查找计算满足条件的二元组个数了。
为了求出满足条件的二元组的个数,需要采用变形的二分查找,在有重复元素的数组中返回小于或等于目标的最大元素,若返回元素等于目标元素,则沿着数组计数该元素出现的次数这里采用左右都是闭区间的区间规则。若 targe ≤ mid,则 right = mid;若 targe > mid,则 left = mid + 1,两者都保证 left ≤ target ≤ right,并且在遇到重复的 target 时,right 会一直减少到第一次出现 target 的位置。循环终止条件为 left == right。
同学反馈,用Python,或者超时,或者爆内存。所以直接上C++。
C++教程,https://www.runoob.com/cplusplus/cpp-tutorial.html
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 4001;
int a[MAXN], b[MAXN], c[MAXN], d[MAXN];
int sum1[MAXN * MAXN], sum2[MAXN * MAXN];
int t = 0;
int BinarySearch(int target)
{
int num = 0;
int left = 0, right = t - 1;
while(left < right){
int mid = left + (right - left)/2;
if (target <= sum2[mid])
right = mid;
else
left = mid + 1;
}
while(sum2[left]==target && left<t){ // left<t 防止越界
num++;
left++;
}
return num;
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
t = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
sum1[t] = a[i] + b[j];
sum2[t] = c[i] + d[j];
t++;
}
sort(sum1, sum1 + t);
sort(sum2, sum2 + t);
int ans = 0;
for (int i=0; i < t; i++)
ans += BinarySearch( -sum1[i] );
cout << ans << '\n';
return 0;
}提交人 结果 内存 时间 代码长度 语言 提交时间 HFYan(GMyhf) Time Limit Exceeded 53728kB 133010ms 1918 B Python3 2020/12/18 11:15AM
# https://stackoverflow.com/questions/2272819/sort-a-part-of-a-list-in-place
# 快速排序实现及其pivot的选取
# https://blog.csdn.net/qq_31903733/article/details/82945605
import random
def quicksort(arr, start , stop):
if(start < stop):
pivotindex = partitionrand(arr, start, stop)
quicksort(arr , start , pivotindex - 1)
quicksort(arr, pivotindex + 1, stop)
def partitionrand(arr , start, stop):
randpivot = random.randrange(start, stop)
arr[start], arr[randpivot] = arr[randpivot], arr[start]
return partition(arr, start, stop)
def partition(arr,start,stop):
pivot = start # pivot
i = start + 1 # a variable to memorize where the
# partition in the array starts from.
for j in range(start + 1, stop + 1):
if arr[j] <= arr[pivot]:
arr[i] , arr[j] = arr[j] , arr[i]
i = i + 1
arr[pivot] , arr[i - 1] = arr[i - 1] , arr[pivot]
pivot = i - 1
return (pivot)
# end quicksort
def BinarySearch(target):
num = 0;
left = 0
global t
right = t - 1
while left < right:
mid = (left + right)//2;
if target <= sumr2[mid]:
right = mid
else:
left = mid + 1
while(sumr2[left]==target and left<t): # left<t 防止越界
num += 1
left += 1
return num
n = int(input())
a = [0]*(n+1)
b = [0]*(n+1)
c = [0]*(n+1)
d = [0]*(n+1)
# https://docs.python.org/3/library/array.html
import array as arr
sumr1 = arr.array('i', [])
sumr2 = arr.array('i', [])
for i in range(n):
a[i],b[i],c[i],d[i] = map(int, input().split())
t = 0;
for i in range(n):
for j in range(n):
sumr1.append( a[i] + b[j] )
sumr2.append( c[i] + d[j] )
t += 1
quicksort(sumr1, 0, t-1)
quicksort(sumr2, 0, t-1)
ans = 0
for i in range(t):
ans += BinarySearch( -sumr1[i] )
print(ans)