2025CCH非专业级软件能力认证第二轮 总结
垃圾话环节
探险家猫猫头,旅行者胖头鱼,铁匠cch。
还少一个祝睿融。
总共给了两个样例解释。
两个样例解释是错的,而且错的很离谱。
样例解释错误率是 100%,值得鼓励。
T1
弱智题。
从 n 开始回推,因为两个 n 一定是连着的。
相差 1 的搭配一下取 min 就做完了。
样例解释读到了题目精髓的脚皮。
code
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m;
struct node {
int x, y;
} a[100005][2];
signed main() {
freopen("treacat.in", "r", stdin);
freopen("treacat.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i][0].x >> a[i][0].y >> a[i][1].x >> a[i][1].y;
}
int ans = abs(a[n][0].x - a[n][1].x) + abs(a[n][0].y - a[n][1].y);
for (int i = n - 1; i >= 1; i--) {
int qwe = abs(a[i + 1][0].x - a[i][1].x) + abs(a[i + 1][0].y - a[i][1].y);
qwe += abs(a[i + 1][1].x - a[i][0].x) + abs(a[i + 1][1].y - a[i][0].y);
int ewq = abs(a[i + 1][1].x - a[i][1].x) + abs(a[i + 1][1].y - a[i][1].y);
ewq += abs(a[i + 1][0].x - a[i][0].x) + abs(a[i + 1][0].y - a[i][0].y);
ans += min(qwe, ewq);
}
cout << ans;
return 0;
}
// int k = clock();T2
关键词,cch,移山,旅行者,祝睿融。
由此我们知道这题是祝睿融。
又因为祝睿融没死所以不是 SPFA。
所以是 dijkstra。
题目还是很弱智的。
code
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, m, k;
int h[N], D[N], RF[N];
vector<pair<int, int>> g[N];
void dijkstra() {
memset(D, 0x7f, sizeof(D)); D[1] = 0;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
q.push({0, 1});
while (!q.empty()) {
pair<int, int> op = q.top(); q.pop();
int d = op.first, u = op.second;
if (d != D[u]) continue;
for (auto &s : g[u]) {
int v = s.first, w = s.second, ans = 0;
if (h[v] > h[1] + k) {
ans = (h[v] - (h[1] + k)) * (h[v] - (h[1] + k));
}
if (D[v] > D[u] + w + ans) {
D[v] = D[u] + w + ans;
q.push({D[v], v});
}
}
}
}
signed main() {
freopen("climb.in", "r", stdin);
freopen("climb.out", "w", stdout);
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 0; i < m; i++) {
int x, y, z; cin >> x >> y >> z;
g[x].push_back({y, z});
g[y].push_back({x, z});
}
dijkstra();
cout << D[n];
return 0;
}
// int k = clock();T3
考场最后不会做了写的题解。
1. 问题理解
题目要求计算满足特定交替大小关系的排列个数:
- 对于所有 i(2 ≤ i ≤ n):
- 如果 i 是奇数,则 a[i-1] < a[i](上升)
- 如果 i 是偶数,则 a[i-1] > a[i](下降)
2. 动态规划
dp[i][j] 表示:
- 长度为 i 的排列
- 最后一个数字是 j(注意:这里 j 是实际数字值,不是排名)
- 满足题目交替条件的排列个数
3. 状态转移
前缀和数组
memset(a, 0, sizeof(a));
for (int j = 1; j < i; j++) {
a[j] = (a[j - 1] + dp[i - 1][j]) % MOD;
}这里 a[j] 是前缀和数组,表示长度为 i-1 的排列中,最后一个数字 ≤ j 的所有排列个数之和。
状态转移
情况1:当前长度 i 是奇数
if (i % 2 == 1) {
for (int j = 1; j <= i; j++) {
dp[i][j] = a[j - 1];
}
}- i 是奇数,要求前一个位置是上升(a[i-2] < a[i-1])
- 当前排列以数字 j 结尾
- 前一个排列的最后一个数字必须 < j(因为要上升)
a[j-1]就是所有前一个排列以小于 j 的数字结尾的排列个数之和
情况2:当前长度 i 是偶数
else {
for (int j = 1; j <= i; j++) {
dp[i][j] = (a[i - 1] - a[j - 1] + MOD) % MOD;
}
}- i 是偶数,要求前一个位置是下降(a[i-2] > a[i-1])
- 当前排列以数字 j 结尾
- 前一个排列的最后一个数字必须 > j(因为要下降)
a[i-1] - a[j-1]就是所有前一个排列以大于 j 的数字结尾的排列个数之和
4. 空间优化
我也不知道 1e8 的 int 数组能不能过就滚动数组了。
5. 最终结果
int ans = 0;
for (int j = 1; j <= n; j++) ans = (ans + dp[n][j]) % MOD;
cout << ans;将所有长度为 n 且满足条件的排列个数求和,得到最终答案。
6. 时间复杂度
- 外层循环:O(n)
- 内层循环:O(n)
- 前缀和计算:O(n)
- 总时间复杂度:O(n²)
code
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
int n, dp[2][10005], sum[10005];
signed main() {
freopen("perm.in", "r", stdin);
freopen("perm.out", "w", stdout);
cin >> n;
if (n == 0) {
cout << 0; return 0;
}
dp[1][1] = 1;
for (int i = 2; i <= n; i++) {
memset(sum, 0, sizeof(sum));
for (int j = 1; j < i; j++) {
sum[j] = (sum[j - 1] + dp[(i - 1) % 2][j]) % MOD;
}
if (i % 2 == 1) {
for (int j = 1; j <= i; j++) {
dp[i % 2][j] = sum[j - 1];
}
} else {
for (int j = 1; j <= i; j++) {
dp[i % 2][j] = (sum[i - 1] - sum[j - 1] + MOD) % MOD;
}
}
}
int ans = 0;
for (int j = 1; j <= n; j++) ans = (ans + dp[n % 2][j]) % MOD;
cout << ans;
return 0;
}
// int k = clock();T4
肯定不是单峰啊!!😡🤪😋🤢😓😮😎🤗😨🤔🤡🤯🤤😊😂🤣😍😒👌😘💕😁👍
上面的是傻逼说的,而且傻逼 wzj 还信了,傻逼吧。
一看就是单峰。
但是三分太恶心了,还是写权值线段树吧。
将每个首领的宝物降序排序,分层放入桶
从大到小枚举 ,将所有层次 的宝物逐步计入强制集合并从全局池中删除
维护全局剩余池的价格分布,用权值线段树或树状数组支持“取最小 件的和”
每次计算当前 i 的总代价,更新最优。
,人话就是
code
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
struct Node {
int cnt, sum;
} tree[4 * N];
int D[N], n, m, q, cnt, A, G, ans = LONG_LONG_MAX;
vector<int> v[N], id[N], R;
multiset<int> s;
void modify(int p, int l, int r, int x, int d) {
tree[p].cnt += d;
tree[p].sum += D[x] * d;
if (l == r) return;
int m = (l + r) / 2;
if (x <= m) {
modify(2 * p, l, m, x, d);
} else {
modify(2 * p + 1, m + 1, r, x, d);
}
}
int query(int p, int l, int r, int k) {
if (l == r) return D[l] * k;
int m = (l + r) / 2, cnt = tree[2 * p].cnt;
if (k <= cnt) return query(2 * p, l, m, k);
return tree[2 * p].sum + query(2 * p + 1, m + 1, r, k - cnt);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1, a, c; i <= m; i++) {
cin >> a >> c;
v[c].push_back(a);
D[++q] = a;
}
sort(D + 1, D + q + 1);
q = unique(D + 1, D + q + 1) - D - 1;
for (int i = 1; i <= n; i++) {
if (v[i].empty()) continue;
for (int &j : v[i]) {
j = lower_bound(D + 1, D + q + 1, j) - D;
modify(1, 1, q, j, 1);
}
sort(v[i].begin(), v[i].end(), greater<>());
id[v[i].size()].push_back(i);
}
for (int i = m; i >= 1; i--) {
for (int j : id[i]) R.push_back(j);
for (int j : R) {
G += D[v[j].back()];
modify(1, 1, q, v[j].back(), -1);
v[j].pop_back(); A++;
}
ans = min(ans, G + query(1, 1, q, max(0ll, i - A)));
}
cout << ans;
return 0;
}
// int k = clock();