poj 2785 4 Values whose Sum is 0

来源:互联网 发布:i5处理器编程够用么 编辑:程序博客网 时间:2024/06/05 16:04
4 Values whose Sum is 0
Time Limit: 15000MSMemory Limit: 228000KTotal Submissions: 18683Accepted: 5544Case 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

题意:给你四个数组,从四个数组中个抽出一个数字,求四数字之和是0的方法种数
错因分析:以后调bug有什么不对的地方,请先把代码从头到尾的每个地方检察一遍
然后再开始单步调试,数组大小写错了,调了我两个多小时,还有,就是没有考虑可能有多个
与之对应的情况;

解答:upper_bound返回数组中第一个>x的下标,lower_bound返回数组中第一个>=x的下标

AC代码:

#include <iostream>
#include<cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
int a[3005], b[3005], c[3005],d[3005],cd[9000005],n, cnt;
int main()
{
while (~scanf("%d", &n))
{

for (int i = 1; i <= n; i++)
scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]);
cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cd[++cnt] = c[i] + d[j];
sort(cd + 1, cd + cnt + 1);
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
int v = -(a[i] + b[j]);
ans += upper_bound(cd + 1, cd+ cnt + 1, v) -
lower_bound(cd + 1, cd + cnt + 1, v);
}
printf("%d\n", ans);
}
return 0;
}

第一次wa的代码:
    #include<cstdio>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include<map>
    #include <algorithm>
    #include <set>
    using namespace std;
    #define MM(a) memset(a,0,sizeof(a))
    typedef long long LL;
    typedef unsigned long long ULL;
    const int mod = 1000000007;
    const double eps = 1e-10;
    const int inf = 0x3f3f3f3f;
    int a[5][3005],b[3005],c[3005],n;;
    int binary(int t)
    {
    int l=0,r=n,v=-t;
    while(r-l>1)
    {
    int mid=(l+r)>>1;
    if(c[mid]>v)
    r=mid;
    else l=mid;
    }
    return v==c[r];
    }
    int main()
    {
    while(~scanf("%d",&n))
    {
    for(int i=1;i<=n;i++)
    for(int k=1;k<=4;k++)
    scanf("%d",&a[k][i]);
    for(int i=1;i<=n;i++)
    {
    b[i]=a[1][i]+a[2][i];
    c[i]=a[3][i]+a[4][i];
    }
    sort(c,c+n+1);
    int ans=0;
    for(int i=1;i<=n;i++)
    if(binary(b[i]))
    ans++;
    printf("%d\n",ans);
    }
    return 0;
    }


原创粉丝点击