QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#485224#7685. Barkley IIyaoyWA 60ms3620kbC++172.4kb2024-07-20 15:10:532024-07-20 15:10:53

Judging History

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

  • [2024-07-20 15:10:53]
  • 评测
  • 测评结果:WA
  • 用时:60ms
  • 内存:3620kb
  • [2024-07-20 15:10:53]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

const int N = 5E5 + 1;
int cnt[N];

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

    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    auto b = a;
    std::sort(b.begin(), b.end());
    b.erase(std::unique(b.begin(), b.end()), b.end());

    const int M = b.size();
    int mex = -1;
    for (int i = 0; i < M; i++) {
        if (b[i] != i + 1) {
            mex = i + 1;
            break;
        }
    }
    if (mex == -1) {
        mex = M + 1;
    }

    std::vector<std::vector<int>> pos(mex + 1, {-1});
    for (int i = 0; i < n; i++) {
        if (a[i] < mex) {
            pos[a[i]].emplace_back(i);
        }
    }
    for (int i = 1; i <= mex; i++) {
        pos[i].emplace_back(n);
    }

    std::vector<std::array<int, 3>> qry;
    for (int i = 1; i <= mex; i++) {
        for (int j = 0; j + 1 < pos[i].size(); j++) {
            int l = pos[i][j] + 1, r = pos[i][j + 1] - 1;
            if (l > r) {
                continue;
            }
            qry.push_back({l, r, i});
        }
    }

    const int B = std::ceil(std::sqrt(n));
    std::sort(qry.begin(), qry.end(), [&](auto &lhs, auto &rhs) {
        if (lhs[0] / B != rhs[0] / B) {
            return lhs[0] / B < rhs[0] / B;
        } else if (lhs[1] != rhs[1]) {
            int t = (lhs[0] / B) % 2;
            return t ? lhs[1] < rhs[1] : lhs[1] > rhs[1];
        } else {
            return lhs[2] < rhs[2];
        }
    });

    int res = 0;
    auto add = [&](int x) {
        cnt[x]++;
        if (cnt[x] == 1) {
            res++;
        }
    };

    auto del = [&](int x) {
        cnt[x]--;
        if (cnt[x] == 0) {
            res--;
        }
    };

    int ans = 0;
    int L = 0, R = -1;
    for (auto &[l, r, x] : qry) {
        while (L > l) {
            add(a[--L]);
        }
        while (R < r) {
            add(a[++R]);
        }
        while (L < l) {
            del(a[L++]);
        }
        while (R > r) {
            del(a[R--]);
        }
        ans = std::max(ans, res - x);
    }

    std::cout << ans << "\n";

    for (int i = 0; i < n; i++) {
        cnt[a[i]] = 0;
    }
}

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: 3612kb

input:

2
5 4
1 2 2 3 4
5 10000
5 2 3 4 1

output:

2
3

result:

ok 2 number(s): "2 3"

Test #2:

score: 0
Accepted
time: 54ms
memory: 3620kb

input:

50000
10 19
12 6 1 12 11 15 4 1 13 18
10 8
8 7 6 7 6 2 2 3 4 8
10 6
3 2 6 6 5 2 3 4 5 6
10 11
6 3 7 9 2 1 2 10 10 4
10 6
6 1 2 6 1 1 3 4 2 1
10 9
8 5 3 9 1 7 5 5 1 1
10 5
1 4 3 2 5 4 5 3 5 2
10 14
3 8 12 10 4 2 3 13 7 3
10 14
5 5 12 2 8 1 13 9 8 5
10 7
5 5 6 6 1 5 3 7 3 4
10 7
5 1 4 6 1 6 4 3 7 5
10...

output:

6
5
4
4
2
4
3
7
4
4
4
5
2
3
6
6
7
5
7
6
5
5
6
2
6
8
7
2
5
5
6
2
2
3
4
5
3
3
7
3
2
5
6
1
3
5
3
3
3
8
6
6
5
7
4
4
5
4
6
6
6
3
7
3
6
3
3
7
7
6
6
7
4
3
3
4
4
6
3
4
6
6
4
5
5
9
4
5
7
5
3
5
2
2
5
6
6
8
4
3
4
5
5
5
7
7
3
2
6
5
3
5
4
4
5
6
6
5
6
7
7
4
5
7
4
7
3
7
6
6
6
5
4
2
5
4
2
3
6
5
2
6
5
5
4
3
5
6
6
6
...

result:

ok 50000 numbers

Test #3:

score: -100
Wrong Answer
time: 60ms
memory: 3536kb

input:

100000
5 4
4 3 1 3 1
5 4
2 2 1 3 4
5 9
7 8 7 1 3
5 3
3 2 2 3 1
5 7
1 4 2 4 7
5 4
4 4 4 2 3
5 3
2 1 2 2 2
5 5
2 1 2 5 4
5 9
1 8 4 4 7
5 6
2 6 4 6 2
5 5
1 2 4 4 4
5 3
2 1 1 1 3
5 5
5 4 5 2 5
5 4
3 3 3 2 1
5 3
3 1 3 2 3
5 7
1 5 2 2 7
5 6
2 2 6 5 6
5 10
6 3 3 1 7
5 8
1 6 8 4 3
5 4
1 2 1 3 3
5 7
2 2 4 3 ...

output:

1
1
2
1
2
2
0
2
2
2
1
0
2
1
1
2
2
2
3
0
3
1
2
2
3
3
1
3
0
0
3
2
2
0
2
2
1
0
2
2
3
3
3
1
3
2
2
3
2
3
2
1
2
3
1
3
3
1
2
3
1
1
2
2
2
2
0
1
0
1
0
2
1
3
0
2
2
3
2
2
1
3
1
3
1
1
1
3
1
1
4
0
1
3
2
2
2
0
3
2
4
3
3
2
1
0
4
4
3
2
1
2
1
2
3
2
3
4
4
3
0
0
1
4
1
3
3
2
3
1
3
4
3
1
2
2
3
2
3
2
3
3
1
3
1
1
4
1
1
3
...

result:

wrong answer 2377th numbers differ - expected: '-1', found: '0'