题目链接: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(差分)的更多相关文章

  1. 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$,可以进行一次反 ...

  2. Educational Codeforces Round 90 (Rated for Div. 2) B. 01 Game(字符串博弈)

    题目链接:https://codeforces.com/contest/1373/problem/B 题意 给出一个二进制串 $s$,Alica 和 Bob 每次可以选择移去 $s$ 中的一个 $10 ...

  3. Educational Codeforces Round 90 (Rated for Div. 2) A. Donut Shops(数学)

    题目链接:https://codeforces.com/contest/1373/problem/A 题意 有两种包装的甜甜圈,第一种 $1$ 个 $a$ 元,第二种 $b$ 个 $c$ 元,问买多少 ...

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

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

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

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

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

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

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

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

随机推荐

  1. ElasticSearch- 单节点 unassigned_shards 故障排查

    故障现象 在部署ELK的单机环境,当连接Kibana时候提示下面错误,即使重启整个服务也是提示Kibana server is not ready. {"message":&quo ...

  2. python安装whl包时出现的问题解决:is not a supported wheel on this platform

    @ 目录 一.问题 二.查找问题 三.问题解决 一.问题 1.下载一个twisted包 安装Twisted,进入https://www.lfd.uci.edu/~gohlke/pythonlibs 下 ...

  3. 【剑指 Offer】04.二维数组中的查找

    题目描述 在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...

  4. kubernets之pod的标签的使用

    一 对于kubernets里面的资源标记完成之后的使用 1 node节点标签的应用(将资源调度到特定的节点上) #kubia-gpu.ymlapiVersion: v1 kind: Pod metad ...

  5. python optparse模块的用法

    引用原博主文章链接: https://www.cnblogs.com/darkpig/p/5717902.html

  6. Nginx和Tomcat配置SSL实现https访问

    环境:CentOS 7 Nginx版本: nginx/1.18.0 1. 安装nginx 详细步骤可以参考如下官网:http://nginx.org/en/linux_packages.html#RH ...

  7. 1V升3V芯片,1V升3.3V芯片,大电流的,低功耗

    一般来说,1V的电压实在很低了,即使是干电池的话,再1V时,也是基本属于没电状态了.还有一种是干电池输出电流大时,也会把干电池的电压从1.5V拉低到1V左右. 更多的是客户对于1V时要能升到3V或者3 ...

  8. 导出带有图片的excel

    public static void main(String[] args) { try { FileOutputStream out = new FileOutputStream("d:\ ...

  9. Before you launch a goroutine, know when it will stop The Zen of Go

    The Zen of Go https://the-zen-of-go.netlify.app/ Ten engineering values for writing simple, readable ...

  10. asctime_s asctime

    asctime_s  asctime // rand随机数.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include "pch.h ...