VP 记录 - 2023 ICPC 亚洲区域赛 (南京)

比赛链接

进度: 7 / 13

题目概览

题号 1标题 2做法
ACool, It’s Yesterday Four Times More
*BIntersection over Union
CPrimitive Root
*DRed Black Tree
*EExtending Distance
FEquivalent Rewriting
GKnapsack
*HPuzzle: Question Mark
ICounter
*JSuffix Structure
*KGrand Finale
LElevator
MTrapping Rain Water

英文题面

中文题面

官方题解

A - Cool, It’s Yesterday Four Times More

解题思路

复杂度

代码参考

Show code

A.cppview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
template <class Tp>
using vec = vector<Tp>;
template <class... Ts>
void dec(Ts &...x) {
((--x), ...);
}
template <class... Ts>
void inc(Ts &...x) {
((++x), ...);
}
void solve(int t_ = 0) {
int n, m;
cin >> n >> m;
vec<vec<char>> ma(n + 2, vec<char>(m + 2, 'O'));
vec<vec<char>> ret;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) cin >> ma[i][j];
vec<vec<int>> vis(n + 2, vec<int>(m + 2, 0));
const vec<pair<int, int>> fx = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int sx, sy, ans = 0, sz = 0;
auto dfs = [&](auto dfs, int x, int y) -> void {
if (ma[x][y] == 'O' || vis[x][y]) return;
vis[x][y] = 1, sz += 1;
for (auto [xx, yy] : fx) dfs(dfs, x + xx, y + yy);
vec<vec<char>> ma1(n + 2, vec<char>(m + 2, '.'));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
if (i + x - sx < 1 || j + y - sy < 1 || i + x - sx > n ||
j + y - sy > m)
ma1[i][j] = 'O';
else ma1[i][j] = ma[i + x - sx][j + y - sy];
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (ma1[i][j] == 'O') ret[i][j] = 'O';
};
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (ma[i][j] == '.' && !vis[i][j]) {
sz = 0, sx = i, sy = j,
ret = vec<vec<char>>(n + 2, vec<char>(m + 2, '.'));
dfs(dfs, i, j);
int rt = 0;
for (int a = 1; a <= n; ++a)
for (int b = 1; b <= m; ++b) { rt += (ret[a][b] == 'O'); }
if (rt == n * m - 1) ans += sz;
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cerr << fixed << setprecision(6);
int i_ = 0;
int t_ = 0;
cin >> t_;
for (i_ = 0; i_ < t_; ++i_) solve(i_);
return 0;
}

B - Intersection over Union

解题思路

复杂度

代码参考

Show code

C - Primitive Root

解题思路

复杂度

代码参考

Show code

C.cppview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using i128 = __int128_t;
template <class... Ts>
void dec(Ts &...x) {
((--x), ...);
}
template <class... Ts>
void inc(Ts &...x) {
((++x), ...);
}
void solve(int t_ = 0) {
i64 p, m;
cin >> p >> m;
if (p == 2) {
cout << m / 2 + 1 << '\n';
return;
}
i64 k = max(i64(0), m / p - 2), ans = k;
for (i128 i = 0; i <= 10; ++i) ans += (((k + i) * p + 1) ^ (p - 1)) <= m;
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cerr << fixed << setprecision(6);
int i_ = 0;
int t_ = 0;
cin >> t_;
for (i_ = 0; i_ < t_; ++i_) solve(i_);
return 0;
}

D - Red Black Tree

解题思路

复杂度

代码参考

Show code

E - Extending Distance

解题思路

复杂度

代码参考

Show code

F - Equivalent Rewriting

解题思路

复杂度

代码参考

Show code

F.cppview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <bits/stdc++.h>
using namespace std;
template <class... Ts>
void dec(Ts &...x) {
((--x), ...);
}
template <class... Ts>
void inc(Ts &...x) {
((++x), ...);
}
void solve(int t_ = 0) {
int n, m;
cin >> n >> m;
vector<vector<int>> op(n);
vector<int> pos(m + 1, -1);
for (int i = 0; i < n; ++i) {
int sz;
cin >> sz;
vector<int> now(sz);
for (int j = 0; j < sz; ++j) {
int x;
cin >> x;
now[j] = x;
pos[x] = i;
}
op[i] = now;
}
vector<vector<int>> e(n);
vector<int> in(n, 0);
for (int i = 0; i < n; ++i) {
e[i].clear();
for (int j = 0; j < op[i].size(); ++j) {
if (i != pos[op[i][j]]) {
e[i].push_back(pos[op[i][j]]);
in[pos[op[i][j]]]++;
}
}
}
priority_queue<int> q;
for (int i = 0; i < n; ++i) {
if (!in[i]) { q.push(i); }
}
int flag = 0, cnt = 0;
vector<int> ans(n, 0);
while (!q.empty()) {
if (q.size() > 1) flag = 1;
int x = q.top();
q.pop();
ans[cnt++] = x;
for (auto y : e[x]) {
in[y]--;
if (!in[y]) q.push(y);
}
}
int i = 0;
for (; i < n; ++i) {
if (ans[i] != i) break;
}
if (!flag || i == n) {
cout << "No\n";
return;
}
cout << "Yes\n";
cout << ans[0] + 1;
for (i = 1; i < n; ++i) cout << " " << ans[i] + 1;
cout << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cerr << fixed << setprecision(6);
int i_ = 0;
int t_ = 0;
cin >> t_;
for (i_ = 0; i_ < t_; ++i_) solve(i_);
return 0;
}

