POJ 2785 4 Values whose Sum is 0

Source: Internet
Author: User
Tags time limit

Original question:
4 Values whose Sum is 0
Time limit:15000ms Memory limit:228000k
Total submissions:18834 accepted:5600
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-46
26-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
Effect:
Give you a number of 6 and then give you 4 arrays of a,b,c,d. Each array contains 6 numbers, and now lets you find out how to find each one in A,b,c,d and 0. The number of output scenarios.

#include <bits/stdc++.h> #include <iostream> #include <algorithm> #include <vector> using
namespace Std;
FStream in,out;
Vector<int> a,b,c,d,s;
int n;
    int main () {Ios::sync_with_stdio (false);
    int a,b,c,d;
        while (cin>>n) {a.clear ();
        B.clear ();
        C.clear ();
        D.clear ();
        S.clear ();
            for (int i=0;i<n;i++) {cin>>a>>b>>c>>d;
            A.push_back (a);
            B.push_back (b);
            C.push_back (c);
        D.push_back (d);

        } for (int i=0;i<n;i++) for (int j=0;j<n;j++) S.push_back (C[i]+d[j]);
        Sort (S.begin (), S.end ());
        Long Long ans=0;
                for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {int tmp=a[i]+b[j];
            Ans+=upper_bound (S.begin (), S.end (),-tmp)-lower_bound (S.begin (), S.end (),-tmp);
}
        }        cout<<ans<<endl;
} return 0;
 }

Answer:
In the Challenge Program design contest above see, very basic two-point search topic. C[i] and d[j] are stored in S. Then the S is sorted, enumerated a[i]+b[j], and then the binary finds the upper and lower bounds of S (A[i]+b[j])

Look at the discussion area This question also has the hash method, strives for later to fill up

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.