标签:
这个题,哈哈,很无语。#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]='\0'; puts(ans); }
#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('('); print(l+1,flag[l][r]); putchar(')'); print(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); else puts("IMPOSSIBLE"); }
Notice that the memory limit is non-standard.
Recently Arthur and Sasha have studied correct bracket sequences. Arthur understood this topic perfectly and become so amazed about correct bracket sequences, so he even got himself a favorite 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 him.
All Arthur remembers about his favorite sequence is for each opening parenthesis (‘(‘) the approximate distance to the corresponding closing one (‘)‘). For the i-th opening bracket he remembers the segment [li,?ri], containing the distance to the corresponding closing bracket.
Formally speaking, for the i-th opening bracket (in order from left to right) we know that the difference of its position and the position of the corresponding closing bracket belongs to the segment [li,?ri].
Help Arthur restore his favorite correct bracket sequence!
The first line contains integer n (1?≤?n?≤?600), the number of opening brackets in Arthur‘s favorite correct bracket sequence.
Next n lines contain numbers li and ri (1?≤?li?≤?ri?<?2n), representing the segment where lies the distance from the i-th opening bracket and the corresponding closing one.
The descriptions of the segments are given in the order in which the opening brackets occur in Arthur‘s favorite sequence if we list them from left to right.
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 are no sequences corresponding to the given information, print a single line "IMPOSSIBLE" (without the quotes).
4 1 1 1 1 1 1 1 1
()()()()
3 5 5 3 3 1 1
((()))
3 5 5 3 3 2 2
IMPOSSIBLE
3 2 3 1 4 1 4
(())()
版权声明:本文为博主原创文章,未经博主允许不得转载。
codeforces 508 E. Arthur and Brackets
标签:
原文地址:http://blog.csdn.net/stl112514/article/details/47173031