Skip to content

Commit a979076

Browse files
committed
Codeforces: Codeforces Round 1022 (Div. 2)
1 parent 955f8b2 commit a979076

6 files changed

Lines changed: 398 additions & 0 deletions

File tree

Codeforces/2108A.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @file 2108A.cpp
3+
* @author Macesuted (i@macesuted.moe)
4+
* @date 2025-05-07
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#ifndef LOCAL
14+
#define endl '\n'
15+
#endif
16+
17+
bool mem1;
18+
19+
void solve(void) {
20+
int n;
21+
cin >> n;
22+
int64_t sum = 0;
23+
for (int i = 1; i <= n; i++) sum += abs((n - i + 1) - i);
24+
cout << 1 + sum / 2 << endl;
25+
return;
26+
}
27+
28+
bool mem2;
29+
30+
int main() {
31+
ios::sync_with_stdio(false), cin.tie(nullptr);
32+
#ifdef LOCAL
33+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
34+
#endif
35+
36+
int _ = 1;
37+
cin >> _;
38+
while (_--) solve();
39+
40+
#ifdef LOCAL
41+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
42+
#endif
43+
return 0;
44+
}

Codeforces/2108B.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @file 2108B.cpp
3+
* @author Macesuted (i@macesuted.moe)
4+
* @date 2025-05-07
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#ifndef LOCAL
14+
#define endl '\n'
15+
#endif
16+
17+
bool mem1;
18+
19+
void solve(void) {
20+
int n, x;
21+
cin >> n >> x;
22+
if (x == 0) {
23+
if (n == 1) return cout << -1 << endl, void();
24+
if (n % 2 == 0) return cout << n << endl, void();
25+
return cout << n + 3 << endl, void();
26+
}
27+
if (x == 1) {
28+
if (n % 2 == 0) return cout << n + 3 << endl, void();
29+
return cout << n << endl, void();
30+
}
31+
int b = min(n, __builtin_popcount(x));
32+
if (b > 1 && (n - b) % 2 == 1) b--;
33+
cout << n - b + x + ((n - b) % 2 == 1) << endl;
34+
return;
35+
}
36+
37+
bool mem2;
38+
39+
int main() {
40+
ios::sync_with_stdio(false), cin.tie(nullptr);
41+
#ifdef LOCAL
42+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
43+
#endif
44+
45+
int _ = 1;
46+
cin >> _;
47+
while (_--) solve();
48+
49+
#ifdef LOCAL
50+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
51+
#endif
52+
return 0;
53+
}

Codeforces/2108C.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @file 2108C.cpp
3+
* @author Macesuted (i@macesuted.moe)
4+
* @date 2025-05-07
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#ifndef LOCAL
14+
#define endl '\n'
15+
#endif
16+
17+
bool mem1;
18+
19+
#define maxn 200005
20+
21+
int a[maxn], fa[maxn], id[maxn];
22+
bool mark[maxn];
23+
24+
int getfa(int p) { return fa[p] == p ? p : fa[p] = getfa(fa[p]); }
25+
26+
void merge(int x, int y) {
27+
x = getfa(x), y = getfa(y);
28+
if (x != y) fa[x] = y, mark[y] |= mark[x];
29+
return;
30+
}
31+
32+
void solve(void) {
33+
int n;
34+
cin >> n;
35+
for (int i = 1; i <= n; i++) cin >> a[i], id[i] = fa[i] = i, mark[i] = false;
36+
sort(id + 1, id + n + 1, [&](int x, int y) { return a[x] > a[y]; });
37+
38+
int ans = 0;
39+
for (int l = 1, r; l <= n; l = r + 1) {
40+
r = l;
41+
while (r + 1 <= n && a[id[r + 1]] == a[id[l]]) r++;
42+
43+
for (int i = l; i <= r; i++) {
44+
int p = id[i];
45+
if (1 < p && a[p - 1] >= a[p]) merge(p - 1, p);
46+
if (p < n && a[p] <= a[p + 1]) merge(p, p + 1);
47+
}
48+
49+
for (int i = l; i <= r; i++) {
50+
int p = getfa(id[i]);
51+
if (!mark[p]) mark[p] = true, ans++;
52+
}
53+
}
54+
55+
cout << ans << endl;
56+
57+
return;
58+
}
59+
60+
bool mem2;
61+
62+
int main() {
63+
ios::sync_with_stdio(false), cin.tie(nullptr);
64+
#ifdef LOCAL
65+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
66+
#endif
67+
68+
int _ = 1;
69+
cin >> _;
70+
while (_--) solve();
71+
72+
#ifdef LOCAL
73+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
74+
#endif
75+
return 0;
76+
}

