比赛链接

A

题解

知识点:数学。

\(2\) 特判加 \(7\),其他加 \(3\) 直接偶数。

时间复杂度 \(O(1)\)

空间复杂度 \(O(1)\)

代码

#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

题解

知识点:贪心。

注意到,最优能做到周长等于底边之和乘 \(2\) 加上高度最大值乘 \(2\) 。

我们把短的边当作底边,长的边当作高,这样长的边的贡献会最少。

时间复杂度 \(O(n)\)

空间复杂度 \(O(1)\)

代码

#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

题解

知识点:贪心,枚举。

从小到大排序后,我们发现单独放一个 \(a[1]\) 或 \(a[n]\) 在 bag3 (或 bag1 )最优,这样就能一次覆盖一段最长的,其他情况因为取在中间,不会超过 \(a[n]-a[1]\) 。

不妨假设单独放了个 \(a[n]\) 在 bag3,再把剩下的分成两段 \([a[1],a[i-1]],[a[i],a[n-1]]\) 分别放在 bag2,1 (较远的放中间),如此得到解 \(a[n] - a[i-1] + a[i] - a[i-1]\) 。同理 \(a[1]\) 单独放,有解 \(a[i] - a[1] + a[i] - a[i-1]\) 。

枚举这两种的所有情况,取最大值。

时间复杂度 \(O(n \log n)\)

空间复杂度 \(O(n)\)

代码

#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

题解

知识点:贪心,数学。

神奇的华容道。

遍历一遍,能出的直接出,当前不能出的放在除了起点终点之外的地方以后再出,但要保证放之后至少还有两个空位,即只能放 \(nm-4\) 个卡片,否则下一个进来以后就满了动不了,其他情况都能随意移动卡片(华容道qwq)。

时间复杂度 \(O(n \log n)\)

空间复杂度 \(O(n)\)

代码

#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。

设 \(dp[u][0/1]\) 表示对于以 \(u\) 为根的子树,子序列包括/不包括 \(u\) 时的答案。

分两种情况讨论:

  1. \(dp[u][0]\) 时,那么子节点 \(v_i\) 的最长不下降子序列是可以任意合并的,即子节点的答案 \(\max (dp[v_i][0],dp[v_i][1])\) 能加在一起。因为 \(a[v_i]\) 互相大小没有限制,所以可以自定义后拼在一起。那么答案便是 \(\sum \max (dp[v_i][0],dp[v_i][1])\) 。

  2. \(dp[u][1]\) 时,由于根节点 \(u\) 最后只可能等于一个子节点 \(v_i\) ,那么 \(u\) 只可能衔接在一个 \(dp[v_i][1]\) 后面。

    \(dp[v_i][0]\) 不能考虑进去。因为,当 \(v_i\) 为根的子树不是条链,一定存在子孙 \(w\) 使得 \(a[v_i]<a[w]\) ,那么 \(a[u]<a[w]\) 不可能衔接到 \(w\) 后面;当 \(v_i\) 为根的子树是链时,则 \(dp[v_i][1] = dp[v_i][0]+1>dp[v_i][0]\) ,没必要选。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#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;
}

Codeforces Round #831 (Div. 1 + Div. 2) A-E的更多相关文章

  1. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  2. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  3. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  5. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  6. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  8. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  9. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. 基于vue的脚手架开发与发布到npm仓库

    什么是脚手架 在项目比较多而且杂的环境下,有时候我们想统一一下各个项目技术栈或者一些插件/组件的封装习惯,但是每次从零开发一个新项目的时候,总是会重复做一些类似于复制粘贴的工作,这是一个很头疼的事情, ...

  2. 全链路追踪体验—最简陋TraceId的生成

    对于后端开发来说,排查问题是常有的事情.而排查问题时最常用的就是看日志,看一次调用中经过了哪些系统,是那个系统出问题了.这就需要业务日志中关联调用链的TraceId信息,从而在应用出现问题时,能够通过 ...

  3. ServletFileUpload 文件上传

    import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.FileUploadExcepti ...

  4. eReplication详解

    eReplication简介 eReplication是一款基于华为存储复制.快照.双活.克隆以及FusionSphere主机复制等特性,保证上层应用数据一致性,针对华为典型容灾解决方案,提供可视化. ...

  5. C语言怎么给函数添加形参的默认值

    以下内容为本人的著作,如需要转载,请声明原文链接微信公众号「englyf」https://www.cnblogs.com/englyf/p/16637890.html 如果不是机缘巧合,当年转到C++ ...

  6. KingbaseES通过sys_waldump解析wal日志

    前言 oracle中的redo日志我们无法直接读取,然而对于KingbaseES数据库,我们可以利用sys_waldump工具解析wal日志,查看wal日志记录的信息. 我们可以利用 sys_wald ...

  7. KingbaseES R3 集群pcp_attach_node 更新show pool_nodes中节点状态

    系统环境: 操作系统: [kingbase@node2 bin]$ cat /etc/centos-release CentOS Linux release 7.2.1511 (Core) 数据库: ...

  8. 【ACG】博主在专栏更新内容后,及时通知关注他的用户去阅读

    ​ 业务场景 当用户打开应用时,系统发送Push消息给用户,向用户推荐博主信息.用户关注博主后,将保存用户的订阅信息,当关注的博主更新内容时,向订阅的用户发送短信,提醒用户及时查看最新内容.从而持续增 ...

  9. 凭借SpringBoot整合Neo4j,我理清了《雷神》中错综复杂的人物关系

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra. 虽然距离中秋放假还要熬过漫长的两天,不过也有个好消息,今天是<雷神4>上线Disney+流媒体的日子 ...

  10. Cat Theme

    将博客皮肤设置为: SimpleMemory 插入CSS代码 #EntryTag{margin-top:20px;font-size:9pt;color:gray}.topicListFooter{t ...