Codeforces Round #603 (Div. 2)E

http://codeforces.com/contest/1263/problem/E

题意:求合法的括号序列

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lson root<<1,l,midd
#define rson root<<1|1,midd+1,r
#define pb push_back
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int M=1e6+6;
int tree[M<<2];
int lzmi[M<<2];///最小前缀和
int lzma[M<<2];///最大前缀和
char s[M];
void up(int root){
    tree[root]=tree[root<<1]+tree[root<<1|1];
    lzma[root]=max(lzma[root<<1],tree[root<<1]+lzma[root<<1|1]);///右区间来选前缀时要考虑到左区间带进来的贡献
    lzmi[root]=min(lzmi[root<<1],tree[root<<1]+lzmi[root<<1|1]);
}
void update(int p,int v,int root,int l,int r){
    if(l==r){
        tree[root]=lzmi[root]=lzma[root]=v;
        return ;
    }
    int midd=(l+r)>>1;
    if(p<=midd)
        update(p,v,lson);
    else
        update(p,v,rson);
    up(root);
}
int main(){
    int n;
    scanf("%d%s",&n,s);
    int nowpos=1;
    for(int i=0;i<n;i++){
        if(s[i]==‘L‘)
            nowpos=max(1,nowpos-1);
        else if(s[i]==‘R‘)
            nowpos++;
        else if(s[i]==‘(‘)
            update(nowpos,1,1,1,n);
        else if(s[i]==‘)‘)
            update(nowpos, -1,1,1,n);
        else
            update(nowpos,0,1,1,n);
        ///若全区间和不为0,则证明左括号数和右括号数不等
        ///若全区间最小前缀和不为0,则证明至少有一个右括号没有被左括号对应,
        if(lzmi[1]<0||tree[1]!=0)
            printf("-1 ");
        else
            printf("%d ",lzma[1]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/starve/p/12038280.html

时间: 2024-07-31 03:04:38

Codeforces Round #603 (Div. 2)E的相关文章

Codeforces Round #603 (Div. 2) E. Editor 线段树

E. Editor The development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text. Your editor consists of a line with infinite length and cursor, which points to the current character. Please note that

Codeforces Round #603 (Div. 2) E. Editor(线段树)

链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text. Your editor consists of a line with infinite length and cursor, which point

Codeforces Round #603 (Div. 2) A. Sweet Problem(数学)

链接: https://codeforces.com/contest/1263/problem/A 题意: You have three piles of candies: red, green and blue candies: the first pile contains only red candies and there are r candies in it, the second pile contains only green candies and there are g ca

Codeforces Round #603 (Div. 2) B. PIN Codes

链接: https://codeforces.com/contest/1263/problem/B 题意: A PIN code is a string that consists of exactly 4 digits. Examples of possible PIN codes: 7013, 0000 and 0990. Please note that the PIN code can begin with any digit, even with 0. Polycarp has n (

Codeforces Round #603 (Div. 2) C. Everyone is a Winner! (数学)

链接: https://codeforces.com/contest/1263/problem/C 题意: On the well-known testing system MathForces, a draw of n rating units is arranged. The rating will be distributed according to the following algorithm: if k participants take part in this event, t

Codeforces Round #603 (Div. 2) F. Economic Difficulties dp

F. Economic Difficulties An electrical grid in Berland palaces consists of 2 grids: main and reserve. Wires in palaces are made of expensive material, so selling some of them would be a good idea! Each grid (main and reserve) has a head node (its num

Codeforces Round #603 (Div. 2) C.Everyone is A Winner!

tag里有二分,非常的神奇,我用暴力做的,等下去看看二分的题解 但是那个数组的大小是我瞎开的,但是居然没有问题233 #include <cstdio> #include <cmath> #include <cstring> using namespace std; const int N = 1e7; int c[N]; int main() { int t; scanf("%d", &t); while (t--) { int n; sc

Codeforces Round #603 (Div. 2)

传送门 感觉脑子还是转得太慢了QAQ,一些问题老是想得很慢... A. Sweet Problem 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/29 22:36:19 */ #include <iostream> #include <algorithm> #include <vector> #include <cmath> #include <set> #include <ma

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i