0% found this document useful (0 votes)
9 views6 pages

Code

Uploaded by

Prince Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Code

Uploaded by

Prince Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

9/12/25, 3:43 PM Code

1 #include <bits/stdc++.h>
2 using namespace std;
3
4
5 /*
6
7 You are given an array A of length N (which may contain both positive and negative
integers), and two integers K and L.
8
9 You must choose two contiguous fragments (subarrays) of lengths K and L. The two
fragments may overlap partially, fully, or not at all.
10
11 If the fragments do not overlap, the total score is simply the sum of all elements
in both fragments.
12
13 If the fragments do overlap, then:
14
15 Each element that lies outside the overlap is counted normally.
16
17 Each element that lies inside the overlap is counted only once, and its sign is
flipped (positive becomes negative, negative becomes positive).
18
19 Your task is to choose the two fragments in such a way that the resulting total
score is maximized.
20
21 Input
22
23 An integer N — the length of the array.
24
25 An array A of N integers.
26
27 Two integers K and L — the lengths of the two fragments.
28
29 */
30 struct SegTree {
31 int n;
32 vector<long long> st;
33
34 SegTree(const vector<long long>& arr) {
35 n = [Link]();
36 [Link](4 * n, LLONG_MIN);
37 build(1, 0, n - 1, arr);
38 }
39
40 void build(int p, int l, int r, const vector<long long>& arr) {
41 if (l == r) {
42 st[p] = arr[l];
43 return;
44 }
45 int m = (l + r) / 2;
46 build(p * 2, l, m, arr);
47 build(p * 2 + 1, m + 1, r, arr);
48 st[p] = max(st[p * 2], st[p * 2 + 1]);
49 }
50
51 long long query(int p, int l, int r, int i, int j) {
52 if (i > r || j < l) return LLONG_MIN;
53 if (i <= l && r <= j) return st[p];
54 int m = (l + r) / 2;

[Link] 1/6
9/12/25, 3:43 PM Code
55 return max(query(p * 2, l, m, i, j),
56 query(p * 2 + 1, m + 1, r, i, j));
57 }
58
59 long long query(int l, int r) {
60 if (l > r) return LLONG_MIN;
61 return query(1, 0, n - 1, l, r);
62 }
63 };
64
65 class Solution {
66 public:
67 long long solveCase(vector<int>& A, int K, int L) {
68 int n = [Link]();
69
70 // prefix sums
71 vector<long long> prefix(n + 1, 0);
72 for (int i = 0; i < n; i++) prefix[i + 1] = prefix[i] + A[i];
73
74 // all K sums and L sums
75 vector<long long> sumK(n - K + 1), sumL(n - L + 1);
76 for (int i = 0; i + K <= n; i++) sumK[i] = prefix[i + K] - prefix[i];
77 for (int j = 0; j + L <= n; j++) sumL[j] = prefix[j + L] - prefix[j];
78
79 // prepare arrays for segment tree
80 vector<long long> arr1(n - K + 1), arr2(n - K + 1);
81 for (int i = 0; i + K <= n; i++) {
82 arr1[i] = sumK[i] - 3 * prefix[i + K]; // case 1
83 arr2[i] = sumK[i]; // case 2
84 }
85
86 SegTree st1(arr1), st2(arr2);
87 long long ans = LLONG_MIN;
88
89 // try all positions for L segment
90 for (int j = 0; j + L <= n; j++) {
91
92 // Case 1: K ends before or at L's end
93 int iLow = max(0, j - K + 1);
94 int iHigh = min(j, n - K);
95 if (iLow <= iHigh) {
96 long long best = [Link](iLow, iHigh);
97 ans = max(ans, sumL[j] + best + 3 * prefix[j]);
98 }
99 // Case 2: L ends before K
100 int iLow2 = j + 1;
101 int iHigh2 = min(j + L - 1, n - K);
102 if (iLow2 <= iHigh2) {
103 long long best = [Link](iLow2, iHigh2);
104 ans = max(ans, sumL[j] + best + 3 * (prefix[j] - prefix[j + L]));
105 }
106 }
107
108 return ans;
109 }
110
111 long long maxSum(vector<int>& A, int K, int L) {
112 // both orientations: K before L, or L before K
113 // length K comes firsst then L
114 // legnth L comes first then k
115 return max(solveCase(A, K, L), solveCase(A, L, K));

[Link] 2/6
9/12/25, 3:43 PM Code
116 }
117 };
118
119 //----------------------------------------------------------------------
120
121 #include <bits/stdc++.h>
122 using namespace std;
123
124 class Solution {
125 private:
126 struct Square { int r, c, s; };
127 static bool overlap(const Square& a, const Square& b) {
128 return !(a.r + a.s <= b.r || b.r + b.s <= a.r ||
129 a.c + a.s <= b.c || b.c + b.s <= a.c);
130 }
131
132 public:
133 int largestTwoSquares(vector<vector<bool>>& A) {
134 if ([Link]() || A[0].empty()) return 0;
135 int n = [Link](), m = A[0].size();
136
137 // DP
138 vector<vector<int>> dp(n, vector<int>(m,0));
139 for (int i=0;i<n;i++){
140 for (int j=0;j<m;j++){
141 if (A[i][j]) {
142 if (i==0 || j==0) dp[i][j]=1;
143 else dp[i][j] = 1 + min({dp[i-1][j], dp[i][j-1], dp[i-1][j-1]});
144 }
145 }
146 }
147
148 vector<Square> squares;
149 for (int i=0;i<n;i++){
150 for (int j=0;j<m;j++){
151 for (int s=1; s<=dp[i][j]; ++s){
152 squares.push_back({i-s+1, j-s+1, s});
153 }
154 }
155 }
156
157 sort([Link](), [Link](), [](const Square& a, const Square& b){
158 return a.s > b.s;
159 });
160
161 for (size_t idx = 0; idx < [Link](); ++idx) {
162 int s = squares[idx].s;
163 for (size_t j = idx+1; j < [Link]() && squares[j].s == s; ++j) {
164 if (!overlap(squares[idx], squares[j])) return s*s;
165 }
166 }
167 return 0;
168 }
169 };
170
171
172 //----------------------------------------------------------------------------------
----
173
174
175 #include <bits/stdc++.h>

