This question, haha, very silent.
Test instructions: Give the legal distance between the opening parenthesis and the closing parenthesis, and find a valid parenthesis sequence.
Since the parentheses are definitely the best for fast matching, maintain a stack, if the current opening parenthesis can be matched then match or throw an opening parenthesis in.
#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;int a[600],b[600],l[600];char ans[1210];int main () {int n;cin>>n;for (int i=0;i<n;i++) cin>>a[i]>>b[i];n*=2;int m=n/2,sum=-1;stack<int>sk;for (int i=0;i<n;i++) { if (Sk.size ()) {int t=sk.top (); if (L[t]+a[t]<=i&&i<=l[t]+b[t]) {ans[i]= ') '; Sk.pop (); continue;}} Sum++;if (sum==m) {puts ("impossible"); return 0;} Ans[i]= ' ('; L[sum]=i;sk.push (sum);} Ans[n]= ';p uts (ANS);
As a result of DP poison, force DP do, that is, Dp[l][r]: the interval [l,r] The opening parenthesis can be dropped together to match.
Dp[l][r]=dp[l][k]&&dp[k+1][r], equivalent to the [l,k] of the left parenthesis are all thrown into the opening parenthesis and relative to its distance to K of the right parenthesis between the legal judgment, the interval [k+1,r] similar, so K received the restriction of the opening parenthesis of the first , and finally keep DP good.
#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;int a[610],b[610];int dp[610][610];int flag[610][610];int dfs (int l,int r) { if (dp[l][r]!=-1) return dp[l][r];if (L>r) return dp[l][r]=1;for (int i=l;i<=r;i++) if (a[l]-1<= (i-l) *2& & (I-l) *2<=b[l]-1&&dfs (l+1,i) &&dfs (i+1,r)) {Flag[l][r]=i;return dp[l][r]=1;} return dp[l][r]=0;} void print (int l,int r) {if (l>r) Return;putchar (' (');p rint (L+1,flag[l][r]);p Utchar (') ');p rint (flag[l][r]+1,r);} int main () {int n;cin>>n;for (int i=0;i<n;i++) Cin>>a[i]>>b[i];memset (Dp,-1,sizeof (DP)), if (Dfs ( 0,n-1) print (0,n-1); Elseputs ("Impossible");}
Time limit per test2 secondsmemory limit per test128 megabytesinputstandard inputoutputstandard output
Notice the memory limit is non-standard.
Recently Arthur and Sasha have studied correct bracket sequences. Arthur understood this topic perfectly and become so amazed on correct bracket sequences, so he even got himself a favo Rite correct bracket sequence of length 2n. Unlike Arthur, Sasha understood the topic very badly, and broke Arthur ' s favorite correct bracket sequence just to spite H Im.
All Arthur remembers on his favorite sequence are for each opening parenthesis ('(') The approximate distance to the corresponding closing one (')‘). For theI-th opening Bracket He remembers the segment[l,? RI], containing the distance to the corresponding closing bracket.
For Mally speaking, for The i -th opening bracket (in order from left to right) we know, the difference of its Position and the position of the corresponding closing bracket belongs to the Segment [ l i ,? R i ] .
Help Arthur restore his favorite correct bracket sequence!
Input
The first line contains integer n (1?≤? N? ≤?600), the number of opening brackets in Arthur ' s favorite correct bracket sequence.
NextNLines contain numbers li and Ri (1?≤? L i? ≤? R i? <?2N ), representing the segment where lies the distance from theI-th opening bracket and the corresponding closing one.
The descriptions of the segments is given in the order in which the opening brackets occur in Arthur ' s favorite sequence If we list them from left to right.
Output
If It is possible-to-restore the correct bracket sequence by the given data, print any possible choice.
If Arthur got something wrong, and there is no sequences corresponding to the given information, print a " impossible"(without the quotes).
Sample Test (s) input
41 11 11) 11 1
Output
()()()()
Input
35 53 31 1
Output
((()))
Input
35 53 32 2
Output
Impossible
Input
32 31 41 4
Output
(())()
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 508 E. Arthur and Brackets