QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#567251 | #9313. Make Max | bigJ | WA | 170ms | 25384kb | C++20 | 3.6kb | 2024-09-16 10:34:39 | 2024-09-16 10:34:39 |
Judging History
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 l = j - 1;
int r = dsu[j] + 1;
if (l < 0 && r >= n) {
continue;
}
ans += r - j;
if (l < 0) {
assert(j == 0);
dsu.merge(r, j);
int t = a[dsu[r]];
a[j] = t;
s[t].extract(r);
s[t].insert(j);
} else if (r >= n) {
dsu.merge(l, j);
a[j] = a[dsu[l]];
} else {
int min = std::min(a[dsu[l]], a[dsu[r]]);
if (a[dsu[l]] == min) {
dsu.merge(l, j);
a[j] = a[dsu[l]];
}
if (a[dsu[r]] == min) {
dsu.merge(r, j);
int t = a[dsu[r]];
a[j] = t;
s[t].extract(r);
s[t].insert(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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3880kb
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: 170ms
memory: 25384kb
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:
254068 244652
result:
wrong answer 1st numbers differ - expected: '4084978', found: '254068'