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 ...
随机推荐
- Mac最新Flutter环境搭建运行和对比理解声明式UI
前言 这段时间一直都在学习和写关于SwiftUI的东西,前面也总结了四篇文章来大体上说了下Demo中功能实现的一些细节,后面准备开始了解学习一下Flutter,争取在年前能再用Flutter写一份项目 ...
- 基于 MPI/OpenMP 混合编程的大规模多体(N-Body)问题仿真实验
完整代码: #include <iostream> #include <ctime> #include <mpi.h> #include <omp.h> ...
- Azure Key Valut 简介
Azure Key Vault(密钥库)是用于安全地存储和访问Secret的云服务,Secret是需要严格控制访问权限的内容,例如API密钥,密码,证书或加密密钥.Key Vault Service支 ...
- ClickHouse安装使用(单机、集群、高可用)
Clickhouse版本:20.3.6.40-2 安装包地址:https://repo.yandex.ru/clickhouse/rpm/stable/x86_64/ 一.单机版 1.安装依赖 yum ...
- 2019 Eclipse的下载与安装教程
Eclipse 是一个开放源代码的.基于Java的可扩展开发平台,可以免费下载使用. 首先我们先进入这个软件的官网:https://www.eclipse.org/ 点击这个网页download下载: ...
- 【Linux】dd命令进行磁盘备份
运用dd命令,将/dev/sdb磁盘中所有的数据全部备份到/dev/sdc磁盘上,需要的命令如下 dd if=/dev/sdb of=/dev/sdc bs=1024k 说明,if是需要备份的磁盘 ...
- springAOP的概述及使用
Spring AOP SpringAOP是Spring中非常重要的功能模块之一,该模块提供了面向切面编程,在事务处理,日志记录,安全控制等操作中广泛使用. SpringAOP的基本概念 AOP的概念 ...
- spring data JPA 使用EntityentiListeners实现数据审计功能设计
当系统中有审计需求时,特别是需要对某些数据进行动态监控时,我们可以使用EntityentiListeners来实现,当然这是基于使用JPA而不是mybatis的情况下. 当前我们的需求场景: 1.需要 ...
- jQuery 点击input框标题收起
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 使用amoeba实现mysql读写分离
使用amoeba实现mysql读写分离 1.什么是amoeba? Amoeba(变形虫)项目,专注 分布式数据库 proxy 开发.座落与Client.DB Server(s)之间.对客户端透明. ...