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的更多相关文章

  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. 工具 – VS Code Extensions

    前言 分享我用着的 Extensions. Angular Language Service 不用介绍,用 Angular 的必装. Better Comments 让注释有多点颜色 more col ...

  2. TypeScript – tsconfig

    前言 上一篇 TypeScript – Get Started 使用了命令 tsc index.ts --module es2015 很少人会在命令时给写 config, 更正规的做法是创建一个 ts ...

  3. 全面掌握 Jest:从零开始的测试指南(上篇)

    随着JavaScript在前后端开发中的广泛应用,测试已成为保证代码质量的关键环节. 为什么需要单元测试 在我们的开发过程中,经常需要定义一些算法函数,例如将接口返回的数据转换成UI组件所需的格式.为 ...

  4. el-table

    el-table-column 渲染的顺序不对,第一列被渲染到最后 el-table-column 必须作为 el-table 或 el-table-column 的直接子元素使用,如果不是将会导致标 ...

  5. Hive 2.3.2安装

    一.安装mysql 安装MySQL服务器端和MySQL客户端: •安装: – yum install mysql – yum install mysql-server •启动: – /etc/init ...

  6. qemu的使用

    一.QEMU的运行模式 直接摘抄自己<揭秘家用路由器0day漏洞挖掘技术>,网上查了一下也没有找到令人满意的QEMU的使用说明,就采用这本书上的介绍.如果后期能够找到比较满意的QEMU的使 ...

  7. 10月《中国数据库行业分析报告》已发布,深度剖析甲骨文大会Oracle技术新趋势

    为了帮助大家及时了解中国数据库行业发展现状.梳理当前数据库市场环境和产品生态等情况,从2022年4月起,墨天轮社区行业分析研究团队出品将持续每月为大家推出最新<中国数据库行业分析报告>,持 ...

  8. innerText 和 inner HTML 的区别

    获取内容时: innerText会自动删除空格和换行:innerHTML会保留空格和换行: <body> <div>获 取内 容</div> <script& ...

  9. 业务上线在即,ODBC应用程序性能频频掉线怎么搞?

  10. 网页转换为PDF的方法 Python

    前言 近期有些文档是在网站上的,量非常大.加之对于纸质书的喜爱,想把他们整合到一个PDF文档中,然后交由拼多多的老熟人打印店给打一下. 但是这个网站网页转PDF有很多在线网站可以用,不过只能转一个页面 ...