折半枚举(双向搜索)poj27854 Values whose Sum is 0

来源:互联网 发布:java内嵌机制 编辑:程序博客网 时间:2024/06/08 07:50

4 Values whose Sum is 0
Time Limit: 15000MS Memory Limit: 228000KTotal Submissions: 23757 Accepted: 7192Case 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

[Submit]   [Go Back]   [Status]   [Discuss]


有时候问题的规模比较大,无法枚举所有元素的组合,但能够枚举一般元素的组合。此时,将问题拆成两半后分别枚举,再合并他们的结果这一方法往往非常有效。


//折半枚举(双向搜索)poj2785#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;typedef long long ll;const int maxn=5005;int n;ll a[maxn],b[maxn],c[maxn],d[maxn];ll cd[maxn*maxn];void solve(){    //枚举cd的组合    for(int i=0;i<n;i++)    {        for(int j=0;j<n;j++)        {            cd[i*n+j]=c[i]+d[j];        }    }    sort(cd,cd+n*n);    ll res=0;    for(int i=0;i<n;i++)    {        for(int j=0;j<n;j++)        {            ll CD=-(a[i]+b[j]);            //二分搜索取出cd中和为CD的部分            res+=upper_bound(cd,cd+n*n,CD)-lower_bound(cd,cd+n*n,CD);        }    }    printf("%lld\n",res);}int main(){    cin>>n;    for(int j=0;j<n;j++)    {        cin>>a[j]>>b[j]>>c[j]>>d[j];    }    solve();    return 0;}


阅读全文
0 0