Skip to content

Commit e52803f

Browse files
authored
Create day004sol01.cpp
1 parent c51e988 commit e52803f

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Submission Link: https://codeforces.com/contest/1997/submission/355835686
2+
3+
4+
/*
5+
So first I made the RBS with minimmum cost by putting closing bracket as soon as we get a opening bracket before it, i.e., closing bracket as early as
6+
possible(greedy appraoch). After getting corect bracket sequence I put the indexes of '(' in vector o and indexes of ')' in vector c in order they occur. Both
7+
vector must contain n/2 elements and now for final answer I simply added the difference of each element in o and c at same index to ans to calculate cost
8+
which was initially zero and then printed the ans.
9+
*/
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
int main() {
14+
long long t;
15+
cin>>t;
16+
while(t--) {
17+
long long n;
18+
cin>>n;
19+
string s;
20+
cin>>s;
21+
long long a=0;
22+
for(long long i=0; i<n; i++) {
23+
if(s[i]=='(') a++;
24+
if(s[i]==')') a--;
25+
if(s[i]=='_') {
26+
if (a>0) {
27+
s[i]=')';
28+
a--;
29+
}
30+
else {
31+
s[i]='(';
32+
a++;
33+
}
34+
}
35+
}
36+
long long ans=0;
37+
vector <long long> o, c;
38+
for(long long i=0; i<n; i++) {
39+
if(s[i]=='(') o.push_back(i);
40+
else c.push_back(i);
41+
}
42+
for(long long i=0; i<n/2; i++) ans+=(c[i]-o[i]);
43+
cout<<ans<<endl;
44+
}
45+
}

0 commit comments

Comments
 (0)