4 Values whose Sum is 0

Source: Internet
Author: User

This question is the same as the two-point question. It only divides numbers into two heaps and uses the binary sorting method. However, it must be understood that it is not a condition to be found, there may be only duplicates. Here is the focus. As I continue my second journey, my time is very complex and the efficiency is not very good. After reading other people's code, it seems that all of them use hash, And the next stop is hash.

4 Values whose Sum is 0

Time Limit: 15000 MS Memory Limit: 228000 K
Total Submissions: 11586 Accepted: 3249
Case Time Limit: 5000 MS

Description


The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, B, c, d) ε A x B x C x D are such that a + B + c + d = 0. in the following, we assume that all lists have 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 have 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 input file, your program has 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
 
Code:
[Cpp]
<Span style = "font-family: FangSong_GB2312; font-size: 18px;" >#include <iostream>
# Include <cmath>
# Include <vector>
# Include <algorithm>
# Define maxn4004
Using namespace std;
Int map1 [maxn * maxn];
Int map2 [maxn * maxn];
Int a [maxn], B [maxn], c [maxn], d [maxn];
Int main ()
{
Int n, I, j, k, sum, p;
Scanf ("% d", & n );
For (I = 0; I <n; I ++)
{
Scanf ("% d", & a [I], & B [I], & c [I], & d [I]);
}
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)
Map1 [I * n + j] = a [I] + B [j];
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)
Map2 [I * n + j] = c [I] + d [j];
Sort (map1, map1 + n * n );
Sort (map2, map2 + n * n );
Sum = 0;
P = n * n-1;
For (I = 0; I <n * n; I ++)
{
While (p> = 0 & map1 [I] + map2 [p]> 0) p --;
If (p <0) break;
Int temp = p;
While (temp> = 0 & map1 [I] + map2 [temp] = 0)
{
Sum ++; temp --;
}
}
Printf ("% d \ n", sum );
// System ("pause ");
Return 0;
}

</Span>

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.