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. [NotePad++]NotePad++实用技巧

    2 应用技巧 2.1 匹配并捕获/截取 截取第1列的数据 截取前 "(.*)", "(.*)", "(.*)"\)\); 截取后: 2.2 ...

  2. [数据库/MYSQL]#解决缺陷#设置Unique索引时:"[Err] 1071 - Specified key was too long; max key length is 767 bytes"

    1 问题复现 原表结构: CREATE TABLE `XX_TEMPERATURE` ( `FLOW_ID` int(11) NOT NULL COMMENT '独立的数据表或FTP唯一标识', -- ...

  3. [操作系统/Linux]磁盘分区

    0 基本概念1: 盘片/盘面/磁头/扇区/磁道/柱面 本小节摘自: 硬盘基本知识(磁头.磁道.扇区.柱面) - 博客园 一张磁盘并不是拿过来直接用,需要先分区. 磁盘本身有很多sector(扇区).c ...

  4. cf1809e(edu145e)

    1 /* 2 _ooOoo_ 3 o8888888o 4 88" . "88 5 (| -_- |) 6 O\ = /O 7 ____/`---'\____ 8 .' \\| |/ ...

  5. Thread面试题

    面试题目录https://www.cnblogs.com/Kaelthas/p/15005844.html 1.一个Thread对象代表一个线程,同一个线程能否多次启动? 不能,在Thread类中变量 ...

  6. Vue2的组件中data为什么不能使用对象

    当一个组件被定义,data 必须声明为返回一个初始数据对象的函数,因为组件可能被用来创建多个实例. 如果 data 仍然是一个纯粹的对象,则所有的实例将共享引用同一个数据对象!通过提供 data 函数 ...

  7. 理解Java程序的执行

    main 方法 public class Solution { public static void main(String[] args) { Person person = new Person( ...

  8. 31-dll

    webpack.dll.js /** * 使用dll技术,对某些库(第三方库:jquery.react.vue...)进行单独打包 * 当你运行 webpack 时,默认查找 webpack.conf ...

  9. 洛谷:P5716日份天数

    题目描述 输入年份和月份,输出这一年的这一月有多少天.需要考虑闰年. 输入格式 输入两个正整数,分别表示年份 \(y\) 和月数 \(m\),以空格隔开. 输出格式 输出一行一个正整数,表示这个月有多 ...

  10. VUEX(状态管理)之憨憨篇

    1.导入vuex包 import vuex from 'vuex' 2.注册vuex到vue中 vue.use(vuex) 3.new vuex.store() 得到一个数据存储对象 var stor ...