G - Knapsack

解题思路

复杂度

代码参考

Show code

G.cppview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
template <class... Ts>
void dec(Ts &...x) {
((--x), ...);
}
template <class... Ts>
void inc(Ts &...x) {
((++x), ...);
}
void solve(int t_ = 0) {
int n, W, k;
cin >> n >> W >> k;
vector<pair<int, int>> a(n);
for (auto &[w, v] : a) cin >> w >> v;
sort(a.begin(), a.end());
vector<vector<i64>> dp(n, vector<i64>(W + 1));
for (int i = 0; i < n; ++i)
for (int j = W; j >= 0; --j)
if (i == 0) {
if (a[i].first <= j) dp[i][j] = a[i].second;
} else {
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
if (a[i].first <= j)
dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i].first] + a[i].second);
}
int i = n - 1;
multiset<int> s;
i64 ans = 0, ret = 0;
for (; s.size() != k; --i) {
s.insert(a[i].second);
ret += a[i].second;
}
for (; i >= 0; --i) {
ans = max(ans, ret + dp[i][W]);
s.insert(a[i].second);
ret += a[i].second;
ret -= *s.begin();
s.erase(s.begin());
}
ans = max(ans, ret);
cout << ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cerr << fixed << setprecision(6);
int i_ = 0;
solve(i_);
return 0;
}

H - Puzzle: Question Mark

解题思路

复杂度

代码参考

Show code

I - Counter

解题思路

复杂度

代码参考

Show code

I.cppview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
template <class Tp>
using vec = vector<Tp>;
template <class... Ts>
void dec(Ts &...x) {
((--x), ...);
}
template <class... Ts>
void inc(Ts &...x) {
((++x), ...);
}
void solve(int t_ = 0) {
int n, m;
cin >> n >> m;
vec<pair<int, int>> a(m);
for (int i = 0; i < m; ++i) cin >> a[i].first >> a[i].second;
sort(a.begin(), a.end());
int flag = 0;
for (int i = 0; i < m; ++i) {
if (a[i].first < a[i].second) {
flag = 1;
break;
}
if (i == 0) continue;
if (a[i - 1].first == a[i].first) {
if (a[i].second != a[i - 1].second) {
flag = 1;
break;
} else continue;
}
auto l = a[i - 1].first - a[i - 1].second, r = a[i - 1].first;
auto li = a[i].first - a[i].second, ri = a[i].first;
if (l == li) continue;
if (a[i - 1].second == 0) {
if (li < r) {
flag = 1;
break;
}
} else {
if (li <= r) {
flag = 1;
break;
}
}
}
if (flag) cout << "No\n";
else cout << "Yes\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cerr << fixed << setprecision(6);
int i_ = 0;
int t_ = 0;
cin >> t_;
for (i_ = 0; i_ < t_; ++i_) solve(i_);
return 0;
}

J - Suffix Structure

解题思路

复杂度

代码参考

Show code

K - Grand Finale

解题思路

复杂度

代码参考

Show code

L - Elevator

解题思路

复杂度

代码参考

Show code

