其他分享
首页 > 其他分享> > E. Bring Balance (括号匹配问题+贪心+前缀和)(CF 794 d2)

E. Bring Balance (括号匹配问题+贪心+前缀和)(CF 794 d2)

作者:互联网

E. Bring Balance
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Alina has a bracket sequence s of length 2n, consisting of n opening brackets '(' and n closing brackets ')'. As she likes balance, she wants to turn this bracket sequence into a balanced bracket sequence.

In one operation, she can reverse any substring of s.

What's the smallest number of operations that she needs to turn s into a balanced bracket sequence? It can be shown that it's always possible in at most n operations.

As a reminder, a sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters + and 1. For example, sequences (())(), (), and (()(())) are balanced, while )(, ((), and (()))( are not.

Input
The first line of the input contains a single integer t (1≤t≤2⋅104)  — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤105).

The second line of each test case contains a string s of length 2n, consisting of n opening and n closing brackets.

The sum of n over all test cases doesn't exceed 2⋅105.

Output
For each test case, in the first line output a single integer k (0≤k≤n)  — the smallest number of operations required.

The i-th of the next k lines should contain two integers li,ri (1≤li≤ri≤2n), indicating that in the i-th operation, Alina will reverse the substring slsl+1…sr−1sr. Here the numeration starts from 1.

If there are multiple sequences of operations with the smallest length which transform the sequence into a balanced one, you can output any of them.

Example
inputCopy
3
2
(())
5
())((()))(
6
())((()))(()
outputCopy
0
2
3 4
9 10
1
2 11
Note
In the first test case, the string is already balanced.

In the second test case, the string will be transformed as follows: ())((()))( → ()()(()))( → ()()(())(), where the last string is balanced.

In the third test case, the string will be transformed to ((()))((())), which is balanced.
View problem

思路:

#include <bits/stdc++.h>
using namespace std;
#define M 400005
#define ri register int 

template <class G> void read(G &x)
{
    x=0;int f=0;char ch=getchar();
    while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?-x:x;
    return ;
}


int t;
int n;
long long arr[M];
int main(){
    ios::sync_with_stdio(false);
  cin.tie(0);
    cin>>t;
    while(t--)
    {
        cin>>n;
        n*=2;
        string s;
        cin>>s;
        vector<int>p(n+1);
        int mx=1;
        for(ri i=0;i<s.length();i++)
        {
            if(s[i]=='(') p[i+1]=1;
            else p[i+1]=-1;
            arr[i+1]=arr[i]+p[i+1];
            if(arr[mx]<arr[i+1]) mx=i+1;
        }
        int flag=1;
        for(ri i=1;i<=n;i++)
        {
            if(arr[i]<0) 
            {
                flag=0;break;
            }
        }
        if(flag)
        {
            printf("0\n");
            continue;
        }
        int l=0,r=0;
        for(ri i=1;i<=n;i++)
        {
            if(arr[i]<0)
            {
                l=i; break; 
            }
        }
        for(ri i=n;i>=1;i--)
        {
            if(arr[i]<0)
            {
                r=i; break; 
            }
        }
        r++;
        int tmp=l;
        for(ri i=0;i<=tmp;i++)
        {
            if(arr[i]>arr[l]) l=i;
        }
        tmp=r;
        for(ri i=tmp+1;i<=n;i++)
        {
            if(arr[i]>arr[r]) r=i;
        }
        l++;
        reverse(s.begin()+l-1,s.begin()+r+1-1);
        flag=1;
        for(ri i=0;i<s.length();i++)
        {
            if(s[i]=='(') p[i+1]=1;
            else p[i+1]=-1;
            arr[i+1]=arr[i]+p[i+1];
            if(arr[i+1]<0) 
            {
                flag=0;break;
            }
        }
        if(flag)
        {
            printf("1\n");
            printf("%d %d\n",l,r);
            continue;
        }
        printf("2\n");
        printf("1 %d %d %d\n",mx,mx+1,n);
    }
    return 0;
    
} 
View Code

最后小声BB,他题目还要要求L,R尽量小嘛?????????

标签:794,前缀,sequence,CF,balanced,Bring,test,ri,string
来源: https://www.cnblogs.com/Lamboofhome/p/16347602.html