QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#136768#236. Balanced SequenceVengeful_Spirit#100 ✓34ms9732kbC++201.3kb2023-08-09 11:07:172023-08-09 11:07:22

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-09 11:07:22]
  • 评测
  • 测评结果:100
  • 用时:34ms
  • 内存:9732kb
  • [2023-08-09 11:07:17]
  • 提交

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