POJ 2785 4 Values whose Sum is 0

Source: Internet
Author: User

4 Values whose Sum is 0
Time Limit: 15000MS Memory Limit: 228000K
Total Submissions: 20015 Accepted: 5974
Case Time Limit: 5000MS

Description

The SUM problem can formulated as Follows:given four lists A, B, C, D of an integer values, compute how many quadruplet ( A, B, C, D) ∈a x B x C x D is such that A + B + c + d = 0. In the following, we assume this all lists has 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 had 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 of the input file, your program have 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), (-3) 2, 22, 56,-46), (-32, 30,-75, 77), (-32,-54, 56, 30).

Source

Southwestern Europe 2005    parsing: Binary enumeration. directly from the 4 series selected words have N4, time complexity is too high, not advisable. Binary enumeration can be taken, a, B, C, D into AB and CD considerations, from A, b after a, b, in order to make the sum of 0 need to remove c+d =-(a+b) from C, D. So first the C, D take the number of N2 method enumeration out, side-by good order, so you can use binary search, time complexity of O (N2LOGN2).    
#include <cstdio> #include <algorithm> #include <vector> #define LL long longusing namespace Std;const int MAXN = 4000+5;int A[MAXN], B[MAXN], C[MAXN], D[maxn];int cd[maxn*maxn];int n;pair<vector<int>::iterator, Vector<int>::iterator> it;void Solve () {for    (int i = 0, i < n; ++i) {for        (int j = 0; J < N; ++j) {
   
    CD[I*N+J] = C[i]+d[j];        }    }    Sort (CD, cd+n*n);    ll res = 0;    for (int i = 0, i < n; ++i) {for        (int j = 0; J < N; ++j) {            int c_d =-(A[i]+b[j]);            it = Equal_range (CD, Cd+n*n, c_d);            Res + = It.second-it.first;        }    }    printf ("%i64d\n", res);} int main () {    scanf ("%d", &n);    for (int i = 0; i < n; ++i)        scanf ("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);    Solve ();    return 0;}
   

  

  

POJ 2785 4 Values whose Sum is 0

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.