题目:

time limit per test

2 seconds

memory limit per test

128 megabytes

input

standard input

output

standard output

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!

Input

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.

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 are no sequences corresponding to the given information, print a single line "IMPOSSIBLE" (without the quotes).

Examples
input
4
1 1
1 1
1 1
1 1
output
()()()()
input
3
5 5
3 3
1 1
output
((()))
input
3
5 5
3 3
2 2
output
IMPOSSIBLE
input
3
2 3
1 4
1 4
output
(())()

题意:

按从左到右的顺序给出n对括号的距离范围[l,r],问这些括号是否能组成合法的括号对。

样例二:

题解:

最右边的一对括号距离肯定要是1,因为它右边不会再有括号被包含了,

同理,从右往左扫,扫到第i对括号的时候,它的右边的所有括号的长度都是确定下来的,然后从i+1到n,考虑最小的能够被i合法包含的括号对,这里贪心找最小能够为左边提供更多方案,从只要有解,都不会漏掉。

时间复杂度总共O(n^2)。

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std; const int maxn = ; int n; struct Node {
int x, y;
bool operator < (const Node& tmp) const {
return x>tmp.x || (x == tmp.x) && y>tmp.y;
}
}nds[maxn]; char ans[maxn * ];
int used[maxn * ];
int fst, las; void init() {
memset(used, , sizeof(used));
} int main() {
// freopen("data_in.txt","r",stdin);
while (scanf("%d", &n) == && n) {
init();
for (int i = ; i<n; i++) {
scanf("%d%d", &nds[i].x, &nds[i].y);
}
int su = ;
for (int i = n - ; i >= ; i--) {
if (nds[i].x == ) {
nds[i].x = ;
}
else {
int sum = , flag = ;
for (int j = i + ; j<n;) {
sum += (nds[j].x + );
if (sum >= nds[i].x&&sum <= nds[i].y) {
nds[i].x = sum; flag = ; break;
}
j += (nds[j].x + ) / ;
}
if (flag == ) {
su = ; break;
}
}
}
if (!su) printf("IMPOSSIBLE\n");
else {
for (int i = ; i<n; i++) {
for (int j = ; j< * n; j++) {
if (used[j] == ) {
ans[j] = '('; used[j] = ;
ans[j + nds[i].x] = ')'; used[j + nds[i].x] = ;
break;
}
}
}
for (int i = ; i< * n; i++) printf("%c", ans[i]);
printf("\n");
}
}
return ;
}

CodeForces 508E Arthur and Brackets 贪心的更多相关文章

  1. Codeforces 508E Arthur and Brackets 区间dp

    Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...

  2. Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心

    E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...

  3. Codeforces Round #288 (Div. 2) E. Arthur and Brackets [dp 贪心]

    E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...

  4. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  5. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  6. Arthur and Brackets CodeForces - 508E (贪心,括号匹配)

    大意: n个括号, 位置未知, 给出每对括号左右间距, 求输出一个合法括号序列. 最后一个括号间距一定为1, 从右往左遍历, 每次括号划分越小越好. #include <iostream> ...

  7. 【codeforces 508E】Artur and Brackets

    [题目链接]:http://codeforces.com/problemset/problem/508/E [题意] 让你构造一个括号字符串; 使得每个从左往右数第i个左括号在这个括号序列中与之匹配的 ...

  8. Codeforces Round #288 (Div. 2) E. Arthur and Brackets

    题目链接:http://codeforces.com/contest/508/problem/E 输入一个n,表示有这么多对括号,然后有n行,每行输入一个区间,第i行的区间表示从前往后第i对括号的左括 ...

  9. C. Arthur and Table(Codeforces Round #311 (Div. 2) 贪心)

    C. Arthur and Table time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. Delphi Android USB Interface with the G2

    来源:http://www.bverhue.nl/g2dev/?p=65 Delphi Android USB Interface with the G2 Leave a reply I first ...

  2. ARM设备树

    学习目标:学习设备树相关内容: 一.概念 在Linux 2.6中,ARM架构的板极硬件细节过多地被硬编码在arch/arm/plat-xxx和arch/arm/mach-xxx,在kernel中存在大 ...

  3. Jquery 批量操作标签属性

     $("[id*='Custom']").removeAttr("disabled")

  4. intel-FPGA的片内存储器问题

    FPGA的片内有很多的存储器资源,可以配置成单端口的ROM.RAM和双端口的ROM.RAM,以及移位寄存器和FIFO等.在学习过程中,笔者遇到过几个小问题,总结如下: 片内是不是有ROM或者RAM? ...

  5. 20155233 2016-2017-2《Java程序设计》课程总结

    20155233 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:预习去写博客,如何达到理想的师生关系. 预备作业2:对自己C语言的学习进行了解与认识. ...

  6. 洛谷 P3369 【模板】普通平衡树(Treap/SBT)

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多 ...

  7. idea 严重: Error configuring application listener of class org.springframework.web.context.Context 后面省略

    根本原因:jar文件没有同步发布到自己项目的lib目录中 解决方案:把之前在这个位置的jar文件,put into 到 /WEB-INF/lib 目录下即可

  8. nginx 反向代理到目录

    upstream yg{ server 127.0.0.x; } server { listen 80; server_name www.hikaru.pw; location / { rewrite ...

  9. javaweb(十六)——JSP指令

    一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...

  10. 为CentOS系统配置防火墙设置

    在各种操作系统中,为了保护系统在网络中是相对安全的,我们通常都会给操作系统配置防火墙,通过配置防火墙来限定哪些流量可以进来,哪些流量可以出去,通过这样的一种方式,可以有效的管理系统的流量,从一定程度上 ...