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

4 Values whose Sum is 0

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).

这道题呢 是一个 折半枚举 的 模版题 ,先简单说一下 题目的 意

思:就是 给你 四个 长度 长度 都 为 n 的 四个 数列, 然后分别从

四个 数列 中 选出 一个 数 然后加起来 看看 是否等于零, 你所要就

得 就是 等于零的 情况 有多少种。 这道题 如果 直接 遍历 一边的

话 时间 复杂度为 n的 4 次方 显然 是 会超时的,如果 你用啦 折

半枚举 时间 复杂度 变为 n 的 2 次方 这样 就笑了很多。

这道题 还用到啦 两个 函数 upper_bound 和 lower_bound 下

upper_bound(a, a+n, val)(a 数组 是 非递减的 数组) 是返回

第一个 大于 val 的 数的 下标(还可以理解为 数组 新添加 一个

val 的 下标), lower_bound(a, a+n, val) 是返回 第一个 大于

等于 val 的 数的 下标。博 客 最下面 会 有 上面 两个 函数的

代码 和 简单的讲解 ,感兴趣的朋友可以看一下!

#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>

#define MAX 4010


using namespace std;

int a[MAX];
int b[MAX];
int c[MAX];
int d[MAX];

int main()
{
    int n;

    while(~scanf("%d", &n))
    {
        int ans = 0;
        static int half_sum[MAX*MAX];

        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++)
                half_sum[i*n+j] = a[i] + b[j];
        sort(half_sum, half_sum+n*n);

        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                ans += upper_bound(half_sum, half_sum+n*n, -(c[i]+d[j])) - lower_bound(half_sum, half_sum+n*n, -(c[i]+d[j]));
        printf("%d\n", ans);
    }

    return 0;
}

 STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法。

  ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置。

 ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中第一个大于val的位置。

 lower_bound和upper_bound如下图所示:

这里写图片描述

1, lower_bound

  这个序列中可能会有很多重复的元素,也可能所有的元素都相同,为了充分考虑这种边界条件,STL中的lower_bound算法总体上是才用了二分查找的方法,但是由于是查找序列中的第一个出现的值大于等于val的位置,所以算法要在二分查找的基础上做一些细微的改动。

 首先是我修改数据结构课本上的二分查找实现的lower_bound算法:
//这个算法中,first是最终要返回的位置
int lower_bound(int *array, int size, int key)
{
    int first = 0, middle;
    int half, len;
    len = size;

    while(len > 0) {
        half = len >> 1;
        middle = first + half;
        if(array[middle] < key) {     
            first = middle + 1;          
            len = len-half-1;       //在右边子序列中查找
        }
        else
            len = half;            //在左边子序列(包含middle)中查找
    }
    return first;
}

2, upper_bound

upper_bound返回的是最后一个大于等于val的位置,也是有一个新元素val进来时的插入位置。

int upper_bound(int *array, int size, int key)
{
    int first = 0, len = size-1;
    int half, middle;

    while(len > 0){
        half = len >> 1;
        middle = first + half;
        if(array[middle] > key)     //中位数大于key,在包含last的左半边序列中查找。
            len = half;
        else{
            first = middle + 1;    //中位数小于等于key,在右半边序列中查找。
            len = len - half - 1;
        }
    }
    return first;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 32
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值