codeforces 508 E. Arthur and Brackets
来源:互联网 发布:邓亚萍 20亿 知乎 编辑:程序博客网 时间:2024/05/01 08:38
题意:给出所有左括号跟右括号之间合法的距离,求出一个合法的括号序列。
由于括号肯定是快点匹配完最好,所以维护一个栈,若当前的左括号可以被匹配那就匹配否则丢一个左括号进去。
#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);}
由于中了dp毒,强行dp做的话,就是这样,dp[l][r]:区间[l,r]的左括号是否可以丢在一起匹配。
dp[l][r]=dp[l][k]&&dp[k+1][r],相当于把[l,k]的左括号全部丢进由第l个左括号跟相对它距离为k的右括号之间进行合法判断,区间[k+1,r]类似,所以k收到了第l个左括号的限制,最后就不断dp好了。
#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);elseputs("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).
41 11 11 11 1
()()()()
35 53 31 1
((()))
35 53 32 2
IMPOSSIBLE
32 31 41 4
(())()
- Codeforces 508E Arthur and Brackets
- codeforces 508 E. Arthur and Brackets
- codeforces 508E Arthur and Brackets (stack)
- codeforces 508 E. Arthur and Brackets
- Codeforces 508E Arthur and Brackets【贪心】
- Arthur and Brackets - CodeForces 508 E dp贪心
- codeforces 508E E. Arthur and Brackets(栈模拟+贪心+构造)
- codeforces 508E E. Arthur and Brackets(栈模拟+贪心+构造)
- Codeforces Round #288 (Div. 2)E. Arthur and Brackets
- codeforces288 E. Arthur and Brackets
- CodeForces Round #288 Div.2. E-Arthur and Brackets——dp/greedy
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心 区间dp
- Arthur and Questions - CodeForces 518 E
- codeforces #308 E. Vanya and Brackets
- Codeforces 552E Vanya and Brackets
- Codeforces 552E Vanya and Brackets
- CodeForces-552E. Vanya and Brackets
- codeforces 552 E Vanya and Brackets
- hdoj 2187 悼念512汶川大地震遇难同胞——老人是真饿了
- 怎样才是理想的程序员
- 【Unity3D ugui】使用艺术字
- Android之Window、WindowManager(一):PopupWindow添加浮动窗口内部过程
- SpringMVC 基于注解的Controller @RequestMapping @RequestParam..
- codeforces 508 E. Arthur and Brackets
- 动态规划-最长递增序列(LIS)【模板】
- Mongodb 实现数据自动在后台删除--根据一个截止时间
- testNG+httpclient接口测试
- lampp 修改MySQL数据库存储路径
- android杂记7:drawable state各个属性详解
- 共同头部+ViewPager+ListView
- QML语法例子:单击MouseArea使Rectangle变换随机的颜色
- C#使用Dotfuscator混淆代码以及加密