QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#263744 | #6454. Unbalanced Parentheses | ZhouShang | WA | 1ms | 5572kb | C++14 | 3.8kb | 2023-11-25 05:32:58 | 2023-11-25 05:32:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i,a,b) for(int i = a; i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define ary(k) array<int, k>
typedef pair<int, int> pii;
typedef vector<int> vi;
int cost[1000002];
int change[1000002];
int suf(int a) {
// cerr << a << " ";
int rj = 1;
if (a < 0) {
rj = -1;
a *= -1;
}
// cerr << rj*((a+1)/2) << "suf\n";
return rj*((a+1)/2);
}
signed main() {
cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
int n, k;
cin >> n >> k;
string slo;
cin >> slo;
int pocz_min = 0;
set<pii> pq;
set<pii, greater<pii>> used;
for (int i = 0; i < n; i++) {
cin >> cost[i];
if (cost[i] < 0) {
pocz_min -= cost[i];
cost[i] *= -1;
if (slo[i] == ')')
slo[i] = '(';
else
slo[i] = ')';
}
if (slo[i] == ')')
pq.insert({cost[i], i});
}
if (n%2) {
cout << -pocz_min << "\n";
return 0;
}
// if (2*k >= n) {
// cout << "?\n";
// return 0;
// }
int k1 = 0, k2 = 0;
for (int i = 0; i < n; i++) {
if (slo[i] == ')') {
k2--;
} else {
k2++;
}
cmx(k1, -k2);
}
int best = 1e18;
int akt = 0, curst = 0, curcost = 0, kk = 0;
while (!pq.empty() && curst + k2/2 <= k) {
auto [c, id] = *pq.begin();
pq.erase({c, id});
used.insert({c, id});
curst++;
curcost += c;
change[id] = 1;
}
if (curst + k2/2 > k)
cmn(best, curcost);
// cerr << best << "\n";
// cerr << k2 << "\n";
for (int i = 0; i < n; i++) {
if (slo[i] == ')') {
akt++;
} else {
akt--;
}
cmx(kk, akt);
if (change[i] == 1) {
curst--;
curcost -= cost[i];
used.erase({cost[i], i});
change[i] = 0;
} else if (slo[i] == ')') { // TODO: Tu weszła zmiana
pq.erase({cost[i], i});
}
if (slo[i] == '(') {
pq.insert({cost[i], i});
}
// cerr << curst << "c1 " << curcost << "\n";
int have = k2/2 + suf(akt) * 2;
while (have + curst > k + 1 && used.size() > 0) {
auto [c, id] = *used.begin();
curst--;
curcost -= c;
used.erase({c, id});
change[id] = 0;
pq.insert({c, id});
}
while (!pq.empty() && !used.empty()) {
auto [c1, id1] = *used.begin();
auto [c2, id2] = *pq.begin();
if (c1 > c2) {
used.insert({c2, id2});
used.erase({c1, id1});
pq.erase({c2, id2});
pq.insert({c1, id1});
curcost += (c2-c1);
change[id1] = 0;
change[id2] = 1;
} else {
break;
}
}
// cerr << curst << "c2 " << curcost << " ";
// cerr << i << " " << have << " ";
while (!pq.empty() && curst + have <= k) {
auto [c, id] = *pq.begin();
pq.erase({c, id});
used.insert({c, id});
curst++;
curcost += c;
change[id] = 1;
}
if (have + curst > k)
cmn(best, curcost);
// cerr << curcost << "\n";
}
if (best > 1e17)
cout << "?\n";
else
cout << best - pocz_min << "\n";
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5572kb
input:
4 1 ((() 480 617 -570 928
output:
527
result:
wrong answer 1st lines differ - expected: '480', found: '527'