Solution

首先审清题意, 这里要求的是子串而不是子序列...

我们考虑用1表示p, -1表示j. 用sum[i]表示字符串前\(i\)的前缀和. 则我们考虑一个字符串\([L, R]\)有什么要求: \(\forall x \in [L, R]\)满足\(sum[x] \ge sum[L - 1]\).

我们分别从前往后和从后往前求出以每个位置为开头的最长合法子串, 然后扔进树状数组里面查询即可.

至于怎么求以每个位置为开头最长合法子串, 我们考虑用一个单调栈来维护: 从前往后扫每个位置, 假如当前位置的\(sum\)小于栈顶的\(sum\)则弹栈, 并把以栈顶为开头的最长合法子串的末尾设为当前位置的前一位. 弹栈结束后, 插入当前位置即可.

#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std;
const int N = (int)1e6, INF = (int)2e9;
int n;
struct record
{
int L, R;
inline int operator <(const record &a) const {return R < a.R;}
}rec[N + 1];
struct segmentTree
{
int mn[N << 2];
inline segmentTree() {memset(mn, 127 ,sizeof(mn));}
void insert(int u, int L, int R, int pos)
{
mn[u] = min(mn[u], pos);
if(L == R) return;
if(pos <= L + R >> 1) insert(u << 1, L, L + R >> 1, pos);
else insert(u << 1 | 1, (L + R >> 1) + 1, R, pos);
}
inline void insert(int pos) {insert(1, 1, n, pos);}
int query(int u, int L, int R, int pos)
{
if(L >= pos) return mn[u];
int mid = L + R >> 1;
if(pos <= mid) return min(query(u << 1, L, L + R >> 1, pos), query(u << 1 | 1, (L + R >> 1) + 1, R, pos));
else return query(u << 1 | 1, (L + R >> 1) + 1, R, pos);
}
inline int query(int pos) {return query(1, 1, n, pos);}
}seg;
struct binaryIndexedTree
{
int mx[N + 1];
inline binaryIndexedTree() {memset(mx, -1, sizeof(mx));}
inline void insert(int pos, int x)
{
for(int i = pos; i <= n; i += i & - i)
mx[i] = max(mx[i], x);
}
inline int query(int pos)
{
int res = -1;
for(int i = pos; i; i -= i & - i) res = max(res, mx[i]);
return res;
}
}BIT;
int main()
{ #ifndef ONLINE_JUDGE freopen("bar.in", "r", stdin);
freopen("bar.out", "w", stdout); #endif scanf("%d\n", &n);
static int a[N + 1];
for (int i = 1; i <= n; ++ i) a[i] = getchar() == 'p' ? 1 : -1;
static int stk[N + 1], sum[N + 2];
int tp = 0; stk[tp ++] = 0;
sum[0] = 0; for (int i = 1; i <= n; ++ i) sum[i] = sum[i - 1] + a[i]; sum[n + 1] = - INF;
static int f[N + 1];
for (int i = 1; i <= n + 1; ++ i)
{
while (tp && sum[i] < sum[stk[tp - 1]]) f[stk[tp - 1] + 1] = i - 1, -- tp;
stk[tp ++] = i;
}
for(int i = 1; i <= n; ++ i) rec[i].L = i, rec[i].R = f[i];
tp = 0; stk[tp ++] = n + 1;
sum[n + 1] = 0; for(int i = n; i; -- i) sum[i] = sum[i + 1] + a[i]; sum[0] = - INF;
for(int i = n; ~ i; -- i)
{
while(tp && sum[i] < sum[stk[tp - 1]]) f[stk[tp - 1] - 1] = i + 1, -- tp;
stk[tp ++] = i;
}
sort(rec, rec + n + 1);
int ans = 0;
/* for(int i = 1, p = 1; i <= n; ++ i)
{
for(; rec[p].R <= i; ++ p) seg.insert(rec[p].L);
int cur = seg.query(f[i]);
if(cur > i) continue;
else ans = max(ans, i - cur + 1);
} */
for(int i = 1, p = 1; i <= n; ++ i)
{
for(; p <= rec[i].R; ++ p) BIT.insert(f[p], p);
int cur = BIT.query(rec[i].L);
if(cur >= rec[i].L) ans = max(ans, cur - rec[i].L + 1);
}
printf("%d\n", ans);
}

