Sakura Tears training 5 题解
A - A Wide, Wide Graph
Question
给定一颗
Solution
如果设树的直径为
否则直径的端点肯定在一个连通块上的
定义树上节点
由于
Code
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n; cin >> n;
vector<vector<int>> g(n + 1);
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
function<void(int, int, vector<int>&)> dfs = [&](int u, int f, vector<int> &d) {
d[u] = d[f] + 1;
for (int v : g[u]) if (v != f) dfs(v, u, d);
};
vector<int> d(n + 1, 0), d1(n + 1, 0), d2(n + 1, 0);
dfs(1, 0, d);
int u = max_element(d.begin() + 1, d.end()) - (d.begin() + 1) + 1;
dfs(u, 0, d1);
int v = max_element(d1.begin() + 1, d1.end()) - (d1.begin() + 1) + 1;
dfs(v, 0, d2);
for (int i = 1; i <= n; i++) d[i] = max(d1[i], d2[i]);
int mxd = d1[v];
sort(d.begin() + 1, d.end());
int pos = 1;
for (int i = 1; i <= n; i++) {
if (i >= mxd) cout << n << ' ';
else {
while (pos <= n && d[pos] <= i) pos += 1;
cout << pos << ' ';
}
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T = 1;
while (T--) solve();
return 0;
}
B - Not Adding
Question
Code
#include <bits/stdc++.h>
using namespace std;
int main() {
freopen ("B.in", "r", stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
int m = *max_element(a.begin() + 1, a.end());
vector<int> cnt(m + 1, 0);
for (auto x : a) cnt[x]++;
int num = 0;
for (int i = m; i >= 1; i--) {
int g = 0;
for (int j = i; j <= m; j += i) {
if (cnt[j])
g = __gcd(g, j);
}
if (g == i) {
if (cnt[i] == 0) {
num += 1;
cnt[i] = 1;
}
}
}
cout << num << endl;
return 0;
}
C - String Deletion
Question
你有一个由
你可以对字符串进行操作,每个操作包括两个步骤:
- 选择一个整数
,然后删除字符 - 如果字符串
不为空,删除由相同字符组成的最大长度前缀
当字符串
Solution
贪心,显然,要删除长度
我们从前往后看,如果有
具体实现可以用双指针或者堆来实现
Code
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
void solve() {
int n; cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
char ch; cin >> ch;
a[i] = ch - '0';
}
priority_queue<pii, vector<pii>, greater<pii>> pq;
int cnt_1 = 0;
for (int i = 1; i <= n;) {
int j = i;
while (j <= n && a[j] == a[i]) j++;
if (j - i > 1) pq.push({i, j - i});
i = j;
}
int ans = 0, pos = 1;
while (pos <= n) {
auto get_pair = [&] () {
while (!pq.empty() && pq.top().first < pos) pq.pop();
if (pq.empty()) return pii(-1, -1);
return pq.top();
};
auto [id, len] = get_pair();
if (id == -1) {
int ans2 = 0;
for (int i = pos; i <= n; i) {
int j = i;
while (j <= n && a[j] == a[i]) j++;
ans2 += 1;
i = j;
}
cout << ans + (ans2 + 1) / 2 << '\n';
return ;
}
else {
int j = pos;
while (j <= n && a[j] == a[pos]) j++;
pos = j;
ans += 1;
pq.pop();
if (len - 1 > 1) pq.push({id, len - 1});
}
}
cout << ans << '\n';
}
int main() {
freopen ("C.in", "r", stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T; cin >> T;
while (T--) solve();
return 0;
}
D - Another Array Problem
Question
给定一个数组
- 选择
,用 替换所有 的所有
求最后
Solution
一股
考虑到如果操作两次相同的
也就说,
然后考虑
- 可以全部变成
- 或者不操作
Code
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n; cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
if (n == 2) {
cout << max({2 * abs(a[1] - a[2]), a[1] + a[2]}) << '\n';
return ;
}
if (n == 3) {
cout << max({3 * a[1], 3 * a[3], 3 * abs(a[1] - a[2]), 3 * abs(a[2] - a[3]), a[1] + a[2] + a[3]}) << '\n';
}
else
cout << n * (*max_element(a.begin() + 1, a.end())) << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T; cin >> T;
while (T--) solve();
return 0;
}
E - XOR-gun
Question
Solution
诈骗题的味道
考虑到如果三个数的最高位都一样,那么把后两个数异或起来肯定小于第三个
所以
对于小于
Code
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
freopen ("E.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n; cin >> n;
if (n > 60) {
cout << "1\n";
return 0;
}
vector<int> a(n + 1), sum(n + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i - 1] ^ a[i];
}
int ans = INF;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
for (int k = j; k <= n; k++) {
if ((sum[i - 1] ^ sum[j - 1]) > (sum[j - 1] ^ sum[k])) {
ans = min(ans, k - i - 1);
}
}
}
}
if (ans == INF) cout << "-1\n";
else cout << ans << '\n';
}
F - Restorer Distance
Quesiton
Solution
显然,
然后发现答案是可三分的
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
freopen ("F.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int N, A, R ,M; cin >> N >> A >> R >> M;
vector<int> h(N + 1);
for (int i = 1; i <= N; i++) cin >> h[i];
M = min(M, A + R);
auto check = [&] (int mid) -> ll{
ll sum1 = 0, sum2 = 0;
for (int i = 1; i <= N; i++) {
if (h[i] < mid) sum1 += mid - h[i];
else sum2 += h[i] - mid;
}
ll sum = min(sum1, sum2);
sum1 -= sum; sum2 -= sum;
return sum1 * A + sum2 * R + sum * M;
};
int l = 0, r = 1e9;
while (l < r) {
int lmid = l + (r - l) / 3, rmid = r - (r - l) / 3;
if (check(lmid) < check(rmid)) r = rmid - 1;
else l = lmid + 1;
}
cout << check(l) << '\n';
return 0;
}
G - Earn or Unlock
Question
安德烈正在玩游戏 Tanto Cuore。
他有一副
游戏进行按回合进行。每回合,安德烈选择一张未锁定的牌 — 牌上写着的值为
- 解锁牌堆顶的前
张锁定牌。如果牌堆中锁定的牌少于 张,则解锁全部锁定的牌。 - 赚取
点胜利点数。
在执行完操作后,无论哪种情况,他都要从牌堆中移除这张牌。
游戏在牌堆中剩余的所有牌都被锁定时结束,或者牌堆中没有更多的牌时结束。
安德烈可以获得的最大胜利点数是多少?
Solution
我们发现了一个事实,如果我选了前
变换一下得到
所以就转化成了一个类似于前缀和的式子
然后就看能不能走到
位运算可以用 bitset 优化,
在使用完
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
freopen ("G.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n; cin >> n;
vector<ll> a(2 * n + 1, 0), sum(2 * n + 1, 0);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= 2 * n; i++) sum[i] = sum[i - 1] + a[i];
bitset<200005> dp;
dp[1] = 1;
ll ans = 0;
for (int i = 1; i <= 2 * n; i++) {
dp |= dp << a[i];
if (dp[i]) {
ans = max(ans, sum[i] - i + 1);
dp[i] = 0;
}
}
cout << ans << '\n';
return 0;
}