Skip to content

Commit 24df1a2

Browse files
committed
The 2022 ICPC Asia Nanjing Regional Contest
1 parent 6acf368 commit 24df1a2

File tree

5 files changed

+337
-0
lines changed

5 files changed

+337
-0
lines changed

QOJ/5414.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @file 5414.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2025-10-21
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define endl '\n'
14+
15+
#define maxn 2005
16+
17+
void solve(void) {
18+
int n, m, k;
19+
cin >> n >> m >> k;
20+
string s;
21+
cin >> s;
22+
23+
int xl = 1, xr = n, yl = 1, yr = m, dx = 0, dy = 0;
24+
for (int i = 0; i < (int)s.size(); i++) {
25+
if (s[i] == 'U') xl--, xr--, dx--;
26+
if (s[i] == 'D') xl++, xr++, dx++;
27+
if (s[i] == 'L') yl--, yr--, dy--;
28+
if (s[i] == 'R') yl++, yr++, dy++;
29+
if (xl < 1) xl = 1;
30+
if (xr > n) xr = n;
31+
if (yl < 1) yl = 1;
32+
if (yr > m) yr = m;
33+
}
34+
xl -= dx, xr -= dx, yl -= dy, yr -= dy;
35+
36+
if (xl > xr || yl > yr) return cout << (k == 0 ? n * m : 0) << endl, void();
37+
38+
int x0 = n + 5, y0 = m + 5;
39+
int x = x0, y = y0;
40+
vector<vector<int>> f(x0 + n + 5, vector<int>(y0 + m + 5, 0));
41+
f[x0][y0] = 1;
42+
for (int i = 0; i < (int)s.size(); i++) {
43+
if (s[i] == 'U') x++;
44+
if (s[i] == 'D') x--;
45+
if (s[i] == 'L') y++;
46+
if (s[i] == 'R') y--;
47+
f[x][y] = 1;
48+
}
49+
50+
for (int i = 0; i < (int)f.size(); i++)
51+
for (int j = 1; j < (int)f[0].size(); j++) f[i][j] += f[i][j - 1];
52+
for (int i = 1; i < (int)f.size(); i++)
53+
for (int j = 0; j < (int)f[0].size(); j++) f[i][j] += f[i - 1][j];
54+
55+
auto sum = [&](int xl, int xr, int yl, int yr) -> int {
56+
return f[xr][yr] - f[xl - 1][yr] - f[xr][yl - 1] + f[xl - 1][yl - 1];
57+
};
58+
59+
k = (xr - xl + 1) * (yr - yl + 1) - k;
60+
61+
int ans = 0;
62+
for (int i = 1; i <= n; i++)
63+
for (int j = 1; j <= m; j++) ans += (sum(xl - i + x0, xr - i + x0, yl - j + y0, yr - j + y0) == k);
64+
65+
cout << ans << endl;
66+
67+
return;
68+
}
69+
70+
int main() {
71+
ios::sync_with_stdio(false), cin.tie(nullptr);
72+
73+
int _ = 1;
74+
cin >> _;
75+
while (_--) solve();
76+
77+
return 0;
78+
}

QOJ/5418.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* @file 5418.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2025-10-21
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define endl '\n'
14+
15+
#define maxn 100005
16+
#define maxlgn 20
17+
18+
class STList {
19+
private:
20+
int f[maxlgn][maxn], lgLen[maxn];
21+
22+
public:
23+
void build(int n, int a[]) {
24+
for (int i = 2; i <= n; i++) lgLen[i] = lgLen[i >> 1] + 1;
25+
for (int i = 0; i < n; i++) f[0][i] = a[i];
26+
for (int t = 1; t <= lgLen[n]; t++)
27+
for (int i = 0; i + (1 << t) <= n; i++) f[t][i] = min(f[t - 1][i], f[t - 1][i + (1 << (t - 1))]);
28+
return;
29+
}
30+
int64_t getMin(int l, int r) {
31+
int t = lgLen[r - l + 1];
32+
return min(f[t][l], f[t][r - (1 << t) + 1]);
33+
}
34+
} STL;
35+
36+
int a[maxn], dep[maxn], fa[maxn][maxlgn], dfni[maxn];
37+
vector<vector<int>> graph, ngraph, rec;
38+
39+
int lca(int x, int y) {
40+
if (dep[x] < dep[y]) swap(x, y);
41+
for (int i = maxlgn - 1; ~i; i--)
42+
if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
43+
if (x == y) return x;
44+
for (int i = maxlgn - 1; ~i; i--)
45+
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
46+
return fa[x][0];
47+
}
48+
49+
int dfnt = 0;
50+
void dfs1(int p, int pre = 0) {
51+
rec[dep[p] = dep[pre] + 1].push_back(p), dfni[p] = ++dfnt, fa[p][0] = pre;
52+
for (int t = 1; t < maxlgn; t++) fa[p][t] = fa[fa[p][t - 1]][t - 1];
53+
for (auto q : graph[p])
54+
if (q != pre) dfs1(q, p);
55+
return;
56+
}
57+
58+
int64_t dfs2(int p, int tar) {
59+
if (ngraph[p].empty()) return STL.getMin(0, tar - 1);
60+
int64_t sum = 0;
61+
for (auto q : ngraph[p]) sum += dfs2(q, tar);
62+
return min(sum, STL.getMin(tar - dep[p], tar - 1));
63+
}
64+
65+
void solve(void) {
66+
int n;
67+
cin >> n;
68+
69+
for (int i = 0; i < n; i++) cin >> a[i];
70+
STL.build(n, a);
71+
72+
graph.clear(), graph.resize(n + 1);
73+
for (int i = 1, x, y; i < n; i++) cin >> x >> y, graph[x].push_back(y), graph[y].push_back(x);
74+
75+
rec.clear(), rec.resize(n + 1);
76+
dfnt = 0, dfs1(1);
77+
78+
int64_t ans = 0;
79+
80+
ngraph.clear(), ngraph.resize(n + 1);
81+
for (int d = 1; d <= n; d++) {
82+
if (rec[d].empty()) continue;
83+
84+
sort(rec[d].begin(), rec[d].end(), [&](int x, int y) -> bool { return dfni[x] < dfni[y]; });
85+
86+
vector<int> tmp;
87+
stack<int> S;
88+
int head = 1;
89+
for (auto x : rec[d]) {
90+
int t = lca(head, x);
91+
while (!S.empty() && dep[S.top()] >= dep[t])
92+
tmp.push_back(S.top()), ngraph[S.top()].push_back(head), head = S.top(), S.pop();
93+
if (head != t) tmp.push_back(t), ngraph[t].push_back(head), head = t;
94+
if (x != t) S.push(head), head = x;
95+
}
96+
97+
while (!S.empty()) tmp.push_back(S.top()), ngraph[S.top()].push_back(head), head = S.top(), S.pop();
98+
99+
ans += dfs2(1, d);
100+
101+
for (auto x : tmp) ngraph[x].clear();
102+
}
103+
104+
cout << ans << endl;
105+
106+
return;
107+
}
108+
109+
int main() {
110+
ios::sync_with_stdio(false), cin.tie(nullptr);
111+
112+
int _ = 1;
113+
cin >> _;
114+
while (_--) solve();
115+
116+
return 0;
117+
}

