POJ_2785_4 Values whose Sum is 0(lower_bound,upper_bound)

4 Values whose Sum is 0

Time Limit: 15000MS   Memory Limit: 228000K
Total Submissions: 16970   Accepted: 4954
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).

题意:题目给定一个n*4的矩阵,然后问你在每一列中选取一个数,使得四个数之和为0的组合有多少。

分析:折半枚举,双向搜索。题目数据量比较大,暴力肯定超时。所以我们必须另寻它法。我们可以先预处理出AB[k]=A[i]+B[j],CD[k]=C[i]+D[j],然后对CD数组排序(sort(CD,CD+k)),然后枚举AB数组的元素,在CD数组上进行二分搜索就可以了。若在CD数组里存在对应的数,那么就加上这个数的个数,怎么得到这个数的个数?这里就可以巧妙地运用STL库里的两个函数了:lower_bound(),upper_bound()。关于这两个函数的详细解释在:http://blog.csdn.net/jhgkjhg_ugtdk77/article/details/46229645

题目链接:http://poj.org/problem?id=2785

代码清单:

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const int maxn = 4000 + 5;

int n;
int A[maxn],B[maxn];
int C[maxn],D[maxn];
int A_B[maxn*maxn],C_D[maxn*maxn];

void input(){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]);
    }
}

void solve(){
    int index=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){

            A_B[index]=A[i]+B[j];
            C_D[index]=C[i]+D[j];
            index++;
        }
    }

    sort(C_D,C_D+index);
    ll ans=0;
    for(int i=0;i<index;i++){
        int number=-A_B[i];
        ans+=(ll)(upper_bound(C_D,C_D+index,number)-lower_bound(C_D,C_D+index,number));
    }
    printf("%I64d\n",ans);
}

int main(){
    input();
    solve();
    return 0;
}
时间: 2024-08-08 08:13:54

POJ_2785_4 Values whose Sum is 0(lower_bound,upper_bound)的相关文章

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: 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

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

K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 1152 Appoint description: System Crawler (2015-03-12) Description The SUM problem can

POJ 2785 4 Values whose Sum is 0 [二分]

传送门 13773503 njczy2010 2785 Accepted 25248K 7079MS G++ 1423B 2015-01-11 10:26:48 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 16102   Accepted: 4659 Case Time Limit: 5000MS Description The SUM problem can be

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

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

Problem UVA1152-4 Values whose Sum is 0 Accept: 794  Submit: 10087Time 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

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