题解:P14262 [ROI 2015 Day1] 自动好友

· · 题解

P14262 题解

思路

题目首先要我们我们要找所有用户对 (i,j),满足:

方法 1,暴力

一开始我直接想到了暴力,首先直接传统双重循环枚举所有 i<j,然后检查三个条件 a_i=a_j, b_i=b_j, c_i=c_j 有几个成立,如果恰好 1 个成立,就计数 +1,但是双循的复杂度是 O(n^2),n\le 10^5,所以暴力会超时。

方法 2,容斥原理

接着我就手欠点开了算法标签,排列组合,容斥原理,排列组合我知道,但是容斥原理是什么就没学过了,然后我上网查了一下,弄懂了。

开始用容斥原理来写这题。

设:

但我们很难直接算"只有 a 相等",所以用容斥:

总对数 =(a \text{相等的对数})+(b\text{相等的对数})+(c\text{相等的对数})- 2\times(ab\text{都相等的对数})- 2\times(ac\text{都相等的对数}) - 2\times(bc\text{都相等的对数}) + 3\times(abc\text{都相等的对数})。

所以我用代码表示一下上面的公式:

ans = A + B + C - 2*(AB + AC + BC) + 3*ABC

就上面这东西,耗我半小时。

时间复杂度 O(n)。

代码

#include <iostream>
using namespace std;
long long ca[101], cb[101], cc[101], ab[101][101], ac[101][101], bc[101][101], abc[101][101][101];
long long A, B, C, AB, AC, BC, ABC;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    int a[100005], b[100005], c[100005];
    for (int i; i < n; i++) cin >> a[i] >> b[i] >> c[i];
    for (int i; i < n; i++) {
        ca[a[i]]++;
        cb[b[i]]++;
        cc[c[i]]++;
    }
    for (int i; i < n; i++) {
        ab[a[i]][b[i]]++;
        ac[a[i]][c[i]]++;
        bc[b[i]][c[i]]++;
    }
    for (int i; i < n; i++) abc[a[i]][b[i]][c[i]]++;
    for (int i = 1; i <= 100; i++) {
        if (ca[i] > 1) A += ca[i] * (ca[i] - 1) / 2;
        if (cb[i] > 1) B += cb[i] * (cb[i] - 1) / 2;
        if (cc[i] > 1) C += cc[i] * (cc[i] - 1) / 2;
    }
    for (int i = 1; i <= 100; i++) {
        for (int j = 1; j <= 100; j++) {
            if (ab[i][j] > 1) AB += ab[i][j] * (ab[i][j] - 1) / 2;
            if (ac[i][j] > 1) AC += ac[i][j] * (ac[i][j] - 1) / 2;
            if (bc[i][j] > 1) BC += bc[i][j] * (bc[i][j] - 1) / 2;
        }
    }
    for (int i = 1; i <= 100; i++) {
        for (int j = 1; j <= 100; j++) {
            for (int k = 1; k <= 100; k++) {
                if (abc[i][j][k] > 1) {
                    ABC += abc[i][j][k] * (abc[i][j][k] - 1) / 2;
                }
            }
        }
    }
    long long ans = A + B + C - 2 * (AB + AC + BC) + 3 * ABC;
    cout << ans << "\n";
    return 0;
}

方法 3,分类计数

观察我发现对于每个用户,只考虑三种情况:

然后用哈希表记录每种 (a,b,c) 组合的出现次数,然后计算。

接着。

统计每个 4 值的出现次数,计算 C(k,2) 求和得到 A 再同样方法得到 B,C。

统计每个 (a,b) 组合的出现次数,计算 C(k,2) 求和得到 AB 同样方法得到 AC,BC。

统计每个 (a,b,c) 组合的出现次数,计算 C(k,2) 求和得到 ABC。

代入容斥公式计算答案就好了。

代码:

#include <iostream>
using namespace std;
long long ans, cnt[101][101][101];
int a[100005], b[100005], c[100005];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i] >> c[i];
    for (int i = 0; i < n; i++) cnt[a[i]][b[i]][c[i]]++;
    for (int i = 0; i < n; i++) {
        int x = a[i];
        for (int y = 1; y <= 100; y++) {
            if (y == b[i]) continue;
            for (int z = 1; z <= 100; z++) {
                if (z == c[i]) continue;
                ans += cnt[x][y][z];
            }
        }
    }
    for (int i = 0; i < n; i++) {
        int y = b[i];
        for (int x = 1; x <= 100; x++) {
            if (x == a[i]) continue;
            for (int z = 1; z <= 100; z++) {
                if (z == c[i]) continue;
                ans += cnt[x][y][z];
            }
        }
    }
    for (int i = 0; i < n; i++) {
        int z = c[i];
        for (int x = 1; x <= 100; x++) {
            if (x == a[i]) continue;
            for (int y = 1; y <= 100; y++) {
                if (y == b[i]) continue;
                ans += cnt[x][y][z];
            }
        }
    }
    cout << ans / 2 << "\n";
    return 0;
}

交上去一看 TLE 了两个点,算了一下时间 O(n \times 100 \times 100) = 100000 \times 10000 = 10^9(可能超时)。

最耗时间的是 for 循环,减少循环嵌套:从三重循环降到二重循环。我们就可以减少大量时间。

改进后的代码,我重写了一遍:

#include <iostream>
using namespace std;
long long ans, cnt[101][101][101];
int a[100005], b[100005], c[100005];
long long sum_a[101][101], sum_b[101][101], sum_c[101][101];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i] >> c[i];
    for (int i = 0; i < n; i++) cnt[a[i]][b[i]][c[i]]++;
    for (int x = 1; x <= 100; x++) {
        for (int y = 1; y <= 100; y++) {
            for (int z = 1; z <= 100; z++) {
                sum_a[x][y] += cnt[x][y][z];
                sum_b[y][z] += cnt[x][y][z];
                sum_c[x][z] += cnt[x][y][z];
            }
        }
    }
    for (int i = 0; i < n; i++) {
        int x = a[i], y = b[i], z = c[i];
        long long tot_a = 0;
        for (int y2 = 1; y2 <= 100; y2++) tot_a += sum_a[x][y2];
        ans += tot_a - sum_a[x][y] - sum_c[x][z] + cnt[x][y][z];
        long long tot_b = 0;
        for (int z2 = 1; z2 <= 100; z2++) tot_b += sum_b[y][z2];
        ans += tot_b - sum_a[x][y] - sum_b[y][z] + cnt[x][y][z];
        long long tot_c = 0;
        for (int x2 = 1; x2 <= 100; x2++) tot_c += sum_c[x2][z];
        ans += tot_c - sum_c[x][z] - sum_b[y][z] + cnt[x][y][z];
    }
    cout << ans / 2 << "\n";
    return 0;
}

后记

求管理员大人通过。