zoukankan      html  css  js  c++  java
  • 【贪心】CSU 1809 Parenthesis (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接:

      http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809

    题目大意:

      给一个长度为N(N<=105)的合法括号序列。Q(Q<=105)个询问,每次交换x,y位置上的括号后这个序列是否还合法,询问之间不影响。

    题目思路:

      【贪心】

      首先可以肯定,交换相同的括号是不会有影响的。把后面的'('和前面的')'交换也不会有影响。所以只用考虑把前面'('和后面的')'交换

      而这时要满足x,y之间的所有位置i不会存在,1~i个括号中左括号数少于右括号数。可以开个线段树维护,也可以用前缀和来做。

      d[i]表示前i个括号,num['(']与num[')']差小于等于1的前缀和。(因为交换'('和')'后num['(']-1,num[')']+1,每次交换差2).

      所以只要判断x,y中间是否存在num['(']-num[')']<1的情况。而这个可以用前缀和来统计,最后d[y]-d[x]即可知道。

     1 //
     2 //by coolxxx
     3 //#include<bits/stdc++.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<string>
     7 #include<iomanip>
     8 #include<map>
     9 #include<stack>
    10 #include<queue>
    11 #include<set>
    12 #include<bitset>
    13 #include<memory.h>
    14 #include<time.h>
    15 #include<stdio.h>
    16 #include<stdlib.h>
    17 #include<string.h>
    18 //#include<stdbool.h>
    19 #include<math.h>
    20 #define min(a,b) ((a)<(b)?(a):(b))
    21 #define max(a,b) ((a)>(b)?(a):(b))
    22 #define abs(a) ((a)>0?(a):(-(a)))
    23 #define lowbit(a) (a&(-a))
    24 #define sqr(a) ((a)*(a))
    25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
    26 #define mem(a,b) memset(a,b,sizeof(a))
    27 #define eps (1e-8)
    28 #define J 10000
    29 #define mod 1000000007
    30 #define MAX 0x7f7f7f7f
    31 #define PI 3.14159265358979323
    32 #define N 100004
    33 using namespace std;
    34 typedef long long LL;
    35 int cas,cass;
    36 int n,m,lll,ans;
    37 LL aans;
    38 int c[N],a[N],b[N],t[N],d[N];
    39 char s[N];
    40 int main()
    41 {
    42     #ifndef ONLINE_JUDGE
    43 //    freopen("1.txt","r",stdin);
    44 //    freopen("2.txt","w",stdout);
    45     #endif
    46     int i,j,k;
    47     int x,y;
    48 //    for(scanf("%d",&cass);cass;cass--)
    49 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
    50 //    while(~scanf("%s",s+1))
    51     while(~scanf("%d",&n))
    52     {
    53         mem(a,0);mem(b,0);mem(c,0);mem(t,0);
    54         scanf("%d",&m);
    55         scanf("%s",s);
    56         for(i=0,j=0;i<n;i++)
    57         {
    58             if(s[i]=='(')t[++j]=i;
    59             else
    60             {
    61                 c[i]=t[j];
    62                 c[t[j--]]=i;
    63             }
    64         }
    65         a[0]=1;b[0]=0;d[0]=1;
    66         for(i=1;i<n;i++)
    67         {
    68             a[i]=a[i-1]+(s[i]=='(');
    69             b[i]=b[i-1]+(s[i]==')');
    70             d[i]=d[i-1];
    71             if(a[i-1]-b[i-1]<=1)d[i]++;
    72         }
    73         for(i=1;i<=m;i++)
    74         {
    75             scanf("%d%d",&x,&y);
    76             x--,y--;
    77             if(x>y)swap(x,y);
    78             if(s[x]==s[y] || s[x]==')')
    79             {
    80                 puts("Yes");
    81                 continue;
    82             }
    83             if(d[y]-d[x]>0)puts("No");
    84             else puts("Yes");
    85         }
    86     }
    87     return 0;
    88 }
    89 /*
    90 //
    91 
    92 //
    93 */
    View Code
  • 相关阅读:
    Ubuntu 12.04 下 Sublime Text 3 Build 3047 破解
    如何让Ubuntu 12.04 LTS更炫更具吸引力
    重装Ubuntu时如何保留/home分区中的数据
    【转】使用TCP协议连续传输大量数据时,是否会丢包,应如何避免?
    TCP协议三次握手过程分析
    狮子的预言
    来跟从我
    服役的灵
    追求良善
    安慰人心的主
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/5842501.html
Copyright © 2011-2022 走看看