POJ2785 4 Values whose Sum is 0 (two minutes)

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=2785


Test instructions

Given your four-size set of N and then how many four-tuple and 0, respectively, from these four sets;


Analysis:

Divided into two groups, get sum1[[,sum2[], and then order, in the sum2 binary search-sum1[i] write down the number can be


The code is as follows:

#include <iostream> #include <cstring> #include <cstdio> #include <algorithm>using namespace std;const int maxn = 4010;int a[4][maxn];int sum1[maxn*maxn],sum2[maxn*maxn];int main () {    int n;    while (~SCANF ("%d", &n)) {for        (int. i=0;i<n;i++) {for            (int j=0;j<4;j++)                scanf ("%d", &a[j][i] );        }        int cnt1=0,cnt2=0;        for (int i=0;i<n;i++) {for            (int j=0;j<n;j++)                sum1[cnt1++]=a[0][i]+a[1][j];        }        for (int i=0;i<n;i++) {for            (int j=0;j<n;j++)                sum2[cnt2++]=a[2][i]+a[3][j];        }        Sort (sum1,sum1+cnt1);        Sort (sum2,sum2+cnt2);        A long long ans = 0;        for (int i=0;i<cnt1;i++)            ans+=upper_bound (Sum2,sum2+cnt2,0-sum1[i])-lower_bound (Sum2,sum2+cnt2,0-sum1[i] );        printf ("%lld\n", ans);    }    return 0;}


POJ2785 4 Values whose Sum is 0 (two minutes)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.