QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#491970 | #4598. Darnassus | fractal# | WA | 64ms | 6520kb | C++17 | 1.2kb | 2024-07-26 03:06:12 | 2024-07-26 03:06:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 200;
int n, p[N], pos[N];
struct dsu {
int p[N], r[N];
void init(int m) {
for (int i = 1; i <= m; ++i) {
p[i] = i;
r[i] = 0;
}
}
int get(int v) {
return p[v] = (p[v] == v ? v : get(p[v]));
}
bool unite(int v, int u) {
if ((v = get(v)) == (u = get(u))) return false;
if (r[v] < r[u]) swap(v, u);
r[v]++;
p[u] = v;
return true;
}
} ds;
void solve() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> p[i];
pos[p[i]] = i;
}
vector<pair<int, pair<int, int>>> e;
for (int i = 2; i <= n; ++i) {
e.push_back({abs(p[i] - p[i - 1]), {i, i - 1}});
}
for (int i = 2; i <= n; ++i) {
e.push_back({abs(pos[i] - pos[i - 1]), {pos[i], pos[i - 1]}});
}
ds.init(n);
sort(e.begin(), e.end());
long long ans = 0;
for (auto [w, l] : e) {
if (ds.unite(l.first, l.second)) {
ans += w;
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int t = 1;
cin >> t;
while (t--) solve();
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 64ms
memory: 6520kb
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:
375460223 375251604 99997 49998 375816636
result:
wrong answer 1st lines differ - expected: '99246830', found: '375460223'