POJ 2785(4 Values whose Sum is 0)

【题意描述】

对于给定的四个序列,从每个序列中选出一个数,并让四个数相加,输出所有相加和为0的情况数目。

【解题思路】

我们可以考虑前两列的数字相加之和一定与后两列相加和互为相反数,那么我们可以枚举出前两列数字之和,并且,枚举出后两列数据之和的相反数,并对之排序,然后利用二分法进行查找即可。

【AC代码】

#include<iostream>
#include<algorithm>
using namespace std;
int n,ans,a[4040],b[4040],c[4040],d[4040],ab[4040*4040],cd[4040*4040];
int main()
{
    int n;
        while(cin>>n)
        {
            for(int i=0;i<n;i++)
                cin>>a[i]>>b[i]>>c[i]>>d[i];
                int pab=0,pcd=0;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    ab[pab++]=a[i]+b[j];
                    cd[pcd++]=-(c[i]+d[j]);
                }
            }
            sort(cd,cd+pcd);
            for(int i=0;i<pab;i++)
            {
               int mid,low=0,up=pcd-1;
               while(low<=up)
               {
                   mid=(low+up)/2;
                   if(cd[mid]==ab[i])
                    {
                        ans++;
                        for(int j=mid+1;j<pcd;j++)
                        {
                            if(cd[j]==ab[i]) ans++;
                            else break;
                        }
                        for(int j=mid-1;j>=0;j--)
                        {
                            if(cd[j]==ab[i]) ans++;
                            else break;
                        }
                        break;
                    }
                    else
                    {
                       if(cd[mid]>ab[i]) up=mid-1;
                       else low=mid+1;
                    }

               }
            }
            cout<<ans<<endl;
        }
        return 0;
}

POJ 2785(4 Values whose Sum is 0)

时间: 2024-08-29 12:10:12

POJ 2785(4 Values whose Sum is 0)的相关文章

POJ - 2785 :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 .

poj 2785 4 Values whose Sum is 0(sort+二分)

题意: 给你ABCD四个集合,集合中数的个数都为N(N<=4000),如果分别在ABCD四个集合中取一个数,a b c d ,求有多少种可能使得a+b+c+d=0. 当然你可以尝试枚举所有的组合,绝对可以计算出结果,大概有N^4种吧,如果你有足够的时间还是可以算出来的,哈哈. 当然我不是用上面一种方法计算的,那样算肯定超时. 我的做法是求出所有a+b 到ab数组中, 和所有 c+d到cd数组中,然后排序,枚举每个ab,用二分在cd中查找有没有可能组成0.  有个问题就是二分只能返回一个结果,所以

poj 2785 4 Values whose Sum is 0 (简单二分)

//每列选一个数相加为0的个数 # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int ab[4010*4010],cd[4010*4010]; int main() { int n,i,k,j,count,a[4010],b[4010],c[4010],d[4010]; while(~scanf("%d",&n)) { f

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

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

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

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