Rikka with Parenthesis II

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5831

Description

As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Correct parentheses sequences can be defined recursively as follows:

1.The empty string "" is a correct sequence.

2.If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.

3.If "X" is a correct sequence, then "(X)" is a correct sequence.

Each correct parentheses sequence can be derived using the above rules.

Examples of correct parentheses sequences include "", "()", "()()()", "(()())", and "(((())))".

Now Yuta has a parentheses sequence S, and he wants Rikka to choose two different position i,j and swap Si,Sj.

Rikka likes correct parentheses sequence. So she wants to know if she can change S to a correct parentheses sequence after this operation.

It is too difficult for Rikka. Can you help her?

Input

The first line contains a number t(1<=t<=1000), the number of the testcases. And there are no more then 10 testcases with n>100

For each testcase, the first line contains an integers n(1<=n<=100000), the length of S. And the second line contains a string of length S which only contains ‘(’ and ‘)’.

Output

For each testcase, print "Yes" or "No" in a line.

Sample Input

3

4

())(

4

()()

6

)))(((

Sample Output

Yes

Yes

No

Hint

题意

给你一个括号序列,你必须交换俩括号位置,问你可以可以使他合法。

题解:

哎呀,好气啊,感觉只有我们队是用线段树去模拟交换的……

最优情况下一定交换第一个右括号和最后一个左括号,交换后判断一下即可。 时间复杂度 O(n)O(n)

代码

#include <bits/stdc++.h>
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define two(x) (1<<(x))
#define clr(x,y) memset((x),(y),sizeof((x)))
#define dbg(x) cout << #x << "=" << x << endl;
const int mod = 1e9 + 7;
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std;
const int maxn = 100000 + 150;
int len,prefix[maxn],lstid;
char str[maxn];
struct Sgtree{
struct node{
int l , r , lzy , mi ; void Update( int v ){
lzy += v;
mi += v;
} }tree[maxn << 2]; void Build( int l , int r , int o ){
tree[o].l = l , tree[o].r = r , tree[o].lzy = tree[o].mi = 0;
if( r > l ){
int mid = l + r >> 1;
Build( l , mid , o << 1 );
Build( mid + 1 , r , o << 1 | 1 );
Maintain( o );
}else{
if( l == len ) lstid = o;
tree[o].mi = prefix[l];
}
} int query( int x , int o ){
int l = tree[o].l , r = tree[o].r;
if( l == r ) return tree[o].mi;
else{
int mid = l + r >> 1 , rs ;
ReleaseLabel(o);
if( x <= mid ) rs = query( x , o << 1 );
else rs = query( x , o << 1 | 1 );
Maintain(o);
return rs;
}
} void Maintain( int o ){
tree[o].mi = min( tree[o << 1].mi , tree[o << 1 | 1 ].mi );
} void ReleaseLabel( int o ){
if( tree[o].lzy ){
tree[o << 1].Update( tree[o].lzy );
tree[o << 1 | 1].Update( tree[o].lzy );
}
tree[o].lzy = 0;
} void Modify( int ql , int qr , int y , int o ){
int l = tree[o].l , r = tree[o].r;
if( ql <= l && r <= qr ) tree[o].Update( y );
else{
int mid = l + r >> 1;
ReleaseLabel( o );
if( ql <= mid ) Modify( ql , qr , y , o << 1 );
if( qr > mid ) Modify( ql , qr , y , o << 1 | 1 );
Maintain( o );
}
} int Search_First( int x , int o ){
int l = tree[o].l , r = tree[o].r;
if( l == r ){
if( tree[o].mi < 0 ) return l;
return -1;
}
int mid = l + r >> 1 , rs = -1;
ReleaseLabel( o );
if( tree[o << 1].mi < 0 ) rs = Search_First( x , o << 1 );
if( rs == -1 && x > mid && tree[o << 1 | 1].mi < 0 ) rs = Search_First( x , o << 1 | 1 );
Maintain( o );
return rs;
} }Sgtree; bool solve(){
rep(i,1,len) if(str[i]=='(' && i > 1){
int go = Sgtree.Search_First( i - 1 , 1 );
if( go != -1 ){
Sgtree.Modify( go , i - 1 , 2 , 1 );
if( Sgtree.tree[1].mi == 0 && Sgtree.query(len,1) == 0 ) return true;
Sgtree.Modify( go , i - 1 , -2 , 1 );
}
}
return false;
} int main(int argc,char *argv[]){
int T=read();
while(T--){
len=read();
sf("%s",str+1);
int ar = 0 , ok = 1;
rep(i,1,len){
prefix[i] = prefix[i - 1];
if(str[i]=='('){
++ prefix[i];
++ ar;
}
else{
if( ar == 0 ) ok = 0;
-- ar;
-- prefix[i];
}
}
Sgtree.Build(1,len,1);
if( ar != 0 ) ok = 0;
if( ok ){
if( len == 2 ) pf("No\n");
else pf("Yes\n");
}else{
bool result = solve();
if( result == true ) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}

hdu 5831 Rikka with Parenthesis II 线段树的更多相关文章

  1. HDU 5831 Rikka with Parenthesis II(六花与括号II)

    31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  2. HDU 5831 Rikka with Parenthesis II (栈+模拟)

    Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  3. HDU 5831 Rikka with Parenthesis II (贪心)

    Rikka with Parenthesis II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  4. hdu 5831 Rikka with Parenthesis II 括号匹配+交换

    Rikka with Parenthesis II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  5. HDU 5831 Rikka with Parenthesis II (贪心) -2016杭电多校联合第8场

    题目:传送门. 题意:T组数据,每组给定一个长度n,随后给定一个长度为n的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...

  6. HDU 5831 Rikka with Parenthesis II

    如果左括号数量和右括号数量不等,输出No 进行一次匹配,看匹配完之后栈中还有多少元素: 如果n=2,并且栈中无元素,说明是()的情况,输出No 如果n=2,并且栈中有元素,说明是)(的情况,输出Yes ...

  7. HDU 5831 Rikka with Parenthesis II ——(括号匹配问题)

    用一个temp变量,每次出现左括号,+1,右括号,-1:用ans来记录出现的最小的值,很显然最终temp不等于0或者ans比-2小都是不可以的.-2是可以的,因为:“))((”可以把最左边的和最右边的 ...

  8. HDU 5828 Rikka with Sequence (线段树)

    Rikka with Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...

  9. HDU 5828 Rikka with Sequence(线段树)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5828 [题目大意] 给出一个数列,要求支持区间加法,区间开方和区间和查询操作. [题解] 考虑开方 ...

随机推荐

  1. bzoj千题计划213:bzoj2660: [Beijing wc2012]最多的方案

    http://www.lydsy.com/JudgeOnline/problem.php?id=2660 很容易想到是先把n表示成最大的两个斐波那契数相加,然后再拆分这两个斐波那契数 把数表示成斐波那 ...

  2. JavaScript绝句的小研究

    前几日在网上看到一篇文章:JavaScript绝句,看了以后觉得里面的代码颇为有趣,不过文章里面只是简单的说了这样写的目的和结果,却没有令读者起到既知其然,又知其所以然的效果.这里简单写一篇小文章剖析 ...

  3. 【转】[.Net] 确定当前网站的物理文件路径

    确定当前网站的物理文件路径 在应用程序中,您可能需要确定服务器上的文件或其他资源的路径.例如,如果应用程序以编程方式对文本文件进行读写操作,则必须为用于读取和写入的方法提供该文件的完整物理路径. 将物 ...

  4. Python 装饰器入门(上)

    翻译前想说的话: 这是一篇介绍python装饰器的文章,对比之前看到的类似介绍装饰器的文章,个人认为无人可出其右,文章由浅到深,由函数介绍到装饰器的高级应用,每个介绍必有例子说明.文章太长,看完原文后 ...

  5. The Smallest Difference

    Given two array of integers(the first array is array A, the second array is arrayB), now we are goin ...

  6. git使用常用命令

    第一部分:个人整理部分(读<Git教程By廖雪峰.pdf>笔记) /* 配置全局参数 */git config --global user.name "username" ...

  7. DM816X 实现 USB HID Gadget 鼠标键盘功能【转】

    转自:https://my.oschina.net/renyongke/blog/410695 开发环境: 平台: DM8168 内核 :linux 2.6.32 RDK:DVRRDK_04.00.0 ...

  8. va_start(),va_end()函数应用【转】

    转自:http://www.cnblogs.com/gogly/articles/2416833.html 原理解释: VA_LIST 是在C语言中解决变参问题的一组宏,在<stdarg.h&g ...

  9. 使用Python自己实现简单的数据可视化

    只使用Python的random库,将已有数据生成HTML格式的标签云.思路就是根据同一单词出现的次数多少,生成不同大小不同颜色单词的数据的视图. 比如以下格式的多条数据: 1 Gaming 1 Sk ...

  10. java虚拟机规范(se8)——java虚拟机结构(三)

    2.6. 栈帧 栈帧用于存储数据和部分结果,同样也用于执行动态链接,返回方法的值和分派异常. 当方法被调用的时候会创建一个新的栈帧.当一个方法调用结束时,它对应的栈帧就被销毁了,不管是正常调用结束还是 ...