Educational Codeforces Round 90 (Rated for Div. 2) C. Pluses and Minuses(差分)
题目链接:https://codeforces.com/contest/1373/problem/C
题意
给出一个只含有 $+$ 或 $-$ 的字符串 $s$,按如下伪代码进行操作:
res = 0
for init = 0 to inf
cur = init
ok = true
for i = 1 to |s|
res = res + 1
if s[i] == '+'
cur = cur + 1
else
cur = cur - 1
if cur < 0
ok = false
break
if ok
break
计算最终 $res$ 的值。
题解
$res$ 即所有位置被访问次数的总和。
模拟伪代码,计算走到每个位置时的值,如果当前位置的值比之前所有位置的都要小,则此时 $cur < 0$,意味着当前位置及之前的字符都要再走一遍,而且下一次走到当前位置时 $cur = 0$ 。
代码一
或许更好理解的差分写法。
#include <bits/stdc++.h>
using namespace std; void solve() {
string s; cin >> s;
int n = s.size();
int cnt[n] = {}; //cnt[i] 为位置 i 被访问的次数
cnt[0] = 1;
int mi = 0, now = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '+') ++now;
else --now;
if (now < mi) {
mi = now;
++cnt[0];
--cnt[i + 1];
}
}
for (int i = 1; i < n; i++)
cnt[i] += cnt[i - 1];
cout << accumulate(cnt, cnt + n, 0LL) << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
代码二
代码一的简化版。
#include <bits/stdc++.h>
using ll = long long;
using namespace std; void solve() {
string s; cin >> s;
ll ans = s.size();
int mi = 0, now = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '+') ++now;
else --now;
if (now < mi) {
mi = now;
ans += i + 1;
}
}
cout << ans << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
Educational Codeforces Round 90 (Rated for Div. 2) C. Pluses and Minuses(差分)的更多相关文章
- Educational Codeforces Round 90 (Rated for Div. 2) D. Maximum Sum on Even Positions(dp)
题目链接:https://codeforces.com/contest/1373/problem/D 题意 给出一个大小为 $n$ 的数组 $a$,下标为 $0 \sim n - 1$,可以进行一次反 ...
- Educational Codeforces Round 90 (Rated for Div. 2) B. 01 Game(字符串博弈)
题目链接:https://codeforces.com/contest/1373/problem/B 题意 给出一个二进制串 $s$,Alica 和 Bob 每次可以选择移去 $s$ 中的一个 $10 ...
- Educational Codeforces Round 90 (Rated for Div. 2) A. Donut Shops(数学)
题目链接:https://codeforces.com/contest/1373/problem/A 题意 有两种包装的甜甜圈,第一种 $1$ 个 $a$ 元,第二种 $b$ 个 $c$ 元,问买多少 ...
- 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 ...
随机推荐
- mysql: Character set 'utf8mb4' is not a compiled character set and is not specified in the '/usr/share/mysql/charsets/Index.xml' file
mysql: Character set 'utf8mb4' is not a compiled character set and is not specified in the '/usr/sha ...
- innnodb_doublewrite
有写场景下,双写缓冲确实没必要,例如,你也许像在备库上禁用双写缓冲,此外,一些文件系统,例如zfs做了同样的事,所以,没必要再让innodb做一遍. innodb_double_write=0 即可关 ...
- ctfshow—web—web2
打开靶机,根据提示是SQL注入 打开后看到登录窗口 方法一.手工注入 抓取数据包 开始SQL注入测试 利用万能密码,登录成功 查看回显位置 查询数据库 查询数据库内数据表 如果想整齐一点显示可以添加g ...
- CTFhub刷题记录
一 [WesternCTF2018]shrine 没什么好说的,SSTI模版注入类问题,过滤了()但是我们不慌.开始注入,{{29*3}}测试通过. 发现是jinjia2的模版注入.关键点在于没有() ...
- CTF实验吧-WEB题目解题笔记(1)简单的登陆题
1.简单的登陆题 解题链接: http://ctf5.shiyanbar.com/web/jiandan/index.php Burp抓包解密 乱码,更换思路.尝试id intruder 似乎也没什 ...
- 工作记录:记一次线上ZK掉线问题排查
目录 问题的发现 zk的情况以及分析 总结 问题的发现 最早问题的发现在于用户提的,用户提出他支付时支付失败,过了一会儿再试就好了,于是翻日志,查询到当时duboo调用出现了下类错误: [TraceI ...
- 全网最详细的PyCharm+Anaconda的安装。
目录 PyCharm的安装 一.下载安装包 1.安装网站 2.在导航栏输入网址回车 3.点击 DOWNLOAD. 4.它有专业版和社区版,我们下载社区版就可以使用了.(专业版要收费) 二.安装过程 5 ...
- django 中连接mysql数据库的操作步骤
django中连接mysql数据库的操作步骤: 1 settings配置文件中 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mys ...
- 【IDEA】Lombok--是否值得我们去使用
官网 https://projectlombok.org/ 简介 Project Lombok is a java library that automatically plugs into your ...
- OpenDaylight — YANG
1. 介绍 YANG 是一种用于为 NETCONF 协议建模数据的语言. YANG 将数据的层次结构建模为一棵树. 2. 节点类型 2.1 leaf 它只有一个特定类型的值,并且没有子节点. YANG ...