Educational Codeforces Round 151 (Rated for Div. 2) A-D
A
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool solve() {
int n, k, x;
cin >> n >> k >> x;
if (x != 1) {
cout << "YES" << '\n';
cout << n << '\n';
for (int i = 1;i <= n;i++) cout << 1 << " \n"[i == n];
}
else {
if ((n & 1) && n >= 3) {
if (k <= 2) cout << "NO" << '\n';
else {
cout << "YES" << '\n';
cout << (n - 3) / 2 + 1 << '\n';
cout << 3 << ' ';
for (int i = 1;i <= n - 3;i += 2) cout << 2 << " \n"[i == n - 3];
}
}
else if (!(n & 1)) {
if (k == 1) cout << "NO" << '\n';
else {
cout << "YES" << '\n';
cout << n / 2 << '\n';
for (int i = 1;i <= n;i += 2) cout << 2 << " \n"[i == n - 1];
}
}
else cout << "NO" << '\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
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool solve() {
int xa, ya, xb, yb, xc, yc;
cin >> xa >> ya >> xb >> yb >> xc >> yc;
int ans = 1;
if (1LL * (xa - xb) * (xa - xc) > 0) ans += min(abs(xa - xb), abs(xa - xc));
if (1LL * (ya - yb) * (ya - yc) > 0) ans += min(abs(ya - yb), abs(ya - yc));
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;
}
C
题目
给一个只包含数位的原串 \(s\) ,以及长为 \(m\) 只包含数位的两个串 \(l,r\) 。
判断能否构造长为 \(m\) 的串,满足第 \(i\) 个数位在 \([l_i,r_i]\) 内,且不是 \(s\) 的子序列。
题解
知识点:子序列自动机,贪心。
子序列自动机实际上就是 \(nxt_{i,j}\) ,表示位置 \(i\) 往后(不包括 \(i\) )最近一个字符 \(j\) 的位置,可以逆推的形式构造出来。
通过这个自动机我们构造串的每个数位,显然如果当前位置能找到一个接下来不存在的数位,那么我们必定选它,否则我们选择出现位置最晚的那个数位。这样贪心保证如果存在解,那我们一定能取到。
时间复杂度 \(O(n+m)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int nxt[300007][10];
bool solve() {
string s;
cin >> s;
int n = s.size();
s = "?" + s;
int m;
string l, r;
cin >> m;
cin >> l >> r;
l = "?" + l;
r = "?" + r;
for (int i = 0;i <= 9;i++) nxt[n][i] = n + 1;
for (int i = n - 1;i >= 0;i--) {
for (int j = 0;j <= 9;j++) nxt[i][j] = nxt[i + 1][j];
nxt[i][s[i + 1] - '0'] = i + 1;
}
int pos = 0;
for (int i = 1;i <= m;i++) {
int mx = 0;
for (int j = l[i] - '0';j <= r[i] - '0';j++) mx = max(mx, nxt[pos][j]);
if (mx == n + 1) {
cout << "YES" << '\n';
return true;
}
pos = mx;
}
cout << "NO" << '\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
题目
给定一个长为 \(n\) 的整数数组 \(a\) ,现在从左往右累和。
现在规定一个数 \(k\) ,使得累和过程中若和一旦大于等于 \(k\) ,那么之后不会再小于 \(k\) 。
找到一个 \(k\) ,使得结果最大。
题解
知识点:前缀和,贪心。
\(k\) 的作用是使得让和小于 \(k\) 的位置,经过它们之后的结果等于 \(k\) 。所以规定一个 \(k\) 之后,所有会让经过后的和小于 \(k\) 的位置是可以无视的。
实际上,我们可以把第一个位置到最后一个位置当作连续的一段,一起无视。原因是,假设有两个使得结果会小于 \(k\) 的相邻位置 \(i,j\) ,那么一定有经过 \([1,i]\) 后结果是 \(k\) ,之后再经过 \([i+1,j]\) 之后结果也是 \(k\) ,相当于从 \([i,j]\) 的位置全部被无视了。
因此,对于一个 \(k\) ,最终结果等于总和减去一段被无视的连续段的和,理论上答案最大不会超过总和减去最小和子段。问题变为,找到一个 \(k\) ,其无视的一段连续段的和最小。
我们发现,实际上总是会有一个 \(k\) ,能满足被他无视的一段就是最小和子段,使得结果是理论最大值。只需要找到整个数组最小和的那个连续段 \([l,r]\) ,随后 \(k\) 即为 \([1,l-1]\) 的和。
结论的证明:
- 我们规定 \([1,l-1]\) 为 \(k\) ,并不会使得 \([1,l-1]\) 的和改变,变得大于 \(k\) ,而 \(k\) 即为 \([1,i-1]\) 的和所经历的最大值。因为, \([1,l-1]\) 一定是 \([1,i],i \in[1,l-1]\) 中最大的前缀和,否则若 \([1,i],i \neq l-1\) 是最大的前缀和,那么显然 \([i+1,l-1]\) 的和一定是负的,则 \([i+1,r]\) 为最小和子段,这就矛盾了。
- 在这个 \(k\) 下, \([l,r]\) 一定是被无视的一段,即经过 \([l,r]\) 后结果还是 \(k\) 。首先结果不会小于 \(k\) ,若结果大于 \(k\) ,那么 \([l,r]\) 一定存在一段后缀是正的,否则我们从 \(l\) 开始从 \(0\) 累加,一旦和为负则累和归零继续,因为不存在一段后缀是正的,最后结果累和一定为 \(0\) ,结果也不可能大于 \(k\) 。因此,我们把这段后缀去掉会得到更小和的子段,与 \([l,r]\) 是最小和子段矛盾。
- 而 \([r,n]\) 是不会被无视的,如果有一段被无视,那么其一定有一个前缀是负的,同样可以类似反证,因此和 \([l,r]\) 合并会得到更小和的子段,矛盾了。实际上,一开始证明答案的上限也可以得到这个结论。
综上,我们只需要找到最小和子段 \([l,r]\) ,并让 \(k\) 为 \([1,l-1]\) 的和,那么 \([l,r]\) 一定是有且仅有的被无视的一段,且 \([1,l-1]\) 的和不会改变,那么结果即为总和减去最小和子段,是理论最大值。
以上整个过程,有两种实现方法:
- 枚举每个右端点 \(r\) 在 \([1,r]\) ,则 \([1,r]\) 中的最大前缀和作为 \(k\) , \(k\) 取 \([k+1,r]\) 最小值位置时的值。
- 枚举每个 \(k\) ,则在 \([k+1,n]\) 中找到最小前缀和(最大后缀和)作为 \(r\) , \(k\) 取 \([k+1,r]\) 最小值位置时的值。
时间复杂度 \(O(n)\)
空间复杂度 \(O(1)\)
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool solve() {
int n;
cin >> n;
ll l = 0, r = 0, mi = 0;
ll k = 0;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
r += x;
l = max(l, r);
if (r - l < mi) {
mi = r - l;
k = l;
}
}
cout << k << '\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;
}
Educational Codeforces Round 151 (Rated for Div. 2) A-D的更多相关文章
- 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 ...
随机推荐
- python:字典中遍历key对应的value值
问题描述:python在一个字典中想要遍历某个可以对应的value值. 效果如下: 方法一:使用循环遍历所有的key,然后对比输入的值跟便利出来的key有没有相同的 constellation = [ ...
- 对抗 ChatGPT,免费体验 Claude
对抗 ChatGPT,免费体验 Claude Claude 是 Anthropic 构建的大型语言模型(LLM),对标ChatGPT. Anthropic 创始团队多是前openai研究员和工程师,C ...
- React课堂笔记1
一.概要 React是用于构建用户界面的MVVM框架. React拥有较高的性能,代码逻辑非常简单,越来越多的人已开始关注和使用它.认为它可能是将来Web开发的主流工具之一. 官网:https://z ...
- Redis 报”OutOfDirectMemoryError“(堆外内存溢出)
Redis 报错"OutOfDirectMemoryError(堆外内存溢出) "问题如下: 一.报错信息: 使用 Redis 的业务接口 ,产生 OutOfDirectMemor ...
- 从零开始TP6配置ThinkPHP-ApiDoc
系统:windows11 集成环境:小皮(原phpstudy) composer:2.5 准备工作:安装小皮后,在软件管理中安装composer,2.3安装不上去,只能安装1.8.5,没关系安装后升级 ...
- 【深度思考】聊聊CGLIB动态代理原理
1. 简介 CGLIB的全称是:Code Generation Library. CGLIB是一个强大的.高性能.高质量的代码生成类库,它可以在运行期扩展Java类与实现Java接口, 底层使用的是字 ...
- python pyinstaller库
简要 pyinstaller模块主要用于python代码打包成exe程序直接使用,这样在其它电脑上即使没有python环境也是可以运行的. 用法 一.安装 pyinstaller属于第三方库,因此在使 ...
- java LocalDateTime的使用
1.LocalDateTime的基本使用 //获取当前时间 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println( ...
- [C++提高编程] 3.8 set/ multiset 容器
文章目录 3.8 set/ multiset 容器 3.8.1 set基本概念 3.8.2 set构造和赋值 3.8.3 set大小和交换 3.8.4 set插入和删除 3.8.5 set查找和统计 ...
- Python-pytest-repeat的简单使用
前言: 一.简介 pytest-repeat是pytest的插件,重复执行单个用例,或多个测试用例,并指定重复次数. 二.安装 1.执行如下命令 pip3 install pytest-repeat ...