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. 我们规定 \([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]\) 为最小和子段,这就矛盾了。
  2. 在这个 \(k\) 下, \([l,r]\) 一定是被无视的一段,即经过 \([l,r]\) 后结果还是 \(k\) 。首先结果不会小于 \(k\) ,若结果大于 \(k\) ,那么 \([l,r]\) 一定存在一段后缀是正的,否则我们从 \(l\) 开始从 \(0\) 累加,一旦和为负则累和归零继续,因为不存在一段后缀是正的,最后结果累和一定为 \(0\) ,结果也不可能大于 \(k\) 。因此,我们把这段后缀去掉会得到更小和的子段,与 \([l,r]\) 是最小和子段矛盾。
  3. 而 \([r,n]\) 是不会被无视的,如果有一段被无视,那么其一定有一个前缀是负的,同样可以类似反证,因此和 \([l,r]\) 合并会得到更小和的子段,矛盾了。实际上,一开始证明答案的上限也可以得到这个结论。

综上,我们只需要找到最小和子段 \([l,r]\) ,并让 \(k\) 为 \([1,l-1]\) 的和,那么 \([l,r]\) 一定是有且仅有的被无视的一段,且 \([1,l-1]\) 的和不会改变,那么结果即为总和减去最小和子段,是理论最大值。

以上整个过程,有两种实现方法:

  1. 枚举每个右端点 \(r\) 在 \([1,r]\) ,则 \([1,r]\) 中的最大前缀和作为 \(k\) , \(k\) 取 \([k+1,r]\) 最小值位置时的值。
  2. 枚举每个 \(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的更多相关文章

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

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

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

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

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

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

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

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

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

  9. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. ChatGPT API接口编程基础与使用技巧

    总结/朱季谦 趁着这周末空闲时间,在研读完OpenAi官网文档的基础上,及时总结了这篇<ChatGPT API接口编程基础与使用技巧>. 本文大部分内容是围绕编程方面,包括ChatGPT模 ...

  2. 17-js代码压缩

    const { resolve } = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); modul ...

  3. nginx概要

    新机(CentOS7)配置nginx: 一. 更新yum源为阿里云镜像 ping mirrors.aliyun.com mv /etc/yum.repos.d/CentOS-Base.repo /et ...

  4. chatgpt接口开发笔记1:completions接口

    chatgpt接口开发笔记1:completions接口 个人博客地址: https://note.raokun.top 拥抱ChatGPT,国内访问网站:https://www.playchat.t ...

  5. 快速上手Linux核心命令(十):Linux安装软件

    目录 前言 rpm rpm包管理器 yum 自动化RPM包管理工具 前言 这期呢主要说一说Linux中包软件管理相关命令,这一期的命令虽然只有两个.但 软件包的安装和卸载都是我们平常最常用的,需要熟练 ...

  6. 快速求popcount的和

    前置知识 \(\text{popcount}(n)\) 表示将 \(n\) 转为二进制后的数中 \(1\) 的个数. 结论 \[\sum_{i=1}^{n} \text{ popcount}(i)=\ ...

  7. 网站七牛云CDN加速配置

    首先进入七牛云管理平台 1.添加域名 2.添加需要加速的域名,比如我添加的是gechuang.net 3.源站配置,这里要用IP地址,访问的目录下面要有能访问测试的文件 4.缓存配置,也就是配置缓存哪 ...

  8. selenium 执行js脚本

    使用 selenium 直接在当前页面中进行js交互 使用selenium 执行 Js 脚本 要使用 js 首先要知道 js 怎么用,现在举个简单得例子,就用12306举例子, 它的首页日期选择框是只 ...

  9. python中的一些解码和编码

    开头 最近爬取百度贴吧搜索页的时候遇到一个url的编码问题,颇为头疼,记录下来防止下次忘记 工具网站 解码编码的工具网站推荐 http://tool.chinaz.com/tools/urlencod ...

  10. Hugging News #0512: 🤗 Transformers、🧨 Diffusers 更新,AI 游戏是下个新热点吗

    每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...