[Link] 3/6
9/12/25, 3:43 PM Code
176 using namespace std;
177
178 vector<string> generateEmails(vector<string>& names, string& company) {
179 unordered_map<string, int> emailcount;
180 vector<string> result;
181
182 for (auto &s : names) {
183 stringstream ss(s);
184 string word;
185 vector<string> parts;
186 while (ss >> word) parts.push_back(word);
187
188 string firstname = parts[0];
189 string lastname = [Link]();
190
191 // remove hyphens from lastname
192 [Link](remove([Link](), [Link](), '-'),
[Link]());
193
194 // lowercase
195 transform([Link](), [Link](), [Link](), ::tolower);
196 transform([Link](), [Link](), [Link](), ::tolower);
197
198 // truncate lastname to 8 chars
199 string lastname_short = [Link](0, min(8, (int)[Link]()));
200
201 string baseemail = firstname + "." + lastname_short;
202 string email = baseemail;
203 if (emailcount[baseemail] > 0) {
204 email += to_string(emailcount[baseemail] + 1);
205 }
206 email += "@" + company + ".com";
207 emailcount[baseemail]++;
208 result.push_back(email);
209 }
210
211 return result;
212 }
213
214 void solve() {
215 int n;
216 cin >> n;
217 [Link]();
218 vector<string> names(n);
219 for (int i = 0; i < n; i++) getline(cin, names[i]);
220
221 string company;
222 cin >> company;
223 vector<string> emails = generateEmails(names, company);
224 for (int i = 0; i < n; i++) {
225 cout << names[i] << " <" << emails[i] << ">";
226 if (i != n - 1) cout << "; ";
227 }
228 cout << "\n";
229 }
230
231 int main() {
232 int t;
233 cin >> t;
234 while (t--) solve();
235 }

[Link] 4/6
9/12/25, 3:43 PM Code
236
237 //---------------------------------------------------
238
239
240
241 class Solution {
242 public:
243 long long fuel = 0; // store total fuel
244 int seats = 5; // car capacity
245
246 // DFS function: returns how many people in this subtree
247 long long dfs(int node, int parent, vector<vector<int>>& adj) {
248 long long people = 1; // each node has 1 person (its house)
249
250 // explore children
251 for (auto& nei : adj[node]) {
252 if (nei == parent) continue; // don't go back to parent
253
254 // find people in child subtree
255 long long subPeople = dfs(nei, node, adj);
256
257 // cars needed to move this subtree to current node
258 long long cars = (subPeople + seats - 1) / seats; // ceil(subPeople/5)
259
260 fuel += cars; // each car passes 1 road → add fuel cost
261
262 // add these people to current node
263 people += subPeople;
264 }
265 return people;
266 }
267
268 long long minimumFuelCost(int n, vector<int>& A, vector<int>& B) {
269 vector<vector<int>> adj(n + 1);
270 for (int i = 0; i < n; i++) {
271 adj[A[i]].push_back(B[i]);
272 adj[B[i]].push_back(A[i]);
273 }
274 dfs(0, -1, adj); // start DFS from office
275 return fuel;
276 }
277 };
278
279
280 //--------------------------------------------------
281
282
283 #include <bits/stdc++.h>
284 using namespace std;
285
286 int solution(vector<int>& A) {
287 int n = [Link]();
288 unordered_map<int,int> freq;
289 int L = 0, maxLen = 0;
290
291 for (int R = 0; R < n; R++) {
292 freq[A[R]]++;
293 while ([Link]() > 2) { // shrink left
294 freq[A[L]]--;
295 if (freq[A[L]] == 0) [Link](A[L]);
296 L++;

[Link] 5/6
9/12/25, 3:43 PM Code
297 }
298 maxLen = max(maxLen, R - L + 1);
299 }
300 return maxLen;
301 }
302
303 //------------------------------
304
305
306
307 #include <bits/stdc++.h>
308 using namespace std;
309
310 int solution(string &S) {
311 int n = [Link]();
312 // remove leading zeros
313 int start = 0;
314 while (start < n && S[start] == '0') start++;
315 if (start == n) return 0; // number is 0
316
317 int ops = 0;
318 for (int i = start + 1; i < n; i++) {
319 if (S[i] == '0') ops += 1; // divide
320 else ops += 2; // subtract + divide
321 }
322 // first '1' takes 1 operation (subtract to 0 eventually)
323 ops += 1;
324 return ops;
325 }
326
327 int main() {
328 string S;
329 cin >> S;
330 cout << solution(S) << "\n";
331 }
332
333

[Link] 6/6

You might also like