Parenthesis

来源:互联网 发布:网络信号哪家强 编辑:程序博客网 时间:2024/06/11 05:26

题目链接

题意:n个字母,m个询问,每个询问两个整数a, b。问当交换第a个字母和第b个字母后,原序列是否还是一个平衡序列。

          数据保证给出一个平衡的原始序列。

思路:括号匹配问题。

          定义一个pre数组,表示前缀和。当碰到 “(” 时,pre[i] = pre[i-1] + 1, 碰到“)”时,pre[i] = pre[i-1] - 1;   只有当数组pre中有元素<0的时候才会出现不平衡的状态。

          交换两个位置的括号分为以下三种情况:

                   1、两个括号一样,交换肯定平衡。

                   2、第a个字母是“)”,只讨论第b个字母是“(”的情况【因为b是“)”的情况前面已经包含了】,因为“(”向前移动,“)”向后移动会使前缀和增加,不会降低到小于0而导致不平衡。

                  3、只剩下第a个字母是“(”, 第“b”个字母是“)”的情况,因为这会导致区间【a, b-1】的前缀和-2, 其余不会影响。 所以我们只要找到【a, b-1】的区间的前缀和的最小值即可,只要最小值 >= 2,交换后就可以平衡,否则不会平衡。

          这里用了线段树和RMQ来查询最小值。

线段树代码: 828ms, 5.3M

/// He  renders  landscapes  with  great  skill  and  artistry.#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cstdlib>#include <cmath>#include <iterator>#include <cctype>#include <sstream>#include <string>#include <vector>#include <set>#include <map>#include <stack>#include <deque>#include <queue>#include <list>#include <functional>#include <ctime>#include <bitset>//#pragma comment(linker, "/STACK:102400000, 102400000")#define debug     puts("+******************************************************+")#define Min(a, b) ( (a < b) ? a : b )#define Max(a, b) ( (a > b) ? a : b )#define lc         o<<1#define rc         o<<1|1#define lson       L, M, lc#define rson       M + 1, R, rc#define mem0(x)   memset(x, 0, sizeof x)#define mem1(x)   memset(x, -1, sizeof x)#define memf(x)   memset(x, false, sizeof x)#define pb        push_back#define pf        push_front#define LB        lower_bound#define UB        upper_bound#define PQ        priority_queue#define fr(x)     freopen("x", "r", stdin )#define fw(x)     freopen("x", "w" , stdout)#define all(a)    a.begin(), a,end()#define X         first#define Y         second#define MP        make_pair#define Abs(x)    ( x >= 0 ) ? x : ( -x )#define MAXS      50000 + 8#define MAXT      10000 + 8#define MAXL      500000 + 8#define INF       0x3f3f3f3f#define INFL      1000000000000000000#define inf       -(1<<30)#define EPS       1e-10#define PI        acos(-1.0)#define sqr(x)    (x * x)using namespace std;typedef long long          LL;typedef unsigned long long uLL;typedef double             DB;typedef long double        LD;typedef pair<int, int >   pii;typedef pair<LL, LL>       pll;const int MOD   = 1e9;const int N     = 1e5 + 8;const int maxn  = 1e3 + 8;const int dx[]  = { -1, 1,  0, 0 };const int dy[]  = {  0, 0, -1, 1 };struct Node{    LL x;    int id;    char ch;    bool operator < ( const Node & n) const    {        return x < n.x;    }} no[N];int n, q;char s[N];int pre[N], seg[N << 2];void pushUp(int o){    seg[o] = Min( seg[lc],  seg[rc] );}void build(int L, int R, int o){    if ( L == R ) {       seg[o] = pre[L]; return;    }    int M = (L  + R) >> 1;    build( lson );    build( rson );    pushUp(o);}int query(int l, int r, int L, int R, int o){    if (  l <= L && r >= R  ) return seg[o];    int  M = (L + R) >> 1;    int ans = INF;    if ( l <= M  ) ans = Min(ans, query( l, r, lson));    if ( r > M  ) ans = Min(ans, query( l, r, rson));    return ans;}int main(){    //freopen("codecoder.in", "r", stdin);    //freopen("out.txt", "w", stdout);     ios::sync_with_stdio(true);    while (~scanf("%d%d", &n, &q))    {        mem0(pre);        scanf("%s", s + 1);        int t = 0;        pre[0] = 0;        for (int i = 1; i <= n; i++) {            if ( s[i] == '(' ) pre[i] = pre[i-1] + 1;            else pre[i] = pre[i-1] - 1;        }        build( 1, n, 1);        int l ,r;        while (q--) {            scanf("%d%d", &l, &r);            if ( l > r) swap(l ,r);            if ( s[l] == s[r] || s[l] == ')'  ) puts("Yes");            else {               int mm = query( l, r-1, 1, n, 1 );               if ( mm >= 2 ) puts("Yes");               else puts("No");            }        }    }    return 0;}

