|
| 1 | + |
| 2 | +#include <bits/stdc++.h> |
| 3 | +#define endl '\n' |
| 4 | +typedef long long ll; |
| 5 | + |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +/* |
| 9 | +PROLBLEM STATEMENT: |
| 10 | +Monocarp had a regular bracket sequence s of length n (n |
| 11 | +
|
| 12 | +is even). He even came up with his own way to calculate its cost. |
| 13 | +
|
| 14 | +He knows that in a regular bracket sequence (RBS), each opening bracket is paired up with the corresponding closing bracket. So he decided to calculate the cost of RBS as the sum of distances between pairs of corresponding bracket pairs. |
| 15 | +
|
| 16 | +For example, let's look at RBS (())(). It has three pairs of brackets: |
| 17 | +
|
| 18 | + (__)__: the distance between brackets at position 1 |
| 19 | +
|
| 20 | +and at 4 is 4−1=3 |
| 21 | +; |
| 22 | +_()___: the distance is 3−2=1 |
| 23 | +; |
| 24 | +____(): the distance is 6−5=1 |
| 25 | +
|
| 26 | + . |
| 27 | +
|
| 28 | +So the cost of (())() is 3+1+1=5. |
| 29 | +
|
| 30 | +Unfortunately, due to data corruption, Monocarp lost all characters on odd positions s1,s3,…,sn−1 |
| 31 | +. Only characters on even positions (s2,s4,…,sn |
| 32 | +
|
| 33 | +) remain. For example, (())() turned to _(_)_). |
| 34 | +
|
| 35 | +Monocarp wants to restore his RBS by placing brackets on the odd positions. But since the restored RBS may not be unique, he wants to choose one with minimum cost. It's too hard to do for Monocarp alone, so can you help him? |
| 36 | +
|
| 37 | +Reminder: A regular bracket sequence is a string consisting of only brackets, such that this sequence, when inserted 1-s and +-s, gives a valid mathematical expression. For example, (), (()) or (()())() are RBS, while ), ()( or ())(() are not. |
| 38 | +
|
| 39 | +
|
| 40 | +APPROACH: |
| 41 | +my first thought was to just fill in the blanks not to calculate the cost so i did trial & error with few test cases and found that |
| 42 | +if(s[i-1]=')')then s[i]must be '(' and vice versa for min cost; |
| 43 | +then to calculate the cost i also found a pattern where iterating over odd indices (0 indexing) if s[i]='(' you will add 1 to ans and also increase cost of prev bracket by 2 |
| 44 | +until you encounter '(' then you will pour the value of keeper into ans and reset value of keeper to 1; |
| 45 | +
|
| 46 | +
|
| 47 | +Time Compx:O(n); |
| 48 | +Space Compx:O(n); |
| 49 | +
|
| 50 | +
|
| 51 | +
|
| 52 | +SUBMISSION LINK: |
| 53 | +https://codeforces.com/contest/1997/submission/355680953 |
| 54 | +
|
| 55 | +
|
| 56 | +*/ |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +void solve(){ |
| 61 | + int n;cin>>n;string s;cin>>s; |
| 62 | + ll keeper=1,sum=0; |
| 63 | + for(int i=1;i<n;i+=2){ |
| 64 | + if(s[i]==')'){ |
| 65 | + sum+=keeper; |
| 66 | + keeper=1; |
| 67 | + } |
| 68 | + else { |
| 69 | + sum+=1; |
| 70 | + keeper+=2; |
| 71 | + } |
| 72 | + } |
| 73 | + cout<<sum<<endl; |
| 74 | + |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +int main(){ |
| 79 | + ios::sync_with_stdio(0); cin.tie(0); |
| 80 | + int t;cin>>t; |
| 81 | + while(t--) |
| 82 | + solve(); |
| 83 | + |
| 84 | +} |
0 commit comments