2016集训测试赛(二十六)Problem A: bar的更多相关文章

  1. 2016北京集训测试赛(十六)Problem C: ball

    Solution 这是一道好题. 考虑球体的体积是怎么计算的: 我们令\(f_k(r)\)表示\(x\)维单位球的体积, 则 \[ f_k(1) = \int_{-1}^1 f_{k - 1}(\sq ...

  2. 2016北京集训测试赛(十六)Problem B: river

    Solution 这题实际上并不是构造题, 而是一道网络流. 我们考虑题目要求的一条路径应该是什么样子的: 它是一个环, 并且满足每个点有且仅有一条出边, 一条入边, 同时这两条边的权值还必须不一样. ...

  3. 2016北京集训测试赛(十六)Problem A: 任务安排

    Solution 这道题告诉我们, 不能看着数据范围来推测正解的时间复杂度. 事实证明, 只要常数足够小, \(5 \times 10^6\)也是可以跑\(O(n \log n)\)算法的!!! 这道 ...

  4. 【2016北京集训测试赛(十六)】 River (最大流)

    Description  Special Judge Hint 注意是全程不能经过两个相同的景点,并且一天的开始和结束不能用同样的交通方式. 题解 题目大意:给定两组点,每组有$n$个点,有若干条跨组 ...

  5. 2016集训测试赛(十九)Problem C: 无聊的字符串

    Solution 傻X题 我的方法是建立后缀后缀树, 然后在DFS序列上直接二分即可. 关键在于如何得到后缀树上每个字符对应的字节点: 我们要在后缀自动机上记录每个点在后缀树上对应的字母. 考虑如何实 ...

  6. 2016集训测试赛(十九)Problem A: 24点大师

    Solution 这到题目有意思. 首先题目描述给我们提供了一种非常管用的模型. 按照题目的方法, 我们可以轻松用暴力解决20+的问题; 关键在于如何构造更大的情况: 我们发现 \[ [(n + n) ...

  7. 2016集训测试赛(十八)Problem C: 集串雷 既分数规划学习笔记

    Solution 分数规划经典题. 话说我怎么老是忘记分数规划怎么做呀... 所以这里就大概写一下分数规划咯: 分数规划解决的是这样一类问题: 有\(a_1, a_2 ... a_n\)和\(b_1, ...

  8. 2016北京集训测试赛(十)Problem A: azelso

    Solution 我们把遇到一个旗子或者是遇到一个敌人称为一个事件. 这一题思路的巧妙之处在于我们要用\(f[i]\)表示从\(i\)这个事件一直走到终点这段路程中, \(i\)到\(i + 1\)这 ...

  9. 2018.7.31 Noip2018模拟测试赛(十六)

     日期: 七月最后一天  总分: 300分  难度: 提高 ~ 省选  得分: 30分(少的可怜) 我太弱了:(题目目录) T1:Mushroom追妹纸 T2:抵制克苏恩 T3:美味 失分分析:(QA ...

随机推荐

  1. git---gui使用

    1.登陆的命令: git config –global user.email "1455971532@qq.com" git config –global user.name &q ...

  2. Python虚拟机类机制之自定义class(四)

    用户自定义class 在本章中,我们将研究对用户自定义class的剖析,在demo1.py中,我们将研究单个class的实现,所以在这里并没有关于继承及多态的讨论.然而在demo1.py中,我们看到了 ...

  3. loj6392 「THUPC2018」密码学第三次小作业 / Rsa

    还是挺好做的,\((e_1,e_2)=1 \Rightarrow e_1s+e_2t=0\),\(m \equiv m^1 \equiv m^{e_1s+e_2t} \equiv c_1^s c_2^ ...

  4. 微服务化的不同阶段 Kubernetes 的不同玩法

    欢迎访问网易云社区,了解更多网易技术产品运营经验. 作为容器集群管理技术竞争的大赢家,Kubernetes已经和微服务紧密联系,采用Kubernetes的企业往往都开始了微服务架构的探索.然而不同企业 ...

  5. VMware Fusion Pro安装Ubuntu 18.04.1

  6. [译]为什么pandas有些命令用括号结尾,有些则没有?

    文章来源:https://nbviewer.jupyter.org/github/justmarkham/pandas-videos/blob/master/pandas.ipynb 方法:用括号调用 ...

  7. Linux下解压rar格式的压缩文件

    如果需要在Linux系统下解压RAR格式的压缩包,需要安装Linux版本的rar程序. 具体安装步骤如下: wget http://www.rarlab.com/rar/rarlinux-3.8.0. ...

  8. ceoi2017 Building Bridges(build)

    Building Bridges(build) 题目描述 A wide river has nn pillars of possibly different heights standing out ...

  9. 向量内积(bzoj 3243)

    Description 两个d 维向量A=[a1,a2,...,ad]与B=[b1,b2,...,bd]的内积为其相对应维度的权值的乘积和,即: 现有 n 个d 维向量x1,...,xn ,小喵喵想知 ...

  10. Ant自动打包

    在ant的官网http://ant.apache.org进行下载后apache-ant-1.8.2包 解压(存放的路径不要有中文字符) 把ant里的lib设置到环境变量:E:\Android\apac ...