Poj_2785_4 Values whose Sum is 0 (lower_bound,upper_bound)

Source: Internet
Author: User

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 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).


Test instructions: Topic given a matrix of n*4, and then asked you to select a number in each column, so that the sum of four number of 0 of the combination of how many.

Analysis: Binary enumeration, bidirectional search. The amount of data on the topic is larger, and violence definitely expires. So we have to find another way to do it. We can preprocess the ab[k]=a[i]+b[j],cd[k]=c[i]+d[j], then sort the CD Array (sort (cd,cd+k)), then enumerate the elements of the AB array, and search on the CD array for two points. If there is a corresponding number in the CD array, then add the number, how to get the number? Here you can skillfully apply the two functions in the STL Library: Lower_bound (), Upper_bound (). A detailed explanation of these two functions is in: http://blog.csdn.net/jhgkjhg_ugtdk77/article/details/46229645

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

Code Listing:

#include <set> #include <map> #include <cmath> #include <queue> #include <stack> #include <ctime> #include <cctype> #include <string> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;typedef long Long ll;typedef unsigned long long ull;typedef unsigned int uint;const int MAXN = 4000 + 5;int n;int a[maxn],b[maxn];int c[maxn],d[maxn];i    NT A_b[maxn*maxn],c_d[maxn*maxn];void input () {scanf ("%d", &n);    for (int i=0;i<n;i++) {scanf ("%d%d%d%d", &a[i],&b[i],&c[i],&d[i]);    }}void solve () {int index=0;            for (int. i=0;i<n;i++) {for (int j=0;j<n;j++) {a_b[index]=a[i]+b[j];            C_D[INDEX]=C[I]+D[J];        index++;    }} sort (C_d,c_d+index);    ll Ans=0;        for (int i=0;i<index;i++) {int number=-a_b[i];    ans+= (LL) (Upper_bound (C_d,c_d+index,number)-lower_bound (C_d,c_d+index,number)); }    printf ("%i64d\n", ans);}    int main () {input ();    Solve (); return 0;}


Poj_2785_4 Values whose Sum is 0 (lower_bound,upper_bound)

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.