Skip to content

Commit 4b1f7be

Browse files
committed
CodeForces practice.
1 parent d881a74 commit 4b1f7be

8 files changed

+327
-1
lines changed

CodeForces/A_Line_Breaks.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
int N, M;
9+
cin >> N >> M;
10+
int count = 0;
11+
int ans = 0;
12+
bool ok = true;
13+
for (int i = 0; i < N; i++) {
14+
string S;
15+
cin >> S;
16+
if (ok && count + S.size() <= M) {
17+
count += S.size();
18+
ans++;
19+
} else {
20+
ok = false;
21+
}
22+
}
23+
cout << ans << endl;
24+
}
25+
26+
int main() {
27+
int t;
28+
cin >> t;
29+
30+
while (t--) {
31+
testCase();
32+
}
33+
34+
cout << flush;
35+
return 0;
36+
}

CodeForces/B_Transfusion.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
int N;
9+
cin >> N;
10+
vector<int> A(N);
11+
vector<ll> sum(2);
12+
vector<ll> count(2);
13+
for (int i = 0; i < N; i++) {
14+
cin >> A[i];
15+
count[i%2]++;
16+
sum[i%2] += A[i];
17+
}
18+
cout << ((sum[0] % count[0] == 0
19+
&& sum[1] % count[1] == 0
20+
&& sum[0] / count[0] == sum[1] / count[1]) ? "YES" : "NO") << endl;
21+
}
22+
23+
int main() {
24+
int t;
25+
cin >> t;
26+
27+
while (t--) {
28+
testCase();
29+
}
30+
31+
cout << flush;
32+
return 0;
33+
}

CodeForces/C_Uninteresting_Number.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
// 1->1
9+
// 2->4
10+
// 3->9
11+
string S;
12+
cin >> S;
13+
int N = S.size();
14+
ll sum = 0;
15+
int twos = 0, threes = 0;
16+
for (int i = 0; i < N; i++) {
17+
sum += S[i] - '0';
18+
if (S[i] == '2') twos++;
19+
else if (S[i] == '3') threes++;
20+
}
21+
bool ok = false;
22+
for (int i = 0; i <= min(9, twos); i++) {
23+
for (int j = 0; j <= min(9, threes); j++) {
24+
ok |= (sum + i * 2 + j * 6) % 9 == 0;
25+
}
26+
}
27+
cout << (ok ? "YES" : "NO") << endl;
28+
}
29+
30+
int main() {
31+
int t;
32+
cin >> t;
33+
34+
while (t--) {
35+
testCase();
36+
}
37+
38+
cout << flush;
39+
return 0;
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
string S;
9+
cin >> S;
10+
int N = S.size();
11+
for (int i = 0; i < N; i++) {
12+
int cur = S[i] - '0';
13+
int mx_id = i;
14+
int mx = cur;
15+
for (int c = 1; i+c < N && (c - 1) < 9 - cur; c++) {
16+
int next = S[i+c] - '0';
17+
if (next - c > mx) {
18+
mx = next - c;
19+
mx_id = i + c;
20+
}
21+
}
22+
for (int c = mx_id; c > i; c--) {
23+
S[c]--;
24+
swap(S[c], S[c-1]);
25+
}
26+
}
27+
cout << S << endl;
28+
}
29+
30+
int main() {
31+
int t;
32+
cin >> t;
33+
34+
while (t--) {
35+
testCase();
36+
}
37+
38+
cout << flush;
39+
return 0;
40+
}