RMQ代码: 136ms, 15.5M

/// He  renders  landscapes  with  great  skill  and  artistry.#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cstdlib>#include <cmath>#include <iterator>#include <cctype>#include <sstream>#include <string>#include <vector>#include <set>#include <map>#include <stack>#include <deque>#include <queue>#include <list>#include <functional>#include <ctime>#include <bitset>//#pragma comment(linker, "/STACK:102400000, 102400000")#define debug     puts("+******************************************************+")#define Min(a, b) ( (a < b) ? a : b )#define Max(a, b) ( (a > b) ? a : b )#define lc         o<<1#define rc         o<<1|1#define lson       L, M, lc#define rson       M + 1, R, rc#define mem0(x)   memset(x, 0, sizeof x)#define mem1(x)   memset(x, -1, sizeof x)#define memf(x)   memset(x, false, sizeof x)#define pb        push_back#define pf        push_front#define LB        lower_bound#define UB        upper_bound#define PQ        priority_queue#define fr(x)     freopen("x", "r", stdin )#define fw(x)     freopen("x", "w" , stdout)#define all(a)    a.begin(), a,end()#define X         first#define Y         second#define MP        make_pair#define Abs(x)    ( x >= 0 ) ? x : ( -x )#define MAXS      50000 + 8#define MAXT      10000 + 8#define MAXL      500000 + 8#define INF       0x3f3f3f3f#define INFL      1000000000000000000#define inf       -(1<<30)#define EPS       1e-10#define PI        acos(-1.0)#define sqr(x)    (x * x)using namespace std;typedef long long          LL;typedef unsigned long long uLL;typedef double             DB;typedef long double        LD;typedef pair<int, int >   pii;typedef pair<LL, LL>       pll;const int MOD   = 1e9;const int N     = 1e5 + 8;const int maxn  = 1e3 + 8;const int dx[]  = { -1, 1,  0, 0 };const int dy[]  = {  0, 0, -1, 1 };struct Node{    LL x;    int id;    char ch;    bool operator < ( const Node & n) const    {        return x < n.x;    }} no[N];int n, q;char s[N];int pre[N], d[N][30];//void pushUp(int o)//{//    seg[o] = Min( seg[lc],  seg[rc] );//}//void build(int L, int R, int o)//{//    if ( L == R ) {//       seg[o] = pre[L]; return;//    }//    int M = (L  + R) >> 1;//    build( lson );//    build( rson );//    pushUp(o);//}//int query(int l, int r, int L, int R, int o)//{//    if (  l <= L && r >= R  ) return seg[o];//    int  M = (L + R) >> 1;//    int ans = INF;//    if ( l <= M  ) ans = Min(ans, query( l, r, lson));//    if ( r > M  ) ans = Min(ans, query( l, r, rson));//    return ans;//}void RMQ_init(){    for (int i = 1;i <= n; i++)        d[i][0] = pre[i];    for (int j = 1;  (1 << j) <= n; j++) {          for ( int i = 1; i  + (1 << j) - 1 <= n; i++) {               d[i][j] = Min( d[i][j-1], d[i + (1 << (j -1) ) ][j - 1] );          }    }}int RMQ(int L, int R){    int k = 0;    while ( (1 <<(k+ 1) ) <= R - L +  1 ) k++;    return Min( d[L][k],  d[R -  (1 << k)  + 1 ][k] );}int main(){    //freopen("codecoder.in", "r", stdin);    //freopen("out.txt", "w", stdout);     //ios::sync_with_stdio(true);    while (~scanf("%d%d", &n, &q))    {        mem0(pre);        scanf("%s", s + 1);        int t = 0;        pre[0] = 0;        for (int i = 1; i <= n; i++) {            if ( s[i] == '(' ) pre[i] = pre[i-1] + 1;            else pre[i] = pre[i-1] - 1;        }        RMQ_init();        int l ,r;        while (q--) {            scanf("%d%d", &l, &r);            if ( l > r) swap(l ,r);            if ( s[l] == s[r] || s[l] == ')'  ) puts("Yes");            else {               int mm = RMQ(l, r-1);               if ( mm >= 2 ) puts("Yes");               else puts("No");            }        }    }    return 0;}


  

0 0