#31. "UR #2" pig Man, the Second battle bracket sequence
Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
Http://uoj.ac/problem/31
Description
Hello everyone I am from Baidu Bar Bar _ called me pig-man, English name _callmeggbond.
I have never been to college, but this does not affect my interest in the fields of discrete mathematics, complexity analysis, and especially the theory of parentheses, which once immersed me. As for the OI algorithm contest, I did participate when I was young, although only a bronze medal, but I primality indifferent, do not care, after all, the so-called FFT, Cactus, and so on, just some tricks, not presentable; only the parentheses sequence will really inspire my enthusiasm for research.
I had naively thought that, with my knowledge and talents, I could settle down in this world, but until I was down on the street, I finally realized the cruelty of reality. Forced to make a living, I had to turn to the study of morality and philosophy; but I still have the same relationship with the parentheses, it's hard to cut.
Rational communication is always not smooth, the study of the road is tortuous, but easy to give up is not my style, in order to continue to achieve their ideals, I now present to you a series of parentheses the Super big problem.
There is one byNAn opening parenthesis "("andNA closing parenthesis ")"Composition of the sequence. Two numbers can be selected per operationl,R , and then put the firstLto the first< Span id= "mathjax-span-16" class= "Mrow" >r The order of the parentheses is reversed (the orientation of the brackets remains the same). For example, " () (() () (() () flips the 3 to 7 The result of parentheses is " () () () ((().
I want to use no more than n operations to turn this sequence into a valid parenthesis sequence.
It is well known that the legal bracket sequence is defined as follows:
- ()is the legal bracket sequence;
- If A it is a valid bracket sequence, it (A) is a valid bracket sequence;
- If A it B is a valid bracket sequence, it AB is a valid bracket sequence.
Ever since I came to uoj this treasure, my vision has widened, and I have seen more people who are rich in human wisdom. I am sure you will be able to give me a more satisfactory answer!
Input
One row of a non-empty string of length 2n represents the initial sequence. Ensure that the string contains only the opening and closing parentheses, and that the number of left and right brackets is n.
Output
For the given string, the output is adjusted into a valid bracket sequence scheme. If such a scenario does not exist, the output line is one integer −1.
Otherwise, the first line, an integer m , indicates that you want to do an m -flip.
NextmLine two integers per line l,R indicates the order of parentheses in the interval [l,R] to be flipped. The flip operation is performed in the order that you output it.
Please make surem≤n And 1≤l≤r≤2n, otherwise you will be sentenced to 0 points.
If you have more than one set of scenarios, you can output any group.
Sample Input
)))()(((
Sample Output
2
1 6
5 8
HINT
Test instructions
The following:
Greedy
There are many kinds of greedy methods, I say two kinds of
Just look at the code.
Code:
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<bitset>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 320051#defineMoD 10007#defineEPS 1e-9intNum;//const int INF=0X7FFFFFFF; //нчоч╢сConst intinf=0x3f3f3f3f; inline ll read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************inttot;intANSL[MAXN],ANSR[MAXN];CharS[MAXN];intsum =0;intFlag =-1;intMain () {scanf ("%s", s); intLen =strlen (s); for(intI=0; i<len;i++) {sum+ = S[i] = ='('?1: -1 ; if(sum==0&&flag!=-1) {Ansl[tot]=Flag; Ansr[tot++]=i+1; Flag=-1; } if(sum<0&&flag==-1) Flag= i+1; } printf ("%d\n", tot); for(intI=0; i<tot;i++) printf ("%d%d\n", Ansl[i],ansr[i]);}
#include <cstring>#include<cstdio>using namespacestd;Chars[500005];intMain () {scanf ("%s", s+1);inttot=0; intLen = strlen (s+1); printf ("%d\n", len/2); for(intI=1; i<=len;++i)if(s[i]=='(') printf ("%d%d\n",++tot,i); return 0;}
Uoj #31. "UR #2" pig man again battle bracket sequence greedy