Skip to content

Commit dae6086

Browse files
authored
Merge branch 'opencodeiiita:main' into DoonMad
2 parents d466217 + 96346b5 commit dae6086

14 files changed

Lines changed: 978 additions & 1 deletion

File tree

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1+
Recommended for freshers. (Only how to use them, implementation will be covered in your dsa course)
2+
3+
COMPLETE STL
4+
5+
video: [https://www.youtube.com/watch?v=RRVYpIET_RU]
6+
7+
1. STACK
8+
19
[https://www.programiz.com/cpp-programming/stack]
10+
211
[https://www.youtube.com/watch?v=WK97Pj0wa7A&list=PL1w8k37X_6L9NXrP1D31hDTKcdAPIL0cG&index=10]
312

4-
Recommended for freshers.
13+
2. HEAP
14+
15+
[https://www.programiz.com/cpp-programming/priority-queue]
16+
17+
[https://www.youtube.com/watch?v=WhIcVlkZ19s&list=PL1w8k37X_6L9NXrP1D31hDTKcdAPIL0cG&index=12]
18+
19+
20+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
DAY 4
3+
Q1 Even Positions
4+
.*/
5+
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
9+
long long solve_case(string s) {
10+
int n = s.size();
11+
12+
int fixedOpen = 0, fixedClose = 0;
13+
for (char c : s) {
14+
if (c == '(') fixedOpen++;
15+
if (c == ')') fixedClose++;
16+
}
17+
18+
int needOpen = n / 2 - fixedOpen;
19+
int needClose = n / 2 - fixedClose;
20+
21+
int balance = 0;
22+
23+
// restore the string
24+
for (int i = 0; i < n; ++i) {
25+
if (s[i] == '(') balance++;
26+
else if (s[i] == ')') balance--;
27+
else {
28+
// underscore -> choose bracket
29+
if (needClose > 0 && balance > 0) {
30+
s[i] = ')';
31+
needClose--;
32+
balance--;
33+
} else {
34+
s[i] = '(';
35+
needOpen--;
36+
balance++;
37+
}
38+
}
39+
}
40+
41+
// compute cost
42+
long long cost = 0;
43+
stack<int> st;
44+
45+
for (int i = 0; i < n; ++i) {
46+
if (s[i] == '(') st.push(i + 1);
47+
else {
48+
int openIdx = st.top(); st.pop();
49+
cost += (i + 1) - openIdx;
50+
}
51+
}
52+
53+
return cost;
54+
}
55+
56+
int main() {
57+
58+
int t;
59+
cin >> t;
60+
while (t--) {
61+
int n;
62+
string s;
63+
cin >> n >> s;
64+
cout << solve_case(s) << "\n";
65+
}
66+
return 0;
67+
}
68+
69+
//TC =O(n) and SC = O(n)
70+
/*
71+
My submission : https://codeforces.com/contest/1997/submission/355669016
72+
*/
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
DAY 4
3+
Q2 Accumulator Apex
4+
.*/
5+
/*
6+
We can only take elements from the left of each list, so every valid move is some prefix of a list.
7+
8+
For each prefix we know:
9+
10+
pref = sum of the prefix
11+
12+
mn = minimum prefix sum while walking through it
13+
14+
A prefix is usable only if:
15+
16+
x + mn >= 0
17+
18+
19+
If we take it, we end with:
20+
21+
x += pref
22+
23+
24+
We consider all prefixes from all lists and always pick the one that safely gives the most benefit.
25+
For each list we only keep the best prefix we have already used.
26+
27+
*/
28+
29+
#include <bits/stdc++.h>
30+
using namespace std;
31+
32+
struct Item {
33+
long long pref;
34+
long long minPref;
35+
int idx;
36+
};
37+
38+
struct Cmp {
39+
bool operator()(const Item& a, const Item& b) const {
40+
if (a.minPref != b.minPref) return a.minPref < b.minPref;
41+
return a.pref < b.pref;
42+
}
43+
};
44+
45+
int main() {
46+
47+
48+
long long x;
49+
int k;
50+
cin >> x >> k;
51+
52+
vector<vector<long long>> lists(k);
53+
for (int i = 0; i < k; ++i) {
54+
int len;
55+
cin >> len;
56+
lists[i].resize(len);
57+
for (int j = 0; j < len; ++j) cin >> lists[i][j];
58+
}
59+
60+
priority_queue<Item, vector<Item>, Cmp> pq;
61+
vector<vector<long long>> pref(k);
62+
63+
for (int i = 0; i < k; ++i) {
64+
long long s = 0, mn = 0;
65+
pref[i].push_back(0);
66+
for (auto v : lists[i]) {
67+
s += v;
68+
mn = min(mn, s);
69+
pref[i].push_back(s);
70+
pq.push({s, mn, i});
71+
}
72+
}
73+
74+
long long best = x;
75+
vector<long long> taken(k, 0);
76+
77+
while (!pq.empty()) {
78+
auto cur = pq.top();
79+
pq.pop();
80+
81+
if (best + cur.minPref < 0) break;
82+
83+
if (cur.pref > taken[cur.idx]) {
84+
best -= taken[cur.idx];
85+
best += cur.pref;
86+
taken[cur.idx] = cur.pref;
87+
}
88+
}
89+
90+
cout << best << "\n";
91+
return 0;
92+
}
93+
94+
95+
96+
97+
//TC =O(Nlog(N)) and SC = O(N)
98+
/*
99+
My submission : https://codeforces.com/contest/1912/submission/355690272
100+
*/
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
3+
We have to find the minimum cost that we could acheive by placing ( and ) in the blank spaces. We have to make sure that () are valid.
4+
5+
I saw the pattern, that we can set the blank to ) as long as there is atleast one open (.
6+
7+
Time: O(n);
8+
Space: O(n);
9+
10+
Submission Link: https://codeforces.com/contest/1997/submission/355679346
11+
12+
*/
13+
14+
#include <bits/stdc++.h>
15+
using namespace std;
16+
17+
using ll = long long;
18+
19+
void solve() {
20+
ll n;
21+
cin >> n;
22+
string s;
23+
cin >> s;
24+
25+
// cout << "n, s: " << n << " " << s << endl;
26+
27+
ll ans = 0;
28+
stack<ll> st;
29+
for (ll i = 0; i < n; i++) {
30+
if (s[i] == '(') {
31+
st.push(i);
32+
// cout << i << " pushed (\n";
33+
}
34+
else if (s[i] == ')') {
35+
if (st.empty()) {
36+
cout << -1 << endl;
37+
return;
38+
}
39+
ll start = st.top();
40+
st.pop();
41+
ans += i - start;
42+
// cout << i << " poped )\n";
43+
}
44+
else {
45+
if (st.empty()) {
46+
st.push(i);
47+
// cout << i << " pushed _\n";
48+
}
49+
else {
50+
ll start = st.top();
51+
st.pop();
52+
ans += i - start;
53+
// cout << i << " popped _\n";
54+
}
55+
}
56+
}
57+
58+
cout << ans << endl;
59+
}
60+
61+
int main() {
62+
int t;
63+
cin >> t;
64+
while (t--) {
65+
solve();
66+
// cout << t << " done\n";
67+
}
68+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
//TC - O(n) and SC - O(n)
3+
//Submission Link - https://codeforces.com/contest/1997/submission/355786129
4+
5+
#include <iostream>
6+
#include <string>
7+
#include <vector>
8+
using namespace std;
9+
void solve() {
10+
int n;
11+
cin >> n;
12+
string s;
13+
cin >> s;
14+
long long total_cost = 0;
15+
long long current_balance = 0;
16+
for (int i = 0; i < n; ++i) {
17+
if (s[i] == '_') {
18+
if (current_balance == 0) {
19+
current_balance++;
20+
} else {
21+
current_balance--;
22+
}
23+
} else if (s[i] == '(') {
24+
current_balance++;
25+
} else {
26+
current_balance--;
27+
}
28+
total_cost += current_balance;
29+
}
30+
31+
cout << total_cost << "\n";
32+
}
33+
34+
int main() {
35+
int t;
36+
cin >> t;
37+
while (t--) {
38+
solve();
39+
}
40+
return 0;
41+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)