2023年码谷提高组模拟赛1016
推广:https://www.cnblogs.com/RainPPR/p/sulution-magu-s1016.html
引入
一套被各种转来转去的题;参考:https://blog.csdn.net/liuziha/article/details/127353981、https://www.luogu.com.cn/blog/Chen5201314/xiao-nei-bi-sai-1025-zong-jie-ti-xie 和 https://www.cnblogs.com/Clyfort/articles/0927-test-solution.html 这三篇题解。
A. 小 O 的珠子(bead)
网址:https://www.luogu.com.cn/problem/U372849。
题意
给定
正解
第一反应是按字典序排列,但是有 Hack 数据:
2
aba
ab
按照字典序排列是 ababa,而正解是 abaab。
问题出在哪里?本人一开始想着特判,但是足以把人绕懵。
于是考虑重新设计排序方式,重新定义字符串比较
证明:要证什么?怎么证明?为什么要证?打表就完了。
实在要证明的话要考虑:严格弱序/Strict weak orderings。
代码
可视化:https://rainppr.gitee.io/archive/0x00000014/
#include <bits/stdc++.h>
using namespace std;
inline bool cmp(const string a, const string b) {
return a + b < b + a;
} signed main() {
vector<string> str; str.clear();
int n; string tmp; cin >> n;
for (int i = 1; i <= n; ++i) cin >> tmp, str.push_back(tmp);
sort(str.begin(), str.end(), cmp);
string res; for (string i : str) res += i;
cout << res << endl;
return 0;
}
B. 王国的传送门(gate)
网址:https://www.luogu.com.cn/problem/U372850。
题意
有
部分分
有
只能走一步,所以只要没连到节点
正解
可以分两种情况讨论:
0x01
有边
从
从其他点
因此问题转换为,求最少改几个点,使得节点
因此可以一遍 DFS,对于每个节点
特别的,如果
0x02
没有边
可以大胆猜测,一定需要一条边 感性理解)
假设我们已经通过若干次操作,使得现在的图
那么此时一定有
因此,必须要有边
问题转换为上一个情况,当然,为了补充自环
代码
#include <bits/stdc++.h>
using namespace std;
int pos1, n, k, v, dp[100010], ans;
vector<int> g[100010];
void dfs(int u, int fa) {
dp[u] = 1; for (int v : g[u]) dfs(v, u), dp[u] = max(dp[u], dp[v] + 1);
if ((u != 1) && (dp[u] > k || (dp[u] == k && fa != 1))) ++ans, dp[u] = 0;
} signed main() {
cin >> n >> k >> pos1;
for (int i = 2; i <= n; ++i) cin >> v, g[v].push_back(i);
dfs(1, -1); printf("%d\n", ans + (pos1 != 1));
return 0;
}
C. 黑白树(bwtree)
网址:https://www.luogu.com.cn/problem/U372853。
题意
一棵
部分分
正解
《正难则反》——忘了是我的哪个网课老师了,好像是教我组合数学的那个
大体的思路是,我们可以知道最后一条边是怎么连的,然后就可以把它“撤销”,直到不能撤销为止,然后看看是否把所有边都分开了。
具体的,我们把两棵树合在一起考虑,当有一条边
注意到最后的合并操作,可以并行,所以可以用一个
时间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
multiset<int> g[100010];
map<pii, int> st;
queue<pii> q;
void add(int u, int v) {
if (u == v) return;
if (u > v) swap(u, v);
g[u].insert(v), g[v].insert(u);
if (++st[{u, v}] == 2) q.push({u, v});
} bool solve() {
int n, x, y; scanf("%d\n", &n);
st.clear(); while (q.size()) q.pop();
for (int i = 1; i <= n + n - 2; ++i) g[i].clear();
for (int i = 1; i <= n + n - 2; ++i) scanf("%d %d", &x, &y), add(x, y);
int s = 0; while (q.size() && s < n - 1) {
if (!st[q.front()]) { q.pop(); continue; }
pii now = q.front(); q.pop();
int &u = now.first, &v = now.second;
if (g[u].size() < g[v].size()) swap(u, v);
for (int t : g[v]) st[minmax(t, v)] = 0, g[t].erase(g[t].find(v)), add(u, t);
g[v].clear(); ++s;
} return s == n - 1;
} signed main() {
int T; scanf("%d\n", &T); while (T--) {
printf(solve() ? "YES\n" : "NO\n");
} return 0;
}
D. 反复横跳(jump)
网址:https://www.luogu.com.cn/problem/U372866。
题意
给定
你需要维护一个关于任意字符串的
进行
- 给定
x ,对于任意S_k ,若其等于S_x 的一个后缀S_x[i,|S_x|] ,则令f(S_k) 加上a_i (本质相同的S_k 只加一次); - 给定
x ,询问f(S_x) 。
其中,
分析
我在读懂题的边缘反复横跳。
BTW
《我分类讨论到最后,发现第一类属于第二类,第三类包含第二类,第四类不存在》