Codeforces Round #831 (Div. 1 + Div. 2) A-E
A
题解
知识点:数学。
特判加 ,其他加 直接偶数。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> #define ll long long using namespace std; bool solve() { int n; cin >> n; if (n == 2) cout << 7 << '\n'; else cout << 3 << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
B
题解
知识点:贪心。
注意到,最优能做到周长等于底边之和乘 加上高度最大值乘 。
我们把短的边当作底边,长的边当作高,这样长的边的贡献会最少。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> #define ll long long using namespace std; bool solve() { int n; cin >> n; ll sum = 0; int mx = 0; for (int i = 1;i <= n;i++) { int x, y; cin >> x >> y; sum += min(x, y); mx = max({ mx,x,y }); } cout << 2 * (sum + mx) << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
C
题解
知识点:贪心,枚举。
从小到大排序后,我们发现单独放一个 或 在 bag3
(或 bag1
)最优,这样就能一次覆盖一段最长的,其他情况因为取在中间,不会超过 。
不妨假设单独放了个 在 bag3
,再把剩下的分成两段 分别放在 bag2,1
(较远的放中间),如此得到解 。同理 单独放,有解 。
枚举这两种的所有情况,取最大值。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> #define ll long long using namespace std; int a[200007]; bool solve() { int n; cin >> n; for (int i = 1;i <= n;i++) cin >> a[i]; sort(a + 1, a + n + 1); ll ans = 0; for (int i = 2;i <= n;i++) { ans = max({ ans,2LL * a[i] - a[i - 1] - a[1],-2LL * a[i - 1] + a[n] + a[i] }); } cout << ans << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
D
题解
知识点:贪心,数学。
神奇的华容道。
遍历一遍,能出的直接出,当前不能出的放在除了起点终点之外的地方以后再出,但要保证放之后至少还有两个空位,即只能放 个卡片,否则下一个进来以后就满了动不了,其他情况都能随意移动卡片(华容道qwq)。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> #define ll long long using namespace std; int a[100007]; bool solve() { int n, m, k; cin >> n >> m >> k; priority_queue<int> pq; int p = k; for (int i = 1;i <= k;i++) cin >> a[i]; for (int i = 1;i <= k;i++) { while (!pq.empty() && pq.top() == p) pq.pop(), p--; if (a[i] == p) p--; else { pq.push(a[i]); if (pq.size() >= n * m - 3) return false; } } cout << "YA" << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << "TIDAK" << '\n'; } return 0; }
E
题解
知识点:树形dp。
设 表示对于以 为根的子树,子序列包括/不包括 时的答案。
分两种情况讨论:
-
时,那么子节点 的最长不下降子序列是可以任意合并的,即子节点的答案 能加在一起。因为 互相大小没有限制,所以可以自定义后拼在一起。那么答案便是 。
-
时,由于根节点 最后只可能等于一个子节点 ,那么 只可能衔接在一个 后面。
不能考虑进去。因为,当 为根的子树不是条链,一定存在子孙 使得 ,那么 不可能衔接到 后面;当 为根的子树是链时,则 ,没必要选。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> using namespace std; vector<int> g[100007]; int f[100007][2]; void dfs(int u) { f[u][0] = 0; f[u][1] = 1; for (auto v : g[u]) { dfs(v); f[u][0] += max(f[v][0], f[v][1]); f[u][1] = max(f[u][1], f[v][1] + 1); } } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 2;i <= n;i++) { int p; cin >> p; g[p].push_back(i); } dfs(1); cout << max(f[1][0], f[1][1]) << '\n'; return 0; }
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/16842270.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