题目链接: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. 使用 Admission Webhook 机制实现多集群资源配额控制

    1 要解决的问题 集群分配给多个用户使用时,需要使用配额以限制用户的资源使用,包括 CPU 核数.内存大小.GPU 卡数等,以防止资源被某些用户耗尽,造成不公平的资源分配. 大多数情况下,集群原生的 ...

  2. Django中一种常见的setting与账密保存/读取方式

    前言 在查看别人Django代码的时候,发现很多的manager文件都是类似于 #!/usr/bin/env python import os import sys if __name__ == '_ ...

  3. Spring Security OAuth2.0认证授权五:用户信息扩展到jwt

    历史文章 Spring Security OAuth2.0认证授权一:框架搭建和认证测试 Spring Security OAuth2.0认证授权二:搭建资源服务 Spring Security OA ...

  4. SpringBoot启动报端口已被占用--解决

    问题 启动SpringBoot项目后发现启动失败,控制台输出以下内容 Description: The Tomcat connector configured to listen on port 81 ...

  5. spring ioc踏出第一步

    spring IOC(容器) AOP(面向切面编程) IOC容器:他的功能就是可以整合像 Structs2 .Hibernate.Mybatis: IOC:控制反转:所谓的控制就是控制资源的获取方法, ...

  6. CentOS | python3.7安装指南

    前言: centos系统本身默认安装有python2.x,版本x根据不同版本系统有所不同 可通过 python --V 或 python --version 查看系统自带的python版本 有一些系统 ...

  7. SDUST数据结构 - chap5 数组与广义表

    选择题:

  8. oracle视图添加hint

    /* Formatted on 2019/8/6 下午 02:51:21 (QP5 v5.163.1008.3004) */ SELECT DB FROM ( SELECT /*+ index(A.r ...

  9. C++导言与入门

    写在开始 计算机编程语言: Remember that a program is just a sequence of instructions telling a computer what to ...

  10. django中的几种返回模版的方式

    redirect方法-----(重定向) # 首先导入redirect方法, from django.shortcuts import redirect 在函数中写一个返回值 return redir ...