Educational Codeforces Round 97 (Rated for Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1437
A. Marketing Scheme
题解
令 \(l = \frac{a}{2}\),那么如果 \(r < a\),每个顾客都会买更多猫粮。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int l, r;
cin >> l >> r;
cout << (r < 2 * l ? "YES" : "NO") << "\n";
}
return 0;
}
B. Reverse Binary Strings
题解
如果有一个连续 \(0\) 或 \(1\) 区间长度大于 \(1\),那么多出来的长度中的每个数都是需要翻转的。答案即连续 \(0\) 或 \(1\) 区间多出长度之和的最大值。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
int ans[2] = {};
for (int i = 0; i < n; ) {
int j = i + 1;
while (j < n and s[j] == s[i]) ++j;
ans[s[i] - '0'] += j - i - 1;
i = j;
}
cout << max(ans[0], ans[1]) << "\n";
}
return 0;
}
C. Chef Monocarp
题解一
\(dp_{ij}\) 的含义为 \(i\) 个点放前 \(j\) 个数的最小花费。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> t(n);
for (auto &x : t) cin >> x;
sort(t.begin(), t.end());
vector<vector<int>> dp(2 * n + 1, vector<int>(n + 1, 1e9));
for (int i = 0; i < 2 * n + 1; i++)
dp[i][0] = 0;
for (int i = 1; i <= 2 * n; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1] + abs(t[j - 1] - i));
}
}
cout << dp[2 * n][n] << "\n";
}
return 0;
}
题解二
由于每层的状态取决于上一层,所以可以倒序遍历,空间复杂度因此由 \(S_{(n^2)}\) 优化至 \(S_{(n)}\) 。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> t(n);
for (auto &x : t) cin >> x;
sort(t.begin(), t.end());
vector<int> dp(n + 1, 1e9);
dp[0] = 0;
for (int i = 1; i <= 2 * n; i++) {
for (int j = n; j >= 1; j--) {
dp[j] = min(dp[j], dp[j - 1] + abs(t[j - 1] - i));
}
}
cout << dp[n] << "\n";
}
return 0;
}
D. Minimal Height Tree
题解
将 \(bfs\) 序列分割为一个个连续递增区间,每个递增区间都可以归于上一层的一个结点。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (auto &x : a) cin >> x;
vector<int> seg;
for (int i = 1; i < n; ) {
int j = i + 1;
while (j < n and a[j] > a[j - 1]) ++j;
seg.push_back(j - i);
i = j;
}
int height = 0;
int prv = 1, nxt = 0;
for (int i = 0; i < int(seg.size()); ) {
++height;
nxt = accumulate(seg.begin() + i, min(seg.end(), seg.begin() + i + prv), 0);
i += prv;
prv = nxt;
}
cout << height << "\n";
}
return 0;
}
Educational Codeforces Round 97 (Rated for Div. 2)【ABCD】的更多相关文章
- Educational Codeforces Round 94 (Rated for Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1400 A. String Similarity 题意 给出一个长 $2n-1$ 的二进制串 $s$,构造一个长 $n$ 的字 ...
- Educational Codeforces Round 97 (Rated for Div. 2)
补了一场Edu round. A : Marketing Scheme 水题 #include <cstdio> #include <algorithm> typedef lo ...
- Educational Codeforces Round 97 (Rated for Div. 2) E. Make It Increasing(最长非下降子序列)
题目链接:https://codeforces.com/contest/1437/problem/E 题意 给出一个大小为 \(n\) 的数组 \(a\) 和一个下标数组 \(b\),每次操作可以选择 ...
- Educational Codeforces Round 97 (Rated for Div. 2) D. Minimal Height Tree (贪心)
题意:有一个从根节点\(BFS\)得来的序列(每次\(bfs\)子节点的时候保证是升序放入队列的),现在让你还原树(没必要和之前相同),问能构造出的最小的树的深度. 题解:不看根节点,我们从第二个位置 ...
- Educational Codeforces Round 97 (Rated for Div. 2) C. Chef Monocarp (DP)
题意:有\(n\)个菜在烤箱中,每个时刻只能将一个菜从烤箱中拿出来,第\(i\)个时刻拿出来的贡献是\(|i-a[i]|\),你可以在任意时刻把菜拿出来,问将所有菜拿出的最小贡献是多少? 题解: 先对 ...
- Educational Codeforces Round 74 (Rated for Div. 2)【A,B,C【贪心】,D【正难则反的思想】】
A. Prime Subtractiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inpu ...
- 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< ...
随机推荐
- 使用Pr压缩视频,增大音量
视屏压缩(亲测有效教程):https://www.zhihu.com/question/67762625 音量增大(亲测有效教程):https://jingyan.baidu.com/article/ ...
- MySql中的有条件插入 insert where
假设现在我们有这样的需求:当数据库中不存在满足条件的记录时,可以插入一条记录,否则程序退出.该怎么实现? 1年以上工作经验的人应该都能立即想到:去检查一下库里有没有记录,没有就插入,有就结束. int ...
- Approach for Unsupervised Bug Report Summarization 无监督bug报告汇总方法
AUSUM: approach for unsupervised bug report summarization 1. Abstract 解决的bug被归类以便未来参考 缺点是还是需要手动的去细读很 ...
- 最全的HashMap源码解析!
HashMap源码解析 HashMap采用键值对形式的存储结构,每个key对应唯一的value,查询和修改的速度很快,能到到O(1)的平均复杂度.他是非线程安全的,且不能保证元素的存储顺序. 他的关系 ...
- MySQL常用的一些(就几个)聚合函数
聚合函数 (常用) 函数名称 描述 CONUT() 记数 SUM() 求和 AVG() 平均值 MAX() 最大值 MIN() 最小值 -- ================= 聚合函数 ====== ...
- postgresql插件安装
postgresql安装包自带插件安装: 1.编译安装插件 # root用户 # postgresql安装过程省略 # 进入解压包的contrib目录 cd postgresql-10.6/contr ...
- python zxing包解析二维码报UnicodeDecodeError错误解决办法
一般错误的原因是这个库不支持中文的解码(二维码内容包含中文). 修改如下: 进入zxing.__init__.py代码中,类BarCode下,parse方法中: 找到下面这两行原代码如下: 1 raw ...
- luogu P2198 杀蚂蚁
题目描述 经过小FF的研究,他发现蚂蚁们每次都走同一条长度为n个单位的路线进攻, 且蚂蚁们的经过一个单位长度所需的时间为T秒.也就是说,只要小FF在条路线上布防且给蚂蚁造成沉痛伤害就能阻止蚂蚁的进军. ...
- 2V转3V的电源芯片电路图,2.4V转3V电路
两节镍氢电池1.2V+1.2V是2.4V的标称电压,2.4V可以转3V输出电路应用. 在2.4V转3V和2V转3V的应用中,输出电流可最大600MA. 2V的低压输入,可以采用PW5100低压输入专用 ...
- secrets 管理工具 Vault 的介绍、安装及使用
原文:https://ryan4yin.space/posts/expirence-of-vault/ Vault 是 hashicorp 推出的 secrets 管理.加密即服务与权限管理工具.它的 ...