QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#136784#236. Balanced SequencekiwiHM#100 ✓37ms4872kbC++141.2kb2023-08-09 11:23:202023-08-09 11:23: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:23:22]
  • 评测
  • 测评结果:100
  • 用时:37ms
  • 内存:4872kb
  • [2023-08-09 11:23:20]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 5;

int n;
int stk[maxn];
char buf[maxn];

inline bool cmp(const pair<int, int> &p, const pair<int, int> &q){
	if((p.first > p.second) != (q.first > q.second)) return (p.first > p.second);
	if(p.first > p.second) return p.second < q.second;
	return p.first > q.first;
}

inline void solve(){
	scanf("%d", &n);
	int ans = 0;
	vector<pair<int, int> > vec;
	for(int i = 0; i < n; ++i){
		scanf("%s", buf); int len = strlen(buf);
		int top = 0;
		for(int j = 0; j < len; ++j){
			if(buf[j] == '(') stk[++top] = '(';
			else{
				if(top && stk[top] == '(') --top, ++ans;
				else stk[++top] = ')';
			}
		}
		int x = 0, y = 0;
		for(int j = 1; j <= top; ++j) x += (stk[j] == '('), y += (stk[j] == ')');
		vec.push_back(make_pair(x, y));
	}
	sort(vec.begin(), vec.end(), cmp);
	
	int rig = 0;
	for(int i = 0; i < vec.size(); ++i){
		// printf("left: %d right: %d\n", vec[i].first, vec[i].second);
		int cur = min(rig, vec[i].second);
		ans += cur, rig -= cur;
		rig += vec[i].first;
	}
	
	printf("%d\n", ans * 2);
}

int main(){
	int T;
	for(scanf("%d", &T); T--; solve());
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 37ms
memory: 4872kb

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