SWERC20054 values whose sum is 0

Posted rign

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SWERC20054 values whose sum is 0相关的知识,希望对你有一定的参考价值。

将前两组求和n^2sort,后两个组枚举二分(upper-lower)每个数字的相反数,求和

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<utility>
#include<queue>
#include<vector>
#include<cctype>
using namespace std;
#define fore(i,a,b) for(int i=a;i<=b;i++)
#define forb(i,a,b) for(int i=a;i>=b;i--)
#define fir first
#define sec second
#define ABS(a) ((a)>0?(a):-(a))
#define PI acos(-1,0)
typedef long long ll;
typedef long double ld;
const int MAXN=4005;
int nums[MAXN*4];
int sorn[MAXN*MAXN];
int tot;

int main(){
    int n;
    cin>>n;
    fore(i,1,4*n){
        scanf("%d",&nums[i]);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            sorn[++tot]=nums[4*i-3]+nums[4*j-2];
        }
    }
    sort(sorn+1,sorn+tot+1);
    //cout<<tot;
    ll ans=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            ans+=upper_bound(sorn+1,sorn+tot+1,-(nums[i*4-1]+nums[j*4]))-lower_bound(sorn+1,sorn+tot+1,-(nums[i*4-1]+nums[j*4]));
        }
    }
    printf("%lld",ans);
    return 0;
}

以上是关于SWERC20054 values whose sum is 0的主要内容,如果未能解决你的问题,请参考以下文章

4 Values whose Sum is 0 二分

4 Values whose Sum is 0(二分)

4 Values whose Sum is 0 (二分+排序)

poj 2785 4 Values whose Sum is 0

4 Values whose Sum is 0 :POJ - 2785

poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))