Codeforces Round #831 (Div. 1 + Div. 2) A-E
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\) 时的答案。
分两种情况讨论:
\(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])\) 。
\(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的更多相关文章
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- 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 ...
- 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 ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- 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 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
随机推荐
- iommu系列之---概念解释篇
本文会对iommu中的一些容易引起疑惑的概念进行阐述,内核版本为4.19. 先上简写: DMAR - DMA remapping DRHD - DMA Remapping Hardware Unit ...
- “判断性别”Demo需求分析和初步设计(中)
大家好~我开设了"深度学习基础班"的线上课程,带领同学从0开始学习全连接和卷积神经网络,进行数学推导,并且实现可以运行的Demo程序 线上课程资料: 本节课录像回放 加QQ群,获得 ...
- mac怎么清理DNS缓存
可以通过Mac系统启动台功能,在其他页面打开终端进行操作即可. 1.打开终端 2.进入终端界面,输入sudo killall -HUP mDNSResponder命令执行即可.
- DataGridVIew控件绑定数据之后的,增、插、删操作
最开始没有绑定数据,很快就实现了增.插.删操作,可是绑定数据之后,进行这些操作就会报错. 网上对这方面的资料比较少,自己摸索着找到了解决方法,也就是直接对绑定的数据进行操作,这里以DataTable为 ...
- Java-随机数据生成器(造数据)
概述 简单易用的随机数据生成器.一般用于开发和测试阶段的数据填充.模拟.仿真研究.演示等场景.可以集成到各种类型的java项目中使用. 优点 非常轻量级(不到1M),容易集成,无需过多第三方依赖 简单 ...
- Android下的Drawable使用
Drawable表示一种可绘制的内容,可以由图片或者颜色组成.Android下的Drawable有BitmapDrawable.GradientDrawable.LayerDrawable等等 1.B ...
- Html飞机大战(三):定义状态
好家伙, 1.为飞机大战定义状态 1.开始 START 有一个飞机大战LOGO &天空 2.开始时 STRATING 有一个飞机加载的界面&天空 3.运行时 RUNNING 我方飞机& ...
- 第七十八篇:写一个按需展示的文本框和按钮(使用ref)
好家伙, 我们又又又来了一个客户 用户说: 我想我的页面上有一个搜索框, 当我不需要他的时候,它就是一个按钮 当我想要搜索的时候,我就点一下它, 然后按钮消失,搜索框出现, 当我在浏览其他东西时,这个 ...
- bfile 类型数据的存取
KingbaseES 支持 bfile 数据类型.对于bfile ,实际数据是存储在操作系统上,数据库存储的只是指向文件的指针. 具体例子如下: test=# create directory BFI ...
- LIKE与等式查询比较
我们知道 char 是定长类型的数据,如果数据长度小于定义的长度,会在字符串尾部加上空格.而对于空格的处理,对于等式匹配,或length等,会忽略空格.而对于like 或模式匹配,空格不能忽略. 一. ...