QOJ/5420.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @file 5420.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2025-10-21
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define endl '\n'
14+
15+
#define maxn 1000005
16+
17+
using pii = pair<int, int>;
18+
19+
int a[maxn], pre[maxn];
20+
21+
void solve(void) {
22+
int n;
23+
cin >> n;
24+
for (int i = 1; i <= n; i++) cin >> a[i];
25+
pre[0] = 1;
26+
for (int i = 1; i <= n; i++)
27+
if ((pre[i] = pre[i - 1] + (a[i] >= 0 ? +1 : -1)) <= 0) return cout << -1 << endl, void();
28+
29+
int cnt = pre[n], sum = 1 + n - (n + 1 - pre[n]) / 2, low = pre[n];
30+
pii ans = {sum, cnt};
31+
for (int i = n; i; i--) {
32+
low = min(low, pre[i]);
33+
if (a[i] == 0) cnt -= 2, sum--, low -= 2;
34+
if (low <= 0) break;
35+
if ((int64_t)ans.first * cnt < (int64_t)sum * ans.second) ans = {sum, cnt};
36+
}
37+
38+
int64_t g = gcd(ans.first, ans.second);
39+
cout << ans.first / g << ' ' << ans.second / g << endl;
40+
41+
return;
42+
}
43+
44+
int main() {
45+
ios::sync_with_stdio(false), cin.tie(nullptr);
46+
47+
int _ = 1;
48+
cin >> _;
49+
while (_--) solve();
50+
51+
return 0;
52+
}

QOJ/5422.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @file 5422.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2025-10-21
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define endl '\n'
14+
15+
void solve(void) {
16+
string s;
17+
cin >> s;
18+
vector<int> cnt(26);
19+
for (auto c : s) cnt[c - 'a']++;
20+
int ans = 0;
21+
for (int i = 0; i < 26; i++) ans = max(ans, cnt[i]);
22+
cout << s.size() - ans << endl;
23+
return;
24+
}
25+
26+
int main() {
27+
ios::sync_with_stdio(false), cin.tie(nullptr);
28+
29+
int _ = 1;
30+
cin >> _;
31+
while (_--) solve();
32+
33+
return 0;
34+
}

QOJ/5426.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @file 5426.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2025-10-21
5+
*
6+
* @copyright Copyright (c) 2025
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define endl '\n'
14+
15+
#define maxn 4005
16+
17+
using pii = pair<int, int>;
18+
19+
pii a[maxn];
20+
int f[maxn];
21+
22+
vector<int> graph[maxn];
23+
24+
void solve(void) {
25+
int n;
26+
cin >> n;
27+
for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second, a[i + n] = a[i];
28+
29+
int s = 2;
30+
while (a[s - 1].second == a[s].second) s++;
31+
32+
int ans = 0;
33+
for (int i = s, j; i < s + n; i = j + 1) {
34+
j = i;
35+
while (j + 1 < s + n && a[j].second == a[j + 1].second) j++;
36+
pii pl = a[i - 1], pr = a[j + 1];
37+
pii dl = {pl.first - a[i].first, pl.second - a[i].second}, dr = {pr.first - a[j].first, pr.second - a[j].second};
38+
if (dl.second < 0 || dr.second < 0) continue;
39+
if (a[i].first > a[j].first) continue;
40+
if ((int64_t)dr.first * dl.second - (int64_t)dr.second * dl.first <= 0 && i == j) continue;
41+
ans++;
42+
}
43+
44+
cout << ans << endl;
45+
46+
return;
47+
}
48+
49+
int main() {
50+
ios::sync_with_stdio(false), cin.tie(nullptr);
51+
52+
int _ = 1;
53+
while (_--) solve();
54+
55+
return 0;
56+
}

0 commit comments

Comments
 (0)