QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#685223 | #7685. Barkley II | Martian148 | RE | 0ms | 3544kb | C++20 | 1.7kb | 2024-10-28 18:21:07 | 2024-10-28 18:21:08 |
Judging History
answer
#include <bits/stdc++.h>
struct query {
int l, r, mex;
bool operator < (const query &rhs) const {
return r < rhs.r;
}
};
void solve() {
int n, M; std::cin >> n >> M;
std::vector<int> a(n + 1, 0);
for (int i = 1; i <= n; i++) std::cin >> a[i];
M = *max_element(a.begin() + 1, a.end()) + 1;
std::vector<std::vector<int>> p(M + 1);
for (int i = 1; i <= M; i++)
p[i].push_back(0);
for (int i = 1; i <= n; i++)
p[a[i]].push_back(i);
for (int i = 1; i <= M; i++)
p[i].push_back(n + 1);
std::vector<query> q;
for (int i = 1; i <= M; i++) {
for (int j = 0; j + 1 < p[i].size(); j++) {
if (p[i][j + 1] - p[i][j] <= 1) continue;
q.push_back({p[i][j] + 1, p[i][j + 1] - 1, i});
}
}
std::vector<int> c(n + 1, 0), pre(n + 1, 0);
auto add_x = [&] (int x, int val) -> void {
for (; x <= n; x += x & -x)
c[x] += val;
};
auto query_x = [&] (int x) -> int {
int res = 0;
for (; x; x -= x & -x)
res += c[x];
return res;
};
std::sort(q.begin(), q.end());
int j = 0, ans = 0;
for (int i = 1; i <= n; i++) {
if (pre[a[i]]) add_x(pre[a[i]], -1);
add_x(i, 1);
pre[a[i]] = i;
while (j < q.size() && q[j].r == i) {
int now = query_x(q[j].r) - query_x(q[j].l - 1);
ans = std::max(ans, now - q[j].mex);
j += 1;
}
}
std::cout << ans << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL); std::cout.tie(NULL);
int T; std::cin >> T;
while (T--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3544kb
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: -100
Runtime Error
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...