QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#136768 | #236. Balanced Sequence | Vengeful_Spirit# | 100 ✓ | 34ms | 9732kb | C++20 | 1.3kb | 2023-08-09 11:07:17 | 2023-08-09 11:07:22 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int>> a;
void solve() {
int n;
cin >> n;
vector<string> s(n);
a.clear();
int len=0, all=0;
for(auto &x : s) {
cin >> x;
int now = 0, mi = 0;
for(auto &y : x) {
if(y == '(') {
now ++;
} else now--;
mi = min(mi, now);
}
len += x.size();
all += now;
a.push_back({mi, now});
}
int ans = len - all;
vector<pair<int, int>> b, c;
for(int i = 0; i < n; ++i) {
if(a[i].second >= 0) c.push_back({a[i].first, a[i].second});
else b.push_back({a[i].second - a[i].first, a[i].first});
}
sort(c.begin(), c.end()); reverse(c.begin(), c.end());
sort(b.begin(), b.end()); reverse(b.begin(), b.end());
int fd = 0, now = 0;
for(int i = 0; i < c.size(); ++i) {
fd = min(fd, now + c[i].first);
now += c[i].second;
}
for(int i = 0; i < b.size(); ++i) {
fd = min(fd, now + b[i].second);
now += b[i].first + b[i].second;
}
ans = ans + fd * 2;
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while(T--) {
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 34ms
memory: 9732kb
input:
482 2 ()())())())()(()))))(())()())))))))))))((()()()))(()()((((((())))(())()) ((())(((()(())())())()))))(( 2 (()(()))))())))))()(()))(())( ())(()()(()(()))(((()))))))())))))))))(()())()((()))()()))(()(()(((())) 1 ))())(())(())(()()()((())())(())))()(()(()(())()((()()()))()((()(()(((()))))(((((()(()...
output:
80 80 88 86 92 90 88 86 92 98 84 96 80 88 96 92 92 90 96 82 92 80 82 84 88 94 88 80 92 82 88 88 88 90 82 88 96 78 96 98 94 98 68 78 82 90 90 92 90 80 78 90 78 84 94 94 84 90 84 92 96 96 82 92 90 90 88 86 94 94 88 94 84 86 96 86 82 90 98 82 78 94 88 86 80 88 96 86 84 86 92 84 84 90 92 82 86 94 84 94 ...
result:
ok 482 lines