Codeforces 508E Arthur and Brackets【贪心】
来源:互联网 发布:淘宝图片软件 编辑:程序博客网 时间:2024/05/22 08:01
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
(())()
题目大意:
需要构造出来一个长度为2*n的括号匹配串,使得从左到右,每个左括号的匹配右括号在区间【Li,Ri】之间。
思路:
用一个栈模拟一下贪心就行。
我们先使得后边的左括号匹配上,再匹配前边的就行了。
Ac代码:
#include<stdio.h>#include<string.h>#include<stack>using namespace std;int l[1500000];int r[1500000];int pos[1500000];char ans[1500000];int main(){ stack<int>s; int n; while(~scanf("%d",&n)) { int flag=0; int cnt=0; stack<int>s; for(int i=1;i<=n;i++) { scanf("%d%d",&l[i],&r[i]); s.push(i); pos[i]=cnt; ans[cnt++]='('; while(!s.empty()) { int u=s.top(); if(pos[u]+l[u]>cnt)break; if(pos[u]+r[u]<cnt) { flag=1; break; } ans[cnt++]=')'; s.pop(); } } if(s.size()==0&&flag==0)printf("%s\n",ans); else printf("IMPOSSIBLE\n"); }}
- Codeforces 508E Arthur and Brackets【贪心】
- Arthur and Brackets - CodeForces 508 E dp贪心
- 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 E. Arthur and Brackets(栈模拟+贪心+构造)
- codeforces 508E E. Arthur and Brackets(栈模拟+贪心+构造)
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心 区间dp
- 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
- CF 518E(Arthur and Questions-贪心)
- Arthur and Questions - CodeForces 518 E
- Codeforces Round #293 (Div. 2) -- E. Arthur and Questions (思路 + 贪心)
- Codeforces 518E Arthur and Questions【贪心+模拟】很考量代码能力的一个题
- codeforces #308 E. Vanya and Brackets
- Codeforces 552E Vanya and Brackets
- WM_SYSCOMAND 消息
- lmux c 一站式学习 函数指针与回调函数
- 知名网站的 404 页面长啥样?你的404长啥样?
- JavaScript有用的代码片段和trick
- PO类注解配置和PO类映射文件配置关系
- Codeforces 508E Arthur and Brackets【贪心】
- forEach和map和for方法的区别
- 3.spring boot自定义配置
- eclipse springcloud服务发现服务注册(Eureka) 学习日志
- lua常用
- Stirling数
- 回调函数和函数指针
- 机器学习十大算法
- 您想找一份与DevOps有关的工作吗