QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#383057 | #7685. Barkley II | Leonard# | WA | 163ms | 4692kb | C++20 | 2.1kb | 2024-04-08 21:15:46 | 2024-04-08 21:15:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N = 5e5 + 10;
struct node {
int l, r, v;
#define ls u << 1
#define rs u << 1 | 1
} tr[N << 2];
void pushup(int u) {
tr[u].v = tr[ls].v + tr[rs].v;
}
void build(int u, int l, int r) {
tr[u] = {l, r};
if(l == r) {
return ;
}
int mid = l + r >> 1;
build(ls, l, mid), build(rs, mid + 1, r);
pushup(u);
}
int query(int u, int l, int r) {
if(tr[u].l >= l && tr[u].r <= r) return tr[u].v;
int mid = tr[u].l + tr[u].r >> 1;
int v = 0;
if(l <= mid) v = v + query(ls, l, r);
if(r > mid) v = v + query(rs, l, r);
return v;
}
void modify(int u, int x, int v) {
if(tr[u].l == x && tr[u].r == x) tr[u].v = v;
else {
int mid = tr[u].l + tr[u].r >> 1;
if(mid >= x) modify(ls, x, v);
else modify(rs, x, v);
pushup(u);
}
}
int n, m, a[N];
void solve() {
cin >> n >> m;
vector<vector<int>> g(m + 2);
for(int i = 1; i <= m + 1; i++) {
g[i].push_back(0);
}
vector<set<int>> s(m + 2);
for(int i = 1; i <= n; i++) {
cin >> a[i];
g[a[i]].push_back(i);
s[a[i]].insert(i);
}
build(1, 1, n);
for(int i = 1; i <= m; i++) {
if(s[i].empty()) continue;
auto it = *s[i].begin();
modify(1, it, 1);
s[i].erase(it);
}
for(int i = 1; i <= m + 1; i++) {
g[i].push_back(n + 1);
}
vector<vector<array<int, 2>>> q(n + 2);
for(int i = 1; i <= m + 1; i++) {
for(int j = 0; j + 1 < g[i].size(); j++) {
q[g[i][j] + 1].push_back({g[i][j + 1] - 1, i});
}
}
sort(q.begin(), q.end());
int ans = -1e9;
for(int i = 1; i <= n; i++) {
for(int j = 0; j < q[i].size(); j++) {
int l = i, r = q[i][j][0], y = q[i][j][1];
if(l > r) continue;
int cnt = query(1, l, r);
ans = max(cnt - y, ans);
if(!s[a[i]].empty()) {
auto it = *s[a[i]].begin();
modify(1, it, 1);
//cout << it << '\n';
s[a[i]].erase(it);
}
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
t = 1;
cin >> t;
while(t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4692kb
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
Wrong Answer
time: 163ms
memory: 3888kb
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:
7 4 4 4 3 4 2 6 5 5 5 4 2 3 3 4 5 5 7 5 4 4 4 2 4 7 7 2 4 4 7 2 4 3 2 4 5 4 5 3 3 5 5 3 4 5 3 3 4 8 5 5 6 6 4 3 6 4 4 4 5 5 5 3 6 2 2 7 7 7 5 5 5 3 4 3 3 5 3 4 4 4 3 2 4 9 2 6 6 3 5 4 2 2 3 4 5 7 4 2 4 4 4 5 7 7 4 1 7 5 2 5 4 5 5 4 5 6 5 7 5 5 5 6 4 6 4 6 4 4 4 5 2 3 4 3 3 1 6 5 3 5 6 4 4 4 4 4 5 4 ...
result:
wrong answer 1st numbers differ - expected: '6', found: '7'