QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#49185#4598. Darnassusckiseki#AC ✓1440ms420260kbC++1.7kb2022-09-19 16:33:122022-09-19 16:33:14

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-19 16:33:14]
  • 评测
  • 测评结果:AC
  • 用时:1440ms
  • 内存:420260kb
  • [2022-09-19 16:33:12]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

struct Dsu {
    vector<int> pa, rk;
    Dsu(int n) : pa(n), rk(n) {
        iota(pa.begin(), pa.end(), 0);
    }
    int anc(int x) {
        return pa[x] == x ? x : pa[x]=anc(pa[x]);
    }
    bool join(int x, int y) {
        x = anc(x);
        y = anc(y);
        if (x == y) return false;
        if (rk[x] < rk[y]) swap(x, y);
        pa[y] = x;
        if (rk[x] == rk[y]) ++rk[x];
        return true;
    }
};

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t; cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> p(n), q(n);

        for (int i = 0; i < n; i++) {
            cin >> p[i];
            --p[i];
            q[p[i]] = i;
        }

        vector<vector<pair<int,int>>> e(n + 1);
        const int K = ceil(sqrt(n));
        for (int i = 0; i < n; i++) {
            for (int j = max(0, i-K); j <= min(n-1, i+K); j++) {
                if (j == i) continue;
                int64_t cost = 1LL * abs(i - j) * abs(p[i] - p[j]);
                if (cost <= n) {
                    e[cost].emplace_back(i, j);
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = max(0, i-K); j <= min(n-1, i+K); j++) {
                if (j == i) continue;
                int64_t cost = 1LL * abs(i - j) * abs(q[i] - q[j]);
                if (cost <= n) {
                    e[cost].emplace_back(q[i], q[j]);
                }
            }
        }

        Dsu dsu(n);
        int64_t ans = 0;
        for (int i = 1; i <= n; i++) {
            for (auto [a, b]: e[i]) {
                if (dsu.join(a, b)) {
                    ans += i;
                }
            }
        }

        cout << ans << '\n';
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1440ms
memory: 420260kb

input:

5
50000
37763 49860 5765 19637 25267 12594 48787 14155 47404 5524 39461 31259 25877 30705 2684 7916 11685 39034 33744 43465 9933 2403 35987 7114 26874 22167 9016 15866 8799 1644 12470 30017 38907 46034 9522 31054 8591 20733 49457 46805 12191 40590 14403 5658 6721 2458 15939 12811 27626 33040 47408 5...

output:

99246830
99608266
99997
49998
98832945

result:

ok 5 lines