比赛链接: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】的更多相关文章

  1. Educational Codeforces Round 94 (Rated for Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1400 A. String Similarity 题意 给出一个长 $2n-1$ 的二进制串 $s$,构造一个长 $n$ 的字 ...

  2. Educational Codeforces Round 97 (Rated for Div. 2)

    补了一场Edu round. A : Marketing Scheme 水题 #include <cstdio> #include <algorithm> typedef lo ...

  3. Educational Codeforces Round 97 (Rated for Div. 2) E. Make It Increasing(最长非下降子序列)

    题目链接:https://codeforces.com/contest/1437/problem/E 题意 给出一个大小为 \(n\) 的数组 \(a\) 和一个下标数组 \(b\),每次操作可以选择 ...

  4. Educational Codeforces Round 97 (Rated for Div. 2) D. Minimal Height Tree (贪心)

    题意:有一个从根节点\(BFS\)得来的序列(每次\(bfs\)子节点的时候保证是升序放入队列的),现在让你还原树(没必要和之前相同),问能构造出的最小的树的深度. 题解:不看根节点,我们从第二个位置 ...

  5. Educational Codeforces Round 97 (Rated for Div. 2) C. Chef Monocarp (DP)

    题意:有\(n\)个菜在烤箱中,每个时刻只能将一个菜从烤箱中拿出来,第\(i\)个时刻拿出来的贡献是\(|i-a[i]|\),你可以在任意时刻把菜拿出来,问将所有菜拿出的最小贡献是多少? 题解: 先对 ...

  6. Educational Codeforces Round 74 (Rated for Div. 2)【A,B,C【贪心】,D【正难则反的思想】】

    A. Prime Subtractiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inpu ...

  7. 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 ...

  8. 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 ...

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

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

随机推荐

  1. 为什么 StringBuffer 有 toStringCache 而 StringBuilder 没有?

    对于 StringBuilder 和 StringBuffer 的源码会发现,StringBuffer 中有一个叫 toStringCache 的成员变量,用来缓存 toString() 方法返回字符 ...

  2. 【EXPDP】Oracle expdp中并行问题

    $ expdp hr/hr tables=test1 dumpfile=test2.dmp directory=pump parallel=4 Export: Release 11.2.0.4.0 - ...

  3. Linux Shell 编程基础详解——吐血整理,墙裂推荐!

    第一部分:Linux Shell 简介 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序, ...

  4. CTFshow萌新赛-web签到

    打开靶机 查看页面信息 可以看到有一个system函数 在Linux中可以使用":"隔离不同的语句 payload如下 https://5105c8b6-83aa-4993-91b ...

  5. ctfhub技能树—web前置技能—http协议—请求方式

    打开靶机环境(每次打开都要30金币,好心疼啊) 题目描述为"请求方式" HTTP的请求方式共有八种 1.OPTIONS 返回服务器针对特定资源所支持的HTTP请求方法,也可以利用向 ...

  6. Trino总结

    文章目录 1.Trino与Spark SQL的区别分析 2.Trino与Spark SQL解析过程对比 3.Trino基本概念 4.Trino架构 5.Trino SQL执行流程 6.Trino Ta ...

  7. idea 启动热部署Devtolls

    1.在子工程pom.xml中添加devtools jar包到需要启动的项目中 1 <dependency> 2 <groupId>org.springframework.boo ...

  8. uni-app开发经验分享十六:发布android版App的详细过程

    开发环境 1. Android Studio下载地址:Android Studio官网 OR Android Studio中文社区 2. HBuilderX(开发工具) 3. App离线SDK下载:最 ...

  9. [已完结]CMU数据库(15-445)实验2-B+树索引实现(下)

    4. Index_Iterator实现 这里就是需要实现迭代器的一些操作,比如begin.end.isend等等 下面是对于IndexIterator的构造函数 template <typena ...

  10. C#高级编程第11版 - 第六章 索引

    [1]6.2 运算符 1.&符在C#里是逻辑与运算.管道符号|在C#里则是逻辑或运算.%运算符用来返回除法运算的余数,因此当x=7时,x%5的值将是2. [2]6.2.1 运算符的简写 1.下 ...