Skip to content

Commit 0ae74ac

Browse files
authored
Merge pull request #366 from dwivediprashant/code/day-4-que-1
code(java) : Solved que-1 of day-4 (Monocarp and Bracket Sequence)
2 parents 9c63e33 + 53b30da commit 0ae74ac

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
Problem Summary:
3+
- Given a partially known regular bracket sequence.
4+
- Odd positions are missing ('_'), even positions are fixed.
5+
- Restore missing brackets to form a valid RBS with minimum total cost.
6+
- Cost = sum of distances between matched bracket pairs.
7+
8+
Key Ideas:
9+
- Valid RBS requires balance never goes negative.
10+
- To minimize cost, close brackets as early as possible.
11+
- Only open a bracket when forced.
12+
13+
Greedy Strategy:
14+
- Traverse left to right.
15+
- Maintain current balance and a stack of '(' positions.
16+
- At '_' positions:
17+
• If balance == 0 → must place '('
18+
• Else → place ')'
19+
- At fixed positions:
20+
• Follow given '(' or ')'
21+
22+
Cost Calculation:
23+
- On encountering ')', pop matching '(' from stack.
24+
- Add (currentIndex - openIndex) to total cost.
25+
26+
Complexity:
27+
- Time: O(n) per test case
28+
- Space: O(n)
29+
30+
Problem submission link:----------------------------------------------------------------------------------
31+
https://codeforces.com/contest/1997/submission/355801157
32+
*/
33+
34+
import java.io.*;
35+
import java.util.*;
36+
37+
public class Solution1 {
38+
public static void main(String[] args) throws Exception {
39+
FastScanner fs = new FastScanner(System.in);
40+
StringBuilder out = new StringBuilder();
41+
42+
int t = fs.nextInt();
43+
while (t-- > 0) {
44+
int n = fs.nextInt();
45+
char[] s = fs.next().toCharArray();
46+
47+
int balance = 0;
48+
long cost = 0;
49+
Deque<Integer> stack = new ArrayDeque<>();
50+
51+
for (int i = 0; i < n; i++) {
52+
if ((i & 1) == 0) {
53+
if (balance == 0) {
54+
stack.push(i);
55+
balance++;
56+
} else {
57+
balance--;
58+
int openPos = stack.pop();
59+
cost += i - openPos;
60+
}
61+
} else {
62+
if (s[i] == '(') {
63+
stack.push(i);
64+
balance++;
65+
} else {
66+
balance--;
67+
int openPos = stack.pop();
68+
cost += i - openPos;
69+
}
70+
}
71+
}
72+
73+
out.append(cost).append('\n');
74+
}
75+
76+
System.out.print(out);
77+
}
78+
79+
static class FastScanner {
80+
private final byte[] buffer = new byte[1 << 16];
81+
private int ptr = 0, len = 0;
82+
private final InputStream in;
83+
84+
FastScanner(InputStream in) {
85+
this.in = in;
86+
}
87+
88+
private int read() throws IOException {
89+
if (ptr >= len) {
90+
len = in.read(buffer);
91+
ptr = 0;
92+
if (len <= 0) return -1;
93+
}
94+
return buffer[ptr++];
95+
}
96+
97+
int nextInt() throws IOException {
98+
int c;
99+
do {
100+
c = read();
101+
} while (c <= ' ');
102+
boolean neg = false;
103+
if (c == '-') {
104+
neg = true;
105+
c = read();
106+
}
107+
int val = 0;
108+
while (c > ' ') {
109+
val = val * 10 + (c - '0');
110+
c = read();
111+
}
112+
return neg ? -val : val;
113+
}
114+
115+
String next() throws IOException {
116+
int c;
117+
do {
118+
c = read();
119+
} while (c <= ' ');
120+
StringBuilder sb = new StringBuilder();
121+
while (c > ' ') {
122+
sb.append((char) c);
123+
c = read();
124+
}
125+
return sb.toString();
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)