CodeForces/G_Tree_Destruction.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
vector<vector<int>> adj;
8+
vector<int> dp, dp2;
9+
10+
void dfs1(int u, int p) {
11+
dp[u] = (int)adj[u].size() - (p != -1);
12+
for (auto v : adj[u]) {
13+
if (v == p) continue;
14+
dfs1(v, u);
15+
dp[u] = max(dp[u], (int)adj[u].size() - 1 - (p != -1) + dp[v]);
16+
}
17+
}
18+
19+
void dfs2(int u, int p) {
20+
dp2[u] = dp[u] + (p != -1);
21+
vector<int> sorted;
22+
for (auto v : adj[u]) {
23+
if (v == p) continue;
24+
dfs2(v, u);
25+
sorted.push_back(dp[v]);
26+
}
27+
sort(sorted.rbegin(), sorted.rend());
28+
if (sorted.size() >= 2) {
29+
dp2[u] = max(dp2[u], sorted[0] + sorted[1] + (int)adj[u].size() - 2);
30+
} else if (sorted.size() == 1) {
31+
dp2[u] = max(dp2[u], sorted[0] + (int)adj[u].size() - 1);
32+
}
33+
}
34+
35+
void testCase() {
36+
int N;
37+
cin >> N;
38+
adj.clear();
39+
dp.clear();
40+
dp2.clear();
41+
adj.resize(N);
42+
dp.resize(N);
43+
dp2.resize(N);
44+
for (int i = 0; i < N - 1; i++) {
45+
int u, v;
46+
cin >> u >> v;
47+
u--; v--;
48+
adj[u].push_back(v);
49+
adj[v].push_back(u);
50+
}
51+
dfs1(0, -1);
52+
dfs2(0, -1);
53+
cout << *max_element(dp2.begin(), dp2.end()) << endl;
54+
}
55+
56+
int main() {
57+
int t;
58+
cin >> t;
59+
60+
while (t--) {
61+
testCase();
62+
}
63+
64+
cout << flush;
65+
return 0;
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
inline void update(int& a, int b) {
8+
if (b < a) a = b;
9+
}
10+
11+
void testCase() {
12+
string A, B, C;
13+
cin >> A >> B >> C;
14+
int N = A.size();
15+
int M = B.size();
16+
A = "@" + A;
17+
B = "@" + B;
18+
vector<vector<int>> dp(N + 1, vector<int>(M + 1, 1e9));
19+
dp[0][0] = 0;
20+
for (int i = 0; i <= N; i++) {
21+
for (int j = 0; j <= M; j++) {
22+
if (i == 0 && j == 0) continue;
23+
if (i) update(dp[i][j], dp[i-1][j] + 1);
24+
if (j) update(dp[i][j], dp[i][j-1] + 1);
25+
if (i && A[i] == C[i + j - 1]) {
26+
update(dp[i][j], dp[i-1][j]);
27+
}
28+
if (j && B[j] == C[i + j - 1]) {
29+
update(dp[i][j], dp[i][j-1]);
30+
}
31+
}
32+
}
33+
cout << dp[N][M] << endl;
34+
}
35+
36+
int main() {
37+
int t;
38+
cin >> t;
39+
40+
while (t--) {
41+
testCase();
42+
}
43+
44+
cout << flush;
45+
return 0;
46+
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Competitive Programming Problems and Solutions
22

3-
![](https://img.shields.io/static/v1?label=Solutions&message=1381&color=brightgreen)
3+
![](https://img.shields.io/static/v1?label=Solutions&message=1388&color=brightgreen)
44

55
This repository is meant as a public place to store my competitive programming solutions to various competitive programming problems on sites such as:
66
- [CodeForces](https://codeforces.com/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
template<typename T>
8+
T gcd(T a, T b) {
9+
return (a == 0) ? b : gcd(b % a, a);
10+
}
11+
12+
template<class T>
13+
struct SparseTable {
14+
vector<vector<T>> jmp;
15+
SparseTable(const vector<T>& V) : jmp(1, V) {
16+
for (int pw = 1, k = 1; pw * 2 <= V.size(); pw *= 2, ++k) {
17+
jmp.emplace_back(V.size() - pw * 2 + 1);
18+
for (int j = 0; j < jmp[k].size(); j++)
19+
jmp[k][j] = gcd(jmp[k - 1][j], jmp[k - 1][j + pw]);
20+
}
21+
}
22+
T query(int a, int b) {
23+
assert(a < b); // or return inf if a == b
24+
int dep = 31 - __builtin_clz(b - a);
25+
return gcd(jmp[dep][a], jmp[dep][b - (1 << dep)]);
26+
}
27+
};
28+
29+
void testCase() {
30+
int N, Q;
31+
cin >> N >> Q;
32+
vector<int> A(N);
33+
for (int i = 0; i < N; i++) {
34+
cin >> A[i];
35+
}
36+
vector<int> diff(N);
37+
for (int i = 1; i < N; i++) {
38+
diff[i] = abs(A[i] - A[i-1]);
39+
}
40+
SparseTable st(diff);
41+
for (int i = 0; i < Q; i++) {
42+
int L, R;
43+
cin >> L >> R;
44+
L--; R--;
45+
if (L == R) {
46+
cout << 0 << " ";
47+
continue;
48+
}
49+
int g = st.query(L + 1, R + 1);
50+
cout << g << " ";
51+
}
52+
cout << endl;
53+
}
54+
55+
int main() {
56+
int t;
57+
cin >> t;
58+
59+
while (t--) {
60+
testCase();
61+
}
62+
63+
cout << flush;
64+
return 0;
65+
}

0 commit comments

Comments
 (0)