比赛链接: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. 【JDBC核心】获取数据库连接

    获取数据库连接 要素一:Driver 接口实现类 Driver 接口: java.sql.Driver 接口是所有 JDBC 驱动程序需要实现的接口.这个接口是提供给数据库厂商使用的,不同数据库厂商提 ...

  2. LeetCode198--打家劫舍问题

    题目 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个 ...

  3. .NET 调整图片尺寸(Resize)各种方法

    本文中如无特别说明 .NET 指 .NET 5或者更高版本,代码同样可用于 .NET Core 前言 调整图片尺寸最常用的场景就是生成缩略图,一般为保持纵横比缩小,如果图片放大会使图片变得模糊,如果确 ...

  4. Linux 入门教程:基础操作 01

    1.1 实验内容 实验楼环境介绍 常用 Shell 命令及快捷键 Linux 使用小技巧 1.2 实验知识点 Linux 基本命令 通配符的使用 查看帮助文档 终端的概念 通常我们在使用 Linux ...

  5. python模块详解 | psutil

    目录 psutil 简介 psutil的功能函数 cpu memory_内存 disk_磁盘 net_网络 pid_进程管理 sensors_传感器 其他(用户,启动时间) psutil简介 psut ...

  6. 攻防世界—pwn—level2

    题目分析 题目提示 下载文件后首先使用checksec检查文件保护机制 使用ida打开,查看伪代码 搜索字符串发现/bash/sh 信息收集 偏移量 system的地址 /bin/sh的地址 编写脚本 ...

  7. ES数据库高可用配置

    ES高可用集群部署 1.ES高可用架构图 2.创建ES用户组 1.Elasticsearch不能在 root 用户下启动,我们需要在三台机器上分创建一个普通用户# 创建elastic用户 userad ...

  8. C#高级编程第11版 - 第七章 索引

    [1]7.1 相同类型的多个对象 1.假如你需要处理同一类型的多个对象,你可以使用集合或者数组. 2.如果你想使用不同类型的不同对象,你最好将它们组合成class.struct或者元组. [2]7.2 ...

  9. 【Soul源码探秘】插件链实现

    引言 插件是 Soul 的灵魂. Soul 使用了插件化设计思想,实现了插件的热插拔,且极易扩展.内置丰富的插件支持,鉴权,限流,熔断,防火墙等等. Soul 是如何实现插件化设计的呢? 一切还得从插 ...

  10. logging philosophy 日志哲学

    Go kit - Frequently asked questions https://gokit.io/faq/ Logging - Why is package log so different? ...