Skip to content

Commit 30b37db

Browse files
authored
Create problem statement for bracket sequence cost
Added problem statement for regular bracket sequence restoration and cost calculation.
1 parent c04aa2b commit 30b37db

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

  • Problems/Data-structures/Day-04
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# PROBLEM STATEMENT :
2+
3+
Monocarp had a regular bracket sequence **s** of length **n** (**n** is even).
4+
He even came up with his own way to calculate its **cost**.
5+
6+
In a regular bracket sequence (RBS), each opening bracket is paired with the corresponding closing bracket.
7+
The **cost** of an RBS is defined as the **sum of distances** between all corresponding bracket pairs.
8+
9+
For example, consider the RBS **(())()**. It has three bracket pairs:
10+
11+
- Brackets at positions **1** and **4** → distance = **4 − 1 = 3**
12+
- Brackets at positions **2** and **3** → distance = **3 − 2 = 1**
13+
- Brackets at positions **5** and **6** → distance = **6 − 5 = 1**
14+
15+
So, the total cost is **3 + 1 + 1 = 5**.
16+
17+
---
18+
19+
Unfortunately, due to data corruption, Monocarp lost all characters on **odd positions**
20+
(**s₁, s₃, …, sₙ₋₁**). Only characters on **even positions** remain.
21+
22+
For example:
23+
(())() → _(_)_)
24+
25+
26+
Monocarp wants to restore the string by placing brackets on odd positions such that:
27+
28+
- The resulting string is a **valid regular bracket sequence**
29+
- The **total cost is minimized**
30+
31+
It is guaranteed that at least one valid restoration exists.
32+
33+
---
34+
35+
## Input
36+
37+
The first line contains a single integer **t**
38+
(**1 ≤ t ≤ 5000**) — the number of test cases.
39+
40+
For each test case:
41+
42+
- The first line contains an even integer **n**
43+
(**2 ≤ n ≤ 2 · 10⁵**) — the length of the string.
44+
- The second line contains a string **s** of length **n**, where:
45+
- Characters at **odd positions** are `'_'`
46+
- Characters at **even positions** are `'('` or `')'`
47+
48+
Additional constraints:
49+
50+
- The sum of **n** over all test cases does not exceed **2 · 10⁵**
51+
- The string can always be restored to at least one valid RBS
52+
53+
---
54+
55+
## Output
56+
57+
For each test case, print a single integer —
58+
the **minimum possible cost** of a regular bracket sequence that can be obtained.
59+
60+
61+
[PROBLEM LINK](https://codeforces.com/problemset/problem/1997/C)

0 commit comments

Comments
 (0)