QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#567185#9313. Make MaxInverted_TunaWA 697ms26792kbC++202.6kb2024-09-16 09:44:122024-09-16 09:44:12

Judging History

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

  • [2024-09-18 15:56:24]
  • hack成功,自动添加数据
  • (/hack/836)
  • [2024-09-16 09:44:12]
  • 评测
  • 测评结果:WA
  • 用时:697ms
  • 内存:26792kb
  • [2024-09-16 09:44:12]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

struct DSU {
    std::vector<int> f, siz;
    
    DSU() {}
    DSU(int n) {
        init(n);
    }
    
    void init(int n) {
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
        siz.assign(n, 1);
    }   
    
    int find(int x) {
        while (x != f[x]) {
            x = f[x] = f[f[x]];
        }
        return x;
    }
    
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    
    bool merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return false;
        }
        siz[x] += siz[y];
        f[y] = x;
        return true;
    }
    
    int size(int x) {
        return siz[find(x)];
    }
};

void solve() {
    int n;
    std::cin >> n;

    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }

    std::map<int, int> L, R;
    DSU dsu(n);
    for (int i = 0, j = 0; i < n; i = j) {
        while (j < n && a[i] == a[j]) {
            j++;
        }
        for (int k = i; k < j; k++) {
            dsu.merge(i, k);
        }
    }
    for (int i = 0; i < n; i++) {
        if (i && dsu.find(i) != dsu.find(i - 1)) {
            L[dsu.find(i)] = dsu.find(i - 1);
        }
        if (i != n - 1 && dsu.find(i) != dsu.find(i + 1)) {
            R[dsu.find(i)] = dsu.find(i + 1);
        }
    }

    std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<>> pq;
    for (int i = 0; i < n; i++) {
        if (dsu.find(i) == i) {
            pq.emplace(a[i], i);
        }
    }

    i64 ans = 0;
    while (pq.size() > 1) {
        auto [x, y] = pq.top();
        pq.pop();

        if (dsu.find(y) != y) {
            continue;
        }
        // std::cerr << y + 1 << " " << dsu.size(y) << " X \n";

        if (L.count(y) && R.count(y)) {
            ans += dsu.size(y);
            if (a[L[y]] < a[R[y]]) {
                dsu.merge(L[y], y);
            } else if (a[L[y]] > a[R[y]]) {
                dsu.merge(R[y], y);
            } else {
                dsu.merge(R[y], y);
                dsu.merge(R[y], L[y]);
            }
        } else if (L.count(y)) {
            ans += dsu.size(y);
            dsu.merge(L[y], y);
        } else if (R.count(y)) {
            ans += dsu.size(y);
            dsu.merge(R[y], y);
        }
    }
    std::cout << ans << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3644kb

input:

4
2
1 2
2
2 2
7
1 1 1 2 2 2 2
3
1 2 3

output:

1
0
3
3

result:

ok 4 number(s): "1 0 3 3"

Test #2:

score: -100
Wrong Answer
time: 697ms
memory: 26792kb

input:

2
198018
875421126 585870339 471894633 383529988 625397685 944061047 704695631 105113224 459022561 760848605 980735314 847376362 980571959 329939331 644635272 326439858 752879510 837384394 175179068 182094523 397239381 1199016 185143405 279638454 252374970 822030887 860312140 137248166 993229443 164...

output:

264090
254571

result:

wrong answer 1st numbers differ - expected: '4084978', found: '264090'