POJ 2785 4 Values whose Sum is 0

文章地址:http://henuly.top/?p=668

题目:

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

Hint:

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

题目链接

先合并左边两列和和右边两列数字和,再便利其中一列数字在另一列数字中二分找到它的相反数,统计个数加入结果即可。

AC代码:

// #include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF = 0x3f3f3f3f;
const int maxn = 4e3 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x) {
    Finish_read = 0;
    x = 0;
    int f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') {
            f = -1;
        }
        if (ch == EOF) {
            return;
        }
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    x *= f;
    Finish_read = 1;
};

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int n;
    read(n);
    vector<int> a, b, c, d;
    for (int i = 0, x; i < n; ++i) {
        read(x); a.pb(x);
        read(x); b.pb(x);
        read(x); c.pb(x);
        read(x); d.pb(x);
    }
    vector<int> ab, cd;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            ab.pb(a[i] + b[j]);
            cd.pb(-(c[i] + d[j]));
        }
    }
    int ans = 0;
    sort(cd.begin(), cd.end());
    for (int i = 0; i < int(ab.size()); ++i) {
        int index = lower_bound(cd.begin(), cd.end(), ab[i]) - cd.begin();
        if (cd[index] == ab[i]) {
            int cnt = 0;
            for (int j = index; j < int(cd.size()); ++j) {
                if (cd[j] == ab[i]) {
                    cnt++;
                }
                else {
                    break;
                }
            }
            ans += cnt;
        }
    }
    printf("%d\n", ans);
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("gedit out.txt");
#endif
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Tony5t4rk/article/details/81435258
今日推荐