Educational Codeforces Round 102 (Rated for Div. 2) D. Program (思维,前缀和)

- 题意:给你一个只含\(+\)和\(-\)的字符串,给你一个数\(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 (思维,前缀和)的更多相关文章
- Educational Codeforces Round 102 (Rated for Div. 2)
比赛地址 A(水题) 题目链接 题目: 给出一个数组\(a\)并能进行一个操作使得数组元素更改为数组任意其他两元素之和,问是否可以让数组元素全部小于等于\(d\) 解析: 排序后判断最大值是否小于等于 ...
- Educational Codeforces Round 102 (Rated for Div. 2) B. String LCM (构造,思维)
题意:给你两个字符串\(a\)和\(b\),找出它们的\(lcm\),即构造一个新的字符串\(c\),使得\(c\)可以由\(x\)个\(a\)得到,并且可以由\(y\)个\(b\)得到,输出\(c\ ...
- Educational Codeforces Round 48 (Rated for Div. 2)异或思维
题:https://codeforces.com/contest/1016/problem/D 题意:有一个 n * m 的矩阵, 现在给你 n 个数, 第 i 个数 a[ i ] 代表 i 这一行所 ...
- 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 ...
- 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() ...
- Educational Codeforces Round 67 (Rated for Div. 2) B题【前缀+二分】【补题ING系列】
题意:给出一个字符串s, 可以从左往右拿走s的字符, 至少要到s的第几个位置才能拼成t 思路:用二维数组记录前缀,然后二分即可. #include<bits/stdc++.h> using ...
- Educational Codeforces Round 73 (Rated for Div. 2)E(思维,博弈)
//这道题博弈的核心就是不能让后手有一段只能放b而长度不够放a的段,并且先手要放最后一次#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h> ...
- 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 ...
- 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 ...
随机推荐
- 【JavaWeb】Servlet 程序
Servlet 程序 Servlet Servlet 是在 Web 服务器中运行的小型 Java 程序.Servlet 通常通过 HTTP(超文本传输协议)接收和响应来自 Web 客户端的请求. ...
- SpringBoot整合Shiro完成认证
三.SpringBoot整合Shiro思路 首先从客户端发来的所有请求都经过Shiro过滤器,如果用户没有认证的都打回去进行认证,认证成功的,再判断是否具有访问某类资源(公有资源,私有资源)的权限,如 ...
- 简单解析一下 Mybatis 常用的几个配置
目录 核心配置文件 环境配置(environments) 属性(properties) 类型别名(typeAliases) 映射器(mappers) Mybatis 参考:https://mybati ...
- ctfshow—web—web签到题
打开靶机,发现只有一句话 查看源码 发现一段字符串,猜是base64加密 拿到flag
- Os-hackNos-特权文件提权
一 信息收集 netdiscover -i eth0 -r 10.10.10.0/24 扫描ip nmap -sP 192.168.43.0/24 扫描开放的端口 使用"-sP"选 ...
- uni-app调用wifi接口
微信小程序条件渲染 在小程序app.json中添加 需要先获取位置信息 "permission": { "scope.userLocation": { &quo ...
- centos&linux
who am i 查看是哪一个用户 init 0关机 ifconfig用于配置网络或显示当前网络接口的状态 eth0是网卡的名字 第一行:flags后面的up指的是网卡处于运行状态,running连接 ...
- 忒修斯的Mac
我有一台Mac笔记本,用了快6年了,当初买它的时候还借了几千块. 三年前,它的屏幕坏了,修理的方式就是直接换屏,而换屏其实就是上半部分连壳带屏幕整个换掉,简单的说:另一台电脑的上半身嫁接过来. 今年, ...
- 向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾。
1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.text.SimpleDateFormat; ...
- Linux下nf_conntrack(最全面)_董明磊-CSDN博客_nf_conntrack https://blog.csdn.net/qq_35299863/article/details/79530732
Linux下nf_conntrack(最全面)_董明磊-CSDN博客_nf_conntrack https://blog.csdn.net/qq_35299863/article/details/79 ...