QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#567337#9313. Make MaxbigJWA 228ms29028kbC++203.7kb2024-09-16 11:14:072024-09-16 11:14:07

Judging History

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

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

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) {
        int px = find(x);
        int py = find(y);
        if (px == py) {
            return false;
        }
        siz[x] += siz[py];
        f[py] = 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(dsu[r + 1], j);
                s[a[dsu[r + 1]]].extract(r + 1);
                s[a[dsu[r + 1]]].insert(j);
            } else if (r + 1 >= n) {
                dsu.merge(dsu[j], j - 1);
            } else {
                int min = std::min(a[dsu[j - 1]], a[dsu[r + 1]]);
                if (a[dsu[r + 1]] == min) {
                    dsu.merge(dsu[r + 1], j);
                    s[a[dsu[r + 1]]].extract(r);
                    if (a[dsu[r + 1]] != a[dsu[j - 1]]) {
                       s[a[dsu[r + 1]]].insert(j);
                    }
                }
                if (a[dsu[j - 1]] == min) {
                    dsu.merge(dsu[j], j - 1);
                }
            }
            chmax(a[j], a[dsu[j]]);
            chmax(a[r], a[dsu[j]]);
        }
    }

    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;
}

详细

Test #1:

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

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: 228ms
memory: 29028kb

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:

938349
894606

result:

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