跳到正文

题解

2025暑假提高级冲刺集训班-5 题解

_TwilightSnow_约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年7月20日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 9 分钟

版本

  1. v1当前

T1 [COCI 2022/2023 #2] Prijateljice

直接模拟即可,每次判断是否能够说出答案,不能的话对方就赢了。

C++
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PII pair<int, int>
#define VI vector<int>
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define up(i, s, t) for (int i = s; i < t; i++)
#define dw(i, s, t) for (int i = s - 1; i >= t; i--)

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n, m;
	cin >> n >> m;
	vector<string> a(n), b(m);
	up(i, 0, n)	cin >> a[i];
	up(i, 0, m) cin >> b[i];
	sort(all(a)), sort(all(b));
	int pl = 0, pz = 0, ansl = 1, ansz = 1;
	while (ansl && ansz) {
		ansl = ansz = 0;
		up(i, pz, m)
			if (b[i] > a[pl] && b[i][0] - a[pl][0] <= 1) {
				pz = i, ansz = 1;
				break;
			}
		up(i, pl, n)
			if (a[i] > b[pz] && a[i][0] - b[pz][0] <= 1) {
				pl = i, ansl = 1;
				break;
			}
	}
	if (ansz) cout << "Zoe";
	else cout << "Leona";
	
	return 0;
}

T2 [USACO18DEC] Teamwork G

简单 DP。

设 fif_{i} 表示枚举到 ii,以 ii 为这个区间结尾的最大得分。那么易得转移:fi=fj+max⁡k=j+1i{ak}×(i−j)(min⁡(i−K,0)≤j<i)f_i = f_j + \max^{i}_{k=j+1}\{a_k\} \times (i - j)(\min(i - K, 0) \le j < i),唯一不好处理的是这个 max⁡\max,总不能再花 O(n)O(n) 的时间去处理吧?但是,我们发现,可以一路倒序枚举 jj,max⁡\max 的值一路算下来即可。

C++
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define up(i, s, t) for (int i = s; i <= t; i++)
#define dw(i, s, t) for (int i = s; i >= t; i--)

const int N = 1e4 + 2;

int n, k, res, a[N], f[N];

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	cin >> n >> k;
	up(i, 1, n) cin >> a[i];
	up(i, 1, n) {
		int mx = a[i];
		dw(j, i - 1, max(0ll, i - k)) {
			f[i] = max(f[i], f[j] + mx * (i - j));
			mx = max(mx, a[j]);
		}
	}
	cout << f[n] << '\n';
	
	return 0;
}

T3 [ARC137B] Count 1’s

因为 0,10,1 只会造成 +1+1 或 −1-1 的贡献,所以我们只需要考虑最大值与最小值,那么答案区间就是 [MAXV,MINV][MAXV,MINV],然后就是算最小或最大子段和即可。

计算的话可以使用前缀和,以 ii 为结尾的最大子段和就是 Si−min⁡j=1i{Sj}S_i - \min^{i}_{j=1} \{S_j\}。最小的同理。

C++
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define up(i, s, t) for (int i = s; i <= t; i++)

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int mx = 0, mn = 0, mxr = 0, mnr = 0, n, a, s = 0;
	cin >> n;
	up(i, 1, n) {
		cin >> a, s += a ? -1 : 1;
		mx = max(mx, s), mn = min(mn, s);
		mxr = max(mxr, s - mn), mnr = min(mnr, s - mx);
	}
	cout << mxr - mnr + 1 << '\n';
	
	return 0;
}

T4 [ABC197C] ORXOR

N≤20N \le 20,所以直接暴力 O(2N)O(2^N) 即可。

C++
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define up(i, s, t) for (int i = s; i <= t; i++)
#define dw(i, s, t) for (int i = s; i >= t; i--)

const int N = 22;

int n, ans = 1e18, a[N];
vector<int> res;

void dfs(int u, int s) {
	if (u == n + 1) {
		int tot = s;
		for (int t : res) tot ^= t;
		ans = min(ans, tot);
		return;
	}
	dfs(u + 1, s | a[u]); // 继续扩展区间
	res.push_back(s);
	dfs(u + 1, a[u]); // 下一个区间
	res.pop_back();
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	cin >> n;
	up(i, 1, n) cin >> a[i];
	dfs(1, 0), cout << ans << '\n';
	
	return 0;
}

当然,我们也可以用子集枚举。

T5 [COCI 2023/2024 #4] Knjige

贪贪贪!

我们可以发现,读书一定是跳过一段,再读一段连续的书,因为后面的价值更大,而跳读的时间是一样的,所以读后面,跳前面一定是更优的。所以,我们可以枚举跳读多少本书,然后用前缀和计算一下,输出答案即可。

时间复杂度:O(N)O(N)。

C++
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PII pair<int, int>
#define VI vector<int>
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define up(i, s, t) for (int i = s; i <= t; i++)
#define dw(i, s, t) for (int i = s; i >= t; i--)

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int n, t, a, b, res = -2e9;
	cin >> n >> t >> a >> b;
	VI k(n + 1), s(n + 1);
	up(i, 1, n) cin >> k[i], s[i] = s[i - 1] + k[i];
	up(i, 0, n) {
		int tt = t - i * b;
		tt /= a, res = max(res, s[min(i + tt, n)] - s[i]);
	}
	cout << res << '\n';
	
	return 0;
}

T6 [tessoku_book_el B56] Road to Promotion Hard

简单树形 DP。

设 fuf_u 表示节点 uu 的等级,用节点 vv 转移即可。

C++
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define up(i, s, t) for (int i = s; i <= t; i++)
#define dw(i, s, t) for (int i = s; i >= t; i--)

const int N = 1e5 + 2;

int n, r, f[N];
vector<int> g[N];

int dfs(int u, int fa) {
	for (int v : g[u])
		if (v != fa) f[u] = max(f[u], dfs(v, u) + 1);
	return f[u];
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	cin >> n >> r;
	for (int i = 1, u, v; i < n && cin >> u >> v; i++)
		g[u].push_back(v), g[v].push_back(u);
	dfs(r, 0);
	up(i, 1, n) cout << f[i] << ' ';
	
	return 0;
}
原文