Codeforces/2108D.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @file 2108D.cpp
3+
* @author Macesuted (i@macesuted.moe)
4+
* @date 2025-05-07
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
bool mem1;
14+
15+
int query(int p) {
16+
cout << "? " << p << endl;
17+
int x;
18+
cin >> x;
19+
return x;
20+
}
21+
22+
void solve(void) {
23+
int n, k;
24+
cin >> n >> k;
25+
26+
if (n == 2 * k) return cout << "! " << k << ' ' << k << endl, void();
27+
28+
int xl = k, xr = n - k + 1;
29+
for (int i = 1; i <= k && xl + 1 < xr; i++) {
30+
int l = xl / k, r = xr / k - 1;
31+
while (i + l * k > xl) l--;
32+
while (i + r * k < xr) r++;
33+
34+
int v = query(i + l * k);
35+
if (query(i + r * k) == v) continue;
36+
37+
while (l + 1 < r) {
38+
int m = (l + r) >> 1;
39+
(query(i + m * k) == v ? l : r) = m;
40+
}
41+
xl = max(xl, i + l * k), xr = min(xr, i + r * k);
42+
}
43+
44+
if (xl + 1 != xr) return cout << "! -1" << endl, void();
45+
46+
cout << "! " << xl << ' ' << n - xr + 1 << endl;
47+
48+
return;
49+
}
50+
51+
bool mem2;
52+
53+
int main() {
54+
ios::sync_with_stdio(false), cin.tie(nullptr);
55+
#ifdef LOCAL
56+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
57+
#endif
58+
59+
int _ = 1;
60+
cin >> _;
61+
while (_--) solve();
62+
63+
#ifdef LOCAL
64+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
65+
#endif
66+
return 0;
67+
}

Codeforces/2108E.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @file 2108E.cpp
3+
* @author Macesuted (i@macesuted.moe)
4+
* @date 2025-05-07
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#ifndef LOCAL
14+
#define endl '\n'
15+
#endif
16+
17+
bool mem1;
18+
19+
#define maxn 200005
20+
21+
using pii = pair<int, int>;
22+
23+
vector<vector<int>> graph;
24+
int col[maxn], fa[maxn], dep[maxn], siz[maxn], dfn[maxn], id[maxn];
25+
int n;
26+
27+
int dfnt;
28+
void dfs(int p) {
29+
id[dfn[p] = ++dfnt] = p, siz[p] = 1;
30+
for (auto q : graph[p]) {
31+
if (q == fa[p]) continue;
32+
fa[q] = p, dep[q] = dep[p] + 1;
33+
dfs(q);
34+
siz[p] += siz[q];
35+
}
36+
return;
37+
}
38+
int getRoot(int p) {
39+
for (auto q : graph[p])
40+
if (q != fa[p] && 2 * siz[q] > n) return getRoot(q);
41+
return p;
42+
}
43+
44+
void solve(void) {
45+
cin >> n;
46+
47+
graph.clear(), graph.resize(n + 1);
48+
for (int i = 1, x, y; i < n; i++) cin >> x >> y, graph[x].push_back(y), graph[y].push_back(x);
49+
50+
fa[1] = -1, dep[1] = 0, dfnt = 0, dfs(1);
51+
int root = getRoot(1);
52+
fa[root] = -1, dep[root] = 0, dfnt = 0, dfs(root);
53+
54+
int x = -1;
55+
for (int i = 1; i <= n; i++)
56+
if (i != root && (x == -1 || siz[i] + dep[i] < siz[x] + dep[x])) x = i;
57+
58+
cout << x << ' ' << fa[x] << endl;
59+
60+
for (int i = 1; i <= n; i++) col[i] = 0;
61+
vector<int> nodes;
62+
for (int i = 1; i <= n; i++)
63+
if (id[i] != x) nodes.push_back(id[i]);
64+
for (int i = 0, j = nodes.size() / 2; j < (int)nodes.size(); i++, j++) col[nodes[i]] = col[nodes[j]] = i + 1;
65+
if (x < fa[x]) swap(col[x], col[fa[x]]);
66+
67+
for (int i = 1; i <= n; i++) cout << col[i] << ' ';
68+
cout << endl;
69+
70+
return;
71+
}
72+
73+
bool mem2;
74+
75+
int main() {
76+
ios::sync_with_stdio(false), cin.tie(nullptr);
77+
#ifdef LOCAL
78+
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
79+
#endif
80+
81+
int _ = 1;
82+
cin >> _;
83+
while (_--) solve();
84+
85+
#ifdef LOCAL
86+
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
87+
#endif
88+
return 0;
89+
}

0 commit comments

Comments
 (0)