比赛链接: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. NOIP初赛篇——04计算机软件系统

    计算机软件是指计算机系统中的程序及其文档,也是用户与硬件之间的接口,用户主要通过软件与计算机进行交流,软件是计算机的灵魂.没有安装软件的计算机称为"裸机",无法完成任何工作.一般软 ...

  2. Redis集群搭建与简单使用【转】

    Redis集群搭建与简单使用 安装环境与版本 用两台虚拟机模拟6个节点,一台机器3个节点,创建出3 master.3 salve 环境. redis 采用 redis-3.2.4 版本. 两台虚拟机都 ...

  3. jxl解析多个excel工作表-java代码

    @Override public ResultBean txImportDqKpi(String filePath) { ResultBean rb = new ResultBean(); int s ...

  4. 【Spring】Spring 事务控制

    Spring 事务控制 Spring 事务控制介绍 JavaEE 体系进行分层开发,事务控制位于业务层,Spring 提供了分层设计业务层的事务处理解决方案. Spring 的事务控制都是基于 AOP ...

  5. SpringBoot对静态资源的映射规则

    在WebMvcAutoConfiguration类中有相对应的方法addResourceHandlers public void addResourceHandlers(ResourceHandler ...

  6. mongodb表索引备份,索引的导出导入

    背景 发现有两个mongodb环境的数据库表索引不一致,另一个数据库有索引缺失,需要将一个数据库里的所有表索引导入到另一个数据库 也可用于单独备份数据库所有表的索引 写mongo shell的js脚本 ...

  7. Nginx基础知识学习(安装/进程模型/事件处理机制/详细配置/定时切割日志)

    一.Linux下Nginx的安装 1.去官网 http://nginx.org/download/下载对应的Nginx安装包,推荐使用稳定版本. 2.上传Nginx到Linux服务器. 3.安装依赖环 ...

  8. 更改mysql的密码

    mysql> set password for 'root'@'localhost' =PASSWORD('');Query OK, 0 rows affected (0.17 sec) mys ...

  9. 【Nginx】使用keepalive和nginx搭载高可用

    首先介绍一下Keepalived,它是一个高性能的服务器高可用或热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx的配合实现web服务端的高可用. Keepa ...

  10. kubernets之服务发现

    一  服务与pod的发现 1.1  服务发现pod是很显而易见的事情,通过简称pod的标签是否和服务的标签一致即可,但是pod是如何发现服务的呢?这个问题其实感觉比较多余,但是接下来你就可能不这么想了 ...