QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#49185 | #4598. Darnassus | ckiseki# | AC ✓ | 1440ms | 420260kb | C++ | 1.7kb | 2022-09-19 16:33:12 | 2022-09-19 16:33:14 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
struct Dsu {
vector<int> pa, rk;
Dsu(int n) : pa(n), rk(n) {
iota(pa.begin(), pa.end(), 0);
}
int anc(int x) {
return pa[x] == x ? x : pa[x]=anc(pa[x]);
}
bool join(int x, int y) {
x = anc(x);
y = anc(y);
if (x == y) return false;
if (rk[x] < rk[y]) swap(x, y);
pa[y] = x;
if (rk[x] == rk[y]) ++rk[x];
return true;
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t; cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> p(n), q(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
--p[i];
q[p[i]] = i;
}
vector<vector<pair<int,int>>> e(n + 1);
const int K = ceil(sqrt(n));
for (int i = 0; i < n; i++) {
for (int j = max(0, i-K); j <= min(n-1, i+K); j++) {
if (j == i) continue;
int64_t cost = 1LL * abs(i - j) * abs(p[i] - p[j]);
if (cost <= n) {
e[cost].emplace_back(i, j);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = max(0, i-K); j <= min(n-1, i+K); j++) {
if (j == i) continue;
int64_t cost = 1LL * abs(i - j) * abs(q[i] - q[j]);
if (cost <= n) {
e[cost].emplace_back(q[i], q[j]);
}
}
}
Dsu dsu(n);
int64_t ans = 0;
for (int i = 1; i <= n; i++) {
for (auto [a, b]: e[i]) {
if (dsu.join(a, b)) {
ans += i;
}
}
}
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1440ms
memory: 420260kb
input:
5 50000 37763 49860 5765 19637 25267 12594 48787 14155 47404 5524 39461 31259 25877 30705 2684 7916 11685 39034 33744 43465 9933 2403 35987 7114 26874 22167 9016 15866 8799 1644 12470 30017 38907 46034 9522 31054 8591 20733 49457 46805 12191 40590 14403 5658 6721 2458 15939 12811 27626 33040 47408 5...
output:
99246830 99608266 99997 49998 98832945
result:
ok 5 lines