結果
| 問題 |
No.3148 Min-Cost Destruction of Parentheses
|
| コンテスト | |
| ユーザー |
Nauclhlt🪷
|
| 提出日時 | 2025-05-11 23:59:25 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,178 bytes |
| コンパイル時間 | 2,655 ms |
| コンパイル使用メモリ | 209,640 KB |
| 実行使用メモリ | 8,008 KB |
| 最終ジャッジ日時 | 2025-05-15 23:07:21 |
| 合計ジャッジ時間 | 4,833 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 2 |
| other | AC * 5 WA * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
int N;
vector<int> parents;
vector<long long> zeros;
vector<int> ones;
long long ans;
UnionFind(const vector<int>& A) {
N = A.size();
parents.assign(N, -1);
zeros = vector<long long>(A.begin(), A.end());
ones.assign(N, 1);
ans = 0;
}
int find(int x) {
if (parents[x] < 0) return x;
vector<int> st;
while (parents[x] >= 0) {
st.push_back(x);
x = parents[x];
}
for (int y : st) parents[y] = x;
return x;
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
parents[x] += parents[y];
parents[y] = x;
ans += ones[x] * zeros[y];
zeros[x] += zeros[y];
ones[x] += ones[y];
}
int size(int x) {
return -parents[find(x)];
}
bool same(int x, int y) {
return find(x) == find(y);
}
};
int main() {
int N;
cin >> N;
string S;
cin >> S;
vector<int> A(N + 1);
A[0] = 0;
for (int i = 1; i <= N; ++i) cin >> A[i];
UnionFind uf(A);
vector<int> stack = {0};
vector<int> parent(N + 1, -1);
int next_idx = 1;
for (int i = 0; i < 2 * N; ++i) {
if (S[i] == '(') {
parent[next_idx] = stack.back();
stack.push_back(next_idx++);
} else {
stack.pop_back();
}
}
// 優先度付きキュー (最大ヒープ)
using T = pair<double, int>;
priority_queue<T> pq;
for (int i = 0; i <= N; ++i) {
pq.emplace((double)(-A[i]), i);
}
while (!pq.empty()) {
auto [value, idx] = pq.top(); pq.pop();
int rep = uf.find(idx);
double check = -(double)uf.zeros[rep] / uf.ones[rep];
if (value != check) continue;
uf.unite(parent[idx], idx);
int new_head = uf.find(idx);
if (new_head != 0) {
double new_val = -(double)uf.zeros[new_head] / uf.ones[new_head];
pq.emplace(new_val, new_head);
}
}
cout << uf.ans << endl;
return 0;
}
Nauclhlt🪷