• 题意:给你一个只含\(+\)和\(-\)的字符串,给你一个数\(x\),\(x\)初始为\(0\),随着字符串的遍历会加一减一,现在有\(m\)个询问,每个询问给出一个区间\([l,r]\)表示将这个区间内的字符串去除,得到新的字符串,问遍历新字符串后,\(x\)取到的值最多有多少.
  • 题解:这题的关键是,\(x\)的值是一一变化的,所以从最大值\(mx\)变为\(mi\),最多有\(mx\)-\(mi\)+1个数,根据题意,区间会把字符串分成两段(不考虑两段的情况),前面的一段很好处理,我们用结构体记录前缀的最小、最大值和前缀和\((l_{mi},l_{mx},l_{full})\),那么我们只要知道后面一段的前缀最小、最大值\((r_{mi},r_{mx})\),那么答案是就是\(max(l_{mx},l_{full}+max(r_{mx},0))-min(l_{mi},l_{full}+min(r_{mi},0))\).关键是后面一段的前缀情况不太容易直观看出来,具体看代码吧.
  • 代码:
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
#define rep(a,b,c) for(int a=b;a<=c;++a)
#define per(a,b,c) for(int a=b;a>=c;--a)
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) {return a/gcd(a,b)*b;} int _; struct misaka{
int mi;
int mx=INF;
int full;
}pre[N]; int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>_;
while(_--){
int n,m;
cin>>n>>m; string s;
cin>>s; vector<PII> suf(n+1);
suf[n]={0,0};
pre[0].mi=0;
pre[0].mx=0;
pre[0].full=0; int mx=0;
int mi=INF;
int cur=0; rep(i,0,n-1){
cur+=(s[i]=='+')?1:-1;
mx=max(mx,cur);
mi=min(mi,cur);
mi=min(mi,0);
pre[i+1].mi=mi;
pre[i+1].mx=mx;
pre[i+1].full=cur;
} cur=0;
mx=-INF;
mi=INF;
per(i,n-1,0){
cur=pre[i+1].full;
mx=max(mx,cur);
mi=min(mi,cur);
suf[i].fi=mi-pre[i].full;
suf[i].se=mx-pre[i].full;
//cout<<cur<<' '<<suf[i].fi<<' '<<suf[i].se<<'\n';
} int l,r;
rep(i,1,m){
cin>>l>>r;
l--,r--;
//cout<<l<<' '<<r<<' '<<pre[l].mi<<' '<<pre[l].mx<<' '<<pre[l].full<<' '<<suf[r+1].fi<<' '<<suf[r+1].se<<'\n';
int _mi=min(pre[l].mi,pre[l].full+suf[r+1].fi);
int _mx=max(pre[l].mx,pre[l].full+suf[r+1].se); cout<<_mx-_mi+1<<'\n';
}
} return 0;
}

Educational Codeforces Round 102 (Rated for Div. 2) D. Program (思维,前缀和)的更多相关文章

  1. Educational Codeforces Round 102 (Rated for Div. 2)

    比赛地址 A(水题) 题目链接 题目: 给出一个数组\(a\)并能进行一个操作使得数组元素更改为数组任意其他两元素之和,问是否可以让数组元素全部小于等于\(d\) 解析: 排序后判断最大值是否小于等于 ...

  2. Educational Codeforces Round 102 (Rated for Div. 2) B. String LCM (构造,思维)

    题意:给你两个字符串\(a\)和\(b\),找出它们的\(lcm\),即构造一个新的字符串\(c\),使得\(c\)可以由\(x\)个\(a\)得到,并且可以由\(y\)个\(b\)得到,输出\(c\ ...

  3. Educational Codeforces Round 48 (Rated for Div. 2)异或思维

    题:https://codeforces.com/contest/1016/problem/D 题意:有一个 n * m 的矩阵, 现在给你 n 个数, 第 i 个数 a[ i ] 代表 i 这一行所 ...

  4. Educational Codeforces Round 60 (Rated for Div. 2)D(思维,DP,快速幂)

    #include <bits/stdc++.h>using namespace std;const long long mod = 1e9+7;unordered_map<long ...

  5. Educational Codeforces Round 60 (Rated for Div. 2)E(思维,哈希,字符串,交互)

    #include <bits/stdc++.h>using namespace std;int main(){ string t; cin>>t; int n=t.size() ...

  6. Educational Codeforces Round 67 (Rated for Div. 2) B题【前缀+二分】【补题ING系列】

    题意:给出一个字符串s, 可以从左往右拿走s的字符, 至少要到s的第几个位置才能拼成t 思路:用二维数组记录前缀,然后二分即可. #include<bits/stdc++.h> using ...

  7. Educational Codeforces Round 73 (Rated for Div. 2)E(思维,博弈)

    //这道题博弈的核心就是不能让后手有一段只能放b而长度不够放a的段,并且先手要放最后一次#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h> ...

  8. Educational Codeforces Round 94 (Rated for Div. 2) D. Zigzags (枚举,前缀和)

    题意:有一长度为\(n(4\le n\le 3000)\)的数组,选择四个位置\((i,j,k,l)\ (1\le i<j<k\le n)\),使得\(a_i=a_k\)并且\(a_j=a ...

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

随机推荐

  1. Windows下如何玩转火热的go-zero

    作者:阿啄debugIT 前言 go-zero 是一个集成了各种工程实践的 web 和 rpc 框架.通过弹性设计保障了大并发服务端的稳定性,经受了充分的实战检验. go-zero 包含极简的 API ...

  2. Flutter 布局类组件:流式布局(Wrap和Flow)

    前言 把超出屏幕显示范围会自动折行的布局称为流式布局.Flutter中通过Wrap和Flow来支持流式布局,将Row换成Wrap后溢出部分则会自动折行. Wrap 接口描述 Wrap({ Key ke ...

  3. Linux 入门教程:00 Background

    Linux 为何物? 就是一个操作系统. Linux 历史: 操作系统始于二十世纪五十年代,当时的操作系统能运行批处理程序.批处理程序不需要用户的交互,它从文件或者穿孔卡片读取数据,然后输出到另外一个 ...

  4. 【二分搜索树】1、二分查找法的实现 - Binary Search

    简单记录 - bobo老师的玩转算法系列–玩转算法 - 二分搜索树 二叉搜索树 Binary Search Tree 查找问题 Searching Problem 查找问题是计算机中非常重要的基础问题 ...

  5. 【Linux】在docker上部署grafana+zabbix监控实录

    -------------------------------------------------------------------------------------------------   ...

  6. DOS的FOR命令用法总结

    鉴于dos自带的关于for命令的帮助信息看起来太简单,自己总结了一下,并增加了必要的实例,以备日后自己查阅.其中一些地方可能存在理解错误,敬请指出. [转发请注明出处]

  7. KeepAlive安装以及简单配置

    操作系统:Centos7.3 一.依赖安装 首先安装相关依赖: yum install -y gcc openssl-devel popt-devel yum -y install libnl lib ...

  8. 1、进程管理常用命令和进程ID

    常用命令 1. ps (英文全拼:process status)命令用于显示当前进程的状态,类似于 windows 的任务管理器. 详细介绍参照:https://www.runoob.com/linu ...

  9. 【IDEA】Lombok--是否值得我们去使用

    官网 https://projectlombok.org/ 简介 Project Lombok is a java library that automatically plugs into your ...

  10. 在OpenDaylight controller上开发App

    安装环境:Ubuntu18.04 一.安装依赖 1. 安装JDK: sudo apt update sudo apt install openjdk-8-jdk-headless 选择默认的 JDK: ...