Educational Codeforces Round 151 (Rated for Div
C. Strong Password
给定一个字符串\(s\),一个密码的长度\(m\),下界字符串\(l\)和上界字符串\(r\),上下界字符串长度均为\(m\),且字符只在0~9范围内,上界字符串的第 \(i\) 位非严格大于下界字符串的第 \(i\) 位,密码的第 \(i\) 位需要位于 \([l_i, r_i]\) 内。问是否存在一个密码不是\(s\)的子序列?
\(1 \leq m \leq 10\)
\(1 \leq |s| \leq 3\times 10^5\)
题解:贪心 + 枚举
- 因为\(m\)的范围比较小,所以我们不妨考虑枚举密码的每一位
- 根据题意得知,第\(i\)位密码\(ch\)必须保证在\([l_i,r_i]\)范围内
- 因为题目给出的是子序列,所以我们一旦选定了第\(i\)位密码为\(ch\),假设\(ch\)在\(s\)中存在且第一次出现的位置为\(pos\),那么第\(i+1\)位密码应该从\(s\)的第\(pos\)位之后开始搜索
- 我们不妨设\(s\)的第\(pos\)位之后的部分字符串为\(t\)
- 如果我们枚举到的密码在\(t\)中不存在,那么说明密码一定能被构造出来
- 如果第\(i\)位密码的所有情况在\(t\)中都存在的话,我们贪心地选择\([l_i,r_i]\)中第一次出现且在\(s\)中下标最大的字符作为第\(i\)位密码
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 2e5 + 10, M = 4e5 + 10;
void solve()
{
string s;
cin >> s;
int m;
cin >> m;
string l, r;
cin >> l >> r;
bool flag = false;
int p = 0;
for (int i = 0; i < m; ++i)
{
int mx = 0;
for (char j = l[i]; j <= r[i]; ++j)
{
int t = s.find(j, p);
if (t == -1)
{
flag = true;
break;
}
mx = max(mx, t);
}
p = mx + 1;
if (flag)
{
cout << "YES" << endl;
return;
}
}
cout << "NO" << endl;
}
signed main(void)
{
Zeoy;
int T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
D. Rating System
给定对局数\(n\),以及每个对局会使得\(rating\)的变化值\(a_i\),初始\(rating\)为\(0\)。问给定的低保线\(k\)(分数到\(k\)后无论\(rating\)怎么变不会低于\(k\))为多少时,\(n\)个对局后玩家的\(rating\)最高
题解:思维 + 最大后缀
- 容易发现答案应该是前缀和\(pre_i\)中的一个,但是如果我们枚举所有的前缀和复杂度显然为\(O(n^2)\),所以我们不妨逆向思维来考虑这个问题
- 设\(suf\_max_i\)为第\(i\)位之后的最大后缀,易得\(suf\_max[i] = max(suf[i+1],pre[n]-pre[i])\)
- 我们手模发现,对于任意一个\(k = pre_i\),最终的\(rating\)为\(pre_i + suf\_max_i\)
- 这样的话,时间复杂度为\(O(n)\)
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 3e5 + 10, M = 4e5 + 10;
int n;
int a[N];
int pre[N];
void solve()
{
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = 1; i <= n; ++i)
pre[i] = pre[i - 1] + a[i];
vector<int> suf_max(n + 10);
for (int i = n; i >= 0; i--)
suf_max[i] = max(suf_max[i + 1], pre[n] - pre[i]);
int k = 0;
for (int i = 1; i <= n; ++i)
{
if (pre[k] + suf_max[k] < pre[i] + suf_max[i])
k = i;
}
cout << max(0LL, pre[k]) << endl;
}
signed main(void)
{
Zeoy;
int T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
Educational Codeforces Round 151 (Rated for Div的更多相关文章
- 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 ...
随机推荐
- 五子棋AI:实现逻辑与相关背景探讨(上)
绪论 本合集将详细讲述如何实现基于群只能遗传算法的五子棋AI,采用C++作为底层编程语言 本篇将简要讨论实现思路,并在后续的文中逐一展开 了解五子棋 五子棋规则 五子棋是一种经典的棋类游戏,规则简单却 ...
- JavaScript中class的静态属性和静态方法
我们可以把一个方法赋值给类的函数本身,而不是赋给它的 "prototype" .这样的方法被称为 静态的(static). 例如这样: class Animal { static ...
- docker启动问题: Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
系统环境:centos 7 docker版本:Docker version 26.1.4, build 5650f9b 问题:Job for docker.service failed because ...
- Angular 18+ 高级教程 – Reactive Forms
前言 上一篇的 Ajax 和这一篇的表单 (Form) 都是前端最最最常见的需求. 为此,Angular 分别提供了两个小型库来帮助开发者实现这些需求: Ajax – HttpClient Form ...
- BOM – Navigator SendBeacon
介绍 游览器专门做给 tracking 用的接口. 从前我们想 tracking 用户点击 anchor 是比较麻烦的. 因为 click 事件触发后, 想发 ajxax 去做 tracking re ...
- IOI2000 邮局 加强版 题解
[IOI2000] 邮局 加强版 题解 考虑动态规划,设 \(f_{i,j}\) 为经过了 \(i\) 个村庄,正在建第 \(j\) 个邮局的最优距离. 以及 \(w_{i,j}\) 表示区间 \( ...
- 致敬传奇 Kruskal 重构树题硬控我三小时
NOI2018 归程 存边的数组拿来干两件事,忘了清空了,其实最好开两个的 dfs 没开 vis 导致不知道为什么出现的绕圈 倍增的 fa[i][j] 定义的时候前面是 \(2^{i}\) 写着写着记 ...
- 智能化IT运维平台建设方案,基于智和信通运维体系的高敏捷二次开发
随着企业信息进程不断加速,运维人员需要面对越来越复杂的业务和越来越多样化的用户需求,不断扩展的应用需要越来越合理的模式.越来越智能的工具来保障运维能灵活便捷.安全稳定地开展.企业网络规模的不断扩大,从 ...
- Flutter将视频或图文分享到抖音
如何在 Flutter 中分享视频到抖音 话不多说,先上效果: 原理 发布内容至抖音 H5 场景_移动/网站应用_抖音开放平台 (open-douyin.com) 本教程没有接入抖音原生 SDK 以及 ...
- android ion
1. 简介 Android的ION子系统的目的主要是通过在硬件设备和用户空间之间分配和共享内存,实现设备之间零拷贝共享内存.说来简单,其实不易.在Soc硬件中,许多设备可以进行DMA,这些设备可能有不 ...
