QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#567361#9313. Make MaxbigJWA 215ms29128kbC++203.9kb2024-09-16 11:22:252024-09-16 11:22:25

Judging History

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

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

answer

#include <bits/stdc++.h>

namespace stdv = std::views;
namespace stdr = std::ranges;

using i64 = long long;

template<typename A, typename B>
constexpr bool chmax(A& a, const B& b) {
    a = (a > b) ? a : b;
    return a == b;
}
template<typename A, typename B>
constexpr bool chmin(A& a, const B& b) {
    a = (a < b) ? a : b;
    return a == b;
}
struct DSU {
    int n;
    std::vector<int> f, siz;

    DSU() :n(0) {}
    explicit DSU(int n) :n(n) {
        init(n);
    }

    void init(int n) {
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
        siz.assign(n, 1);
    }

    constexpr int find(int x) {
        while (x != f[x]) {
            x = f[x] = f[f[x]];
        }
        return x;
    }

    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;
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }

    int size(int x) {
        return siz[find(x)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> p(n), psize(n);
        for (int i = 0; i < n; i++) {
            p[i] = find(i);
            psize[p[i]]++;
        }
        std::vector<std::vector<int>> ans(n);
        for (int i = 0; i < n; i++) {
            ans[i].reserve(psize[i]);
        }
        for (int i = 0; i < n; i++) {
            ans[p[i]].push_back(i);
        }
        ans.erase(std::remove_if(ans.begin(), ans.end(), [](const std::vector<int>& v) {
            return v.empty();
        }), ans.end());
        return ans;
    }

    constexpr int operator[](int i) {
        return find(i);
    }
};

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

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

    auto v = a;
    stdr::sort(v);
    v.erase(std::unique(v.begin(), v.end()), v.end());
    for (int i = 0; i < n; i++) {
        a[i] = std::lower_bound(v.begin(), v.end(), a[i]) - v.begin();
    }

    std::vector<std::set<int>> s(n);

    DSU dsu(n);
    for (int i = 0; i + 1 < n; i++) {
        if (a[i] == a[i + 1]) {
            dsu.merge(i + 1, i);
        }
    }

    for (int i = 0; i < n; i = dsu[i] + 1) {
        s[a[i]].insert(i);
    }

    i64 ans = 0;
    for (int _ = 0; _ < n; _++) {
        for (int j : s[_]) {
            int r = dsu[j];
            if (j - 1 < 0 && r + 1 >= n) {
                continue;
            }
            ans += r - j + 1;
            if (j - 1 < 0) {
                dsu.merge(r + 1, j);
                s[a[r + 1]].extract(r + 1);
                s[a[r + 1]].insert(j);
                chmax(a[j], a[r + 1]);
                chmax(a[r], a[r + 1]);
            } else if (r + 1 >= n) {
                dsu.merge(j, j - 1);
                chmax(a[j], a[j - 1]);
                chmax(a[r], a[j - 1]);
            } else {
                int min = std::min(a[dsu[j - 1]], a[dsu[r + 1]]);
                if (a[dsu[r + 1]] == min) {
                    dsu.merge(r + 1, j);
                    s[a[r + 1]].extract(r);
                    if (a[r + 1] != a[dsu[j - 1]]) {
                       s[a[r + 1]].insert(j);
                    }
                    chmax(a[j], a[r + 1]);
                    chmax(a[r], a[r + 1]);
                }
                if (a[dsu[j - 1]] == min) {
                    dsu.merge(j, j - 1);
                    chmax(a[j], a[j - 1]);
                    chmax(a[r], a[j - 1]);
                }
            }
        }
    }

    std::cout << ans << '\n';
}

int main(int argc, const char* argv[]) {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;
    
    while (t--) {
        Find();
    }
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 215ms
memory: 29128kb

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:

6180497
6194584

result:

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