L.cppview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
template <class Tp>
using vec = vector<Tp>;
#define for_(i, l, r, v...) for (i64 i = (l), i##e = (r), ##v; i <= i##e; ++i)
template <class... Ts>
void dec(Ts &...x) {
((--x), ...);
}
template <class... Ts>
void inc(Ts &...x) {
((++x), ...);
}
struct parcel {
i64 c, f;
bool operator<(const parcel &x) { return x.f < f; }
};
void solve(int t_ = 0) {
int n, w;
i64 k;
cin >> n >> k;
vec<parcel> a(n);
for_(i, 0, n - 1) cin >> a[i].c >> w >> a[i].f, a[i].c *= w;
sort(a.begin(), a.end());
i64 sy = 0, last = 0;
i64 ans = 0;
for (int i = 0; i < n; ++i) {
if (sy) {
int t = min(a[i].c, k - sy);
a[i].c -= t;
sy += t;
}
if (!a[i].c) continue;
ans += a[i].c / k * a[i].f;
sy = a[i].c % k, last = a[i].f;
if (sy) ans += last;
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cerr << fixed << setprecision(6);
int i_ = 0;
int t_ = 0;
cin >> t_;
for (i_ = 0; i_ < t_; ++i_) solve(i_);
return 0;
}

M - Trapping Rain Water

解题思路

复杂度

代码参考

Show code

M.cppview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
template <class Tp>
using vec = vector<Tp>;
#define for_(i, l, r, v...) for (i64 i = (l), i##e = (r), ##v; i <= i##e; ++i)
template <class... Ts>
void dec(Ts &...x) {
((--x), ...);
}
template <class... Ts>
void inc(Ts &...x) {
((++x), ...);
}
struct segtree {
struct YYZ {
i64 sum, mx, lazy;
YYZ(): sum(0), mx(0), lazy(0) {}
};
vec<YYZ> tr;
segtree(int N): tr(N * 4) {}
void update(int x) {
tr[x].sum = tr[x << 1].sum + tr[x << 1 | 1].sum;
tr[x].mx = max(tr[x << 1].mx, tr[x << 1 | 1].mx);
}
void build(int x, int l, int r, const vec<i64> &a) {
tr[x].lazy = 0;
if (l == r) {
tr[x].sum = tr[x].mx = a[l];
return;
}
int mid = l + (r - l) / 2;
build(x << 1, l, mid, a), build(x << 1 | 1, mid + 1, r, a);
update(x);
}
void pushdown(int x, int l, int r) {
if (tr[x].lazy) {
int mid = l + (r - l) / 2;
tr[x << 1].lazy = tr[x << 1].mx = tr[x].lazy,
tr[x << 1].sum = (mid - l + 1) * tr[x].lazy;
tr[x << 1 | 1].lazy = tr[x << 1 | 1].mx = tr[x].lazy,
tr[x << 1 | 1].sum = (r - mid) * tr[x].lazy;
tr[x].lazy = 0;
}
}
void modify(int x, int l, int r, int L, int R, i64 k) {
if (L <= l && R >= r) {
tr[x].lazy = tr[x].mx = k;
tr[x].sum = (r - l + 1) * k;
return;
}
pushdown(x, l, r);
int mid = l + (r - l) / 2;
if (L <= mid) modify(x << 1, l, mid, L, R, k);
if (R > mid) modify(x << 1 | 1, mid + 1, r, L, R, k);
update(x);
}
int query(int x, int l, int r, i64 k) {
if (tr[1].mx < k) return r + 1;
if (l == r) {
if (l == 0 && tr[x].mx > k) return -1;
return l;
}
pushdown(x, l, r);
int mid = l + (r - l) / 2;
if (tr[x << 1].mx > k) return query(x << 1, l, mid, k);
else return query(x << 1 | 1, mid + 1, r, k);
}
};
void solve(int t_ = 0) {
int n;
cin >> n;
i64 mx = 0, sm = 0;
vec<i64> a(n), b(n), f(n), g(n);
for_(i, 0, n - 1)
cin >> a[i], mx = max(mx, a[i]), b[n - 1 - i] = a[i], sm += a[i];
f[0] = a[0];
for (int i = 1; i < n; ++i) f[i] = max(f[i - 1], a[i]);
g[0] = b[0];
for (int i = 1; i < n; ++i) g[i] = max(g[i - 1], b[i]);
segtree tf(n), tg(n);
tf.build(1, 0, n - 1, f);
tg.build(1, 0, n - 1, g);
int q;
cin >> q;
for_(i, 1, q, l, v) {
cin >> l >> v;
--l;
a[l] += v, sm += v;
mx = max(mx, a[l]);
int r = tf.query(1, 0, n - 1, a[l]);
if (r - 1 >= l) tf.modify(1, 0, n - 1, l, r - 1, a[l]);
r = tg.query(1, 0, n - 1, a[l]);
if (r - 1 >= n - l - 1) tg.modify(1, 0, n - 1, n - l - 1, r - 1, a[l]);
cout << tf.tr[1].sum + tg.tr[1].sum - n * mx - sm << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cerr << fixed << setprecision(6);
int i_ = 0;
int t_ = 0;
cin >> t_;
for (i_ = 0; i_ < t_; ++i_) solve(i_);
return 0;
}

  1. 打 * 的是还没写的题↩︎

  2. 带超链接的是找到了原题或原型↩︎