Skip to content

Commit 2196231

Browse files
authored
Merge branch 'opencodeiiita:main' into main
2 parents 365f960 + 7c850b1 commit 2196231

67 files changed

Lines changed: 4030 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
9+
[https://www.programiz.com/cpp-programming/stack]
10+
11+
[https://www.youtube.com/watch?v=WK97Pj0wa7A&list=PL1w8k37X_6L9NXrP1D31hDTKcdAPIL0cG&index=10]
12+
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: 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)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 🧮 Accumulator Apex
2+
3+
**Problem Link:**
4+
🔗 https://codeforces.com/problemset/problem/1912/A
5+
6+
**Time Limit:** 3 seconds
7+
**Memory Limit:** 1024 MB
8+
9+
---
10+
11+
## 📘 Problem Statement
12+
13+
Allyn is playing a new strategy game called **Accumulator Apex**.
14+
15+
In this game:
16+
17+
- Allyn is given an initial integer value **x**, called the **accumulator**.
18+
- There are **k** lists of integers.
19+
- Allyn can make multiple turns.
20+
21+
### 🔁 Game Rules
22+
23+
On each turn, Allyn can:
24+
25+
- Choose **any non-empty list**
26+
- Withdraw the **leftmost element** from that list
27+
- Add it to the accumulator **x**, **only if** the resulting value of **x** is **non-negative**
28+
29+
Allyn may **end the game at any moment**.
30+
31+
---
32+
33+
## 🎯 Objective
34+
35+
Find the **maximum possible value** of the accumulator **x** that Allyn can achieve.
36+
37+
---
38+
39+
### 📥 Input Format
40+
41+
The first line contains two integers **x** and **k**:
42+
43+
- $0 \le x \le 10^9$
44+
- $1 \le k \le 10^5$
45+
46+
The next **k** lines describe the lists:
47+
48+
- Each line starts with an integer **lᵢ** $(lᵢ \ge 1)$
49+
- Followed by **lᵢ** integers representing the list elements (from left to right)
50+
51+
---
52+
53+
### 🔢 Constraints
54+
55+
- Each element in the lists satisfies:
56+
$|a_{ij}| \le 10^9$
57+
58+
- The total number of elements across all lists does not exceed:
59+
$10^5$
60+
61+
---
62+
63+
## 📤 Output Format
64+
65+
- Print a single integer — the **maximum possible value** of the accumulator **x**.
66+
67+
---
68+
69+
## 📝 Notes
70+
71+
- Elements must be taken **from the left end** of a list.
72+
- You can switch between lists at any time.
73+
- You are **not required** to consume all elements.
74+
- The accumulator value must **never become negative**.
75+
76+
---
77+
78+
## ✅ Example (Conceptual)
79+
80+
If at some point adding an element makes the accumulator negative, that move is **not allowed**.
81+
82+
The optimal strategy may involve:
83+
- Taking positive elements early
84+
- Skipping lists that block progress due to large negative prefixes
85+
86+
---
87+
88+
## 🏁 End of Problem
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+
*/

0 commit comments

Comments
 (0)