记于2020-3-19

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 × B × C × D are such that a + b + c + d = 0. In the following, we
assume that all lists have the same size n.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases
following, each of them as described below. This line is followed by a blank line, and there is also a
blank line between two consecutive inputs.
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 test case, your program has to write the number quadruplets whose sum is zero.
The outputs of two consecutive cases will be separated by a blank line.

Sample Input

1
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

Sample Explanation:

Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30),
(26, 30, -10, -46), (-32, 22, 56, -46), (-32, 30, -75, 77), (-32, -54, 56, 30).

这道题让我心态崩裂,也怪于我的知识不扎实(真正的原因居然是我想错map的时间复杂度),我想不做英雄,我只想做我喜欢的事。

思路:看代码直接就能看懂吧。用的map,运行有点慢,在超时的边缘。

还有一种优化方式是将后两组的组成结果放到数组里, 然后二分寻找答案(快了map3倍左右)(见代码二)。

#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 5010;

int a[N], b[N], c[N], d[N];


int main()
{
    int n; cin >> n;
    
    while(n --)
    {
        int len; cin >> len;
        
        for(int i = 0; i < len; i ++) scanf("%d %d %d %d", a + i, b + i, c + i, d + i);
        
        unordered_map<LL, int> S;
        
        for(int i = 0; i < len ; i ++)
        {
            for(int j = 0; j < len; j ++)
            {
                S[c[i] + d[j]] ++;;
            }
        }
        LL sum = 0;
        
        for(int i = 0; i < len; i ++)
        {
            for(int j = 0; j < len; j ++)
            {
                sum += S[-(a[i] + b[j])];
            }
        }
        S.clear();
        
        if(n == 0) printf("%d\n", sum);
        else printf("%d\n\n", sum);
    }
    return 0;
}

#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 4010, M = 4010 * 4010;

int a[N], b[N], c[N], d[N], t[M], idx;

int main()
{
    int m; cin >> m;
    
    while(m --)
    {
        int n; cin >> n;
        for(int i = 0; i < n ; i ++) scanf("%d %d %d %d", a + i, b + i, c + i, d + i);
        idx = 0;
        for(int i = 0; i < n ; i ++)
        {
            for(int j = 0; j < n ; j ++)
            {
                t[idx ++] = c[i] + d[j];
            }
        }
        
        sort(t, t + idx);
        
        LL sum = 0;
        for(int i = 0; i < n; i ++)
        {
            for(int j = 0; j < n; j ++)
            {
                int star = lower_bound(t, t + idx, -(a[i] + b[j])) - t;
                
                int end = upper_bound(t, t + idx, -(a[i] + b[j])) - t;
                
                
                sum += (end - star);
                
                //cout << sum << endl;
            }
        }
        if(m)cout << sum << '\n' << '\n';
        
        else cout << sum << '\n';
        
    }
    return 0;
    
}


发布了53 篇原创文章 · 获赞 14 · 访问量 1879

猜你喜欢

转载自blog.csdn.net/weixin_45630535/article/details/104966838