Skip to content

Commit 9c63e33

Browse files
authored
Merge pull request #365 from PHOX-9/b1
Add solution
2 parents bcc066c + c99d09a commit 9c63e33

1 file changed

Lines changed: 208 additions & 0 deletions

File tree

  • Problems/Data-structures/Day-04/sol/Ibrahim
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
//https://codeforces.com/problemset/submission/1997/355801068
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
#define int long long
7+
#define ll long long
8+
#define ull unsigned long long
9+
#define ld long double
10+
#define pii pair<int,int>
11+
#define vi vector<int>
12+
#define vvi vector<vector<int>>
13+
#define vpi vector<pair<int,int>>
14+
#define all(x) (x).begin(), (x).end()
15+
16+
17+
const long long MAXN = 2e6;
18+
const long long MOD = 1e9 + 7;
19+
const int INF = 1e18;
20+
const ld EPS = 1e-9;
21+
22+
23+
#define fastio ios::sync_with_stdio(false); cin.tie(nullptr);
24+
25+
#ifndef ONLINE_JUDGE
26+
#define debug(x) cerr << #x << " = " << x << "\n";
27+
#else
28+
#define debug(x)
29+
#endif
30+
31+
// Utility functions
32+
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
33+
int lcm(int a, int b) { return a / gcd(a, b) * b; }
34+
int mod_add(int a, int b, int m=MOD) { return ((a % m) + (b % m) + m) % m; }
35+
int mod_sub(int a, int b, int m=MOD) { return ((a % m) - (b % m) + m) % m; }
36+
int mod_mul(int a, int b, int m=MOD) { return ((a % m) * (b % m)) % m; }
37+
38+
//Binary Exponentiation
39+
int binexp(int a, int b, int m=MOD) {
40+
int res = 1;
41+
a %= m;
42+
while(b > 0) {
43+
if(b & 1) res = (res * a) % m;
44+
a = (a * a) % m;
45+
b >>= 1;
46+
}
47+
return res;
48+
}
49+
50+
int mod_inv(int a, int m=MOD) {
51+
return binexp(a, m - 2, m);
52+
}
53+
54+
long long fac[MAXN + 1];
55+
long long inv[MAXN + 1];
56+
57+
long long exp(long long x, long long n, long long m) {
58+
x %= m;
59+
long long res = 1;
60+
while (n > 0) {
61+
if (n % 2 == 1) { res = res * x % m; }
62+
x = x * x % m;
63+
n /= 2;
64+
}
65+
return res;
66+
}
67+
68+
void factorial() {
69+
fac[0] = 1;
70+
for (long long i = 1; i <= MAXN; i++) { fac[i] = fac[i - 1] * i % MOD; }
71+
}
72+
73+
void inverses() {
74+
inv[MAXN] = exp(fac[MAXN], MOD - 2, MOD);
75+
for (long long i = MAXN; i >= 1; i--) { inv[i - 1] = inv[i] * i % MOD; }
76+
}
77+
78+
long long choose(long long n, long long r) {
79+
if (r > n)return 0ll;
80+
return (fac[n] * inv[r] % MOD * inv[n - r] % MOD) % MOD;
81+
}
82+
83+
long long catalan(long long n) {
84+
return (exp(n + 1, MOD - 2, MOD) % MOD * choose(2 * n, n) % MOD) % MOD;
85+
}
86+
87+
//Returns all prime numbers <=N
88+
vector<int> sieve(int n) {
89+
vector<bool> prime(n + 1, true);
90+
for (int p=2;p*p<=n;p++) {
91+
if (prime[p] == true) {
92+
93+
for (int i=p*p;i<=n;i+=p)
94+
prime[i] = false;
95+
}
96+
}
97+
98+
vector<int> res;
99+
for (int p = 2; p <= n; p++){
100+
if (prime[p]){
101+
res.push_back(p);
102+
}
103+
}
104+
return res;
105+
}
106+
107+
//Prime factors of all numbers upto N
108+
vector<vector<int>> primefactors(int N)
109+
{
110+
vvi pfac(N + 1);
111+
for (int i=2;i<=N;i++){
112+
if (!pfac[i].empty())
113+
continue;
114+
115+
for (int j = i; j <= N; j += i)
116+
pfac[j].push_back(i);
117+
}
118+
119+
return pfac;
120+
}
121+
122+
//smallest prime factor of a number
123+
int spf(int n) {
124+
if (n % 2 == 0) return 2;
125+
for (int i = 3; i * i <= n; i += 2) {
126+
if (n % i == 0) return i;
127+
}
128+
return n;
129+
}
130+
131+
int power2(int p)
132+
{
133+
int v=1ll<<p;
134+
return v;
135+
}
136+
137+
//Sort functions
138+
139+
void sort(vector<int>&a)
140+
{
141+
sort(a.begin(),a.end());
142+
}
143+
144+
void psort(vector<pair<int,int>>&a)
145+
{
146+
sort(a.begin(),a.end());
147+
}
148+
149+
void rsort(vector<int>&a)
150+
{
151+
sort(a.rbegin(),a.rend());
152+
}
153+
154+
void rpsort(vector<pair<int,int>>&a)
155+
{
156+
sort(a.rbegin(),a.rend());
157+
}
158+
159+
//2-D Vector Declaration:
160+
//vvi mat(n,vi(m)); // nxm matrix, all initialized to 0
161+
162+
163+
/***************Code***************/
164+
165+
void solve() {
166+
int n,i;
167+
cin>>n;
168+
string s;
169+
cin>>s;
170+
171+
s[0]='(';
172+
for(i=1;i<n;i++)
173+
{
174+
if(i%2==0)
175+
{
176+
if(s[i-1]=='(')
177+
s[i]=')';
178+
else
179+
s[i]='(';
180+
}
181+
}
182+
// cout<<s<<endl;
183+
stack <int> st;
184+
int ans=0;
185+
for(i=0;i<n;i++)
186+
{
187+
if(s[i]=='(')
188+
st.push(i);
189+
else
190+
{
191+
int v=st.top();
192+
ans+=i-v;
193+
st.pop();
194+
}
195+
}
196+
cout<<ans<<endl;
197+
}
198+
199+
200+
signed main() {
201+
fastio;
202+
int t = 1;
203+
cin >> t;
204+
while (t--) {
205+
solve();
206+
}
207+
return 0;
208+
}

0 commit comments

Comments
 (0)