题目:

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. 追溯了解Ubuntu之------基本命令操作(叁)

    在使用Ubuntu中的一些基本命令与Linux中是有区别的: 1.       查看Ubuntu系统位数:uname  -ar 或 getconf LONG_BIT 2.          获取Ubu ...

  2. Node.js 引用 gm 包错误 Error: Could not execute GraphicsMagick/ImageMagick

    今天在学习前后台图像剪切时,下载了有图片剪切瑞士军刀之称的 GraphicsMagick. 给 gm.exe 配置了环境变量,在 npm 下好了 gm 的模块,但是运行却出现了错误. 错误如图: [E ...

  3. ie的盒模型和标准模型

    使用 box-sizing:content-box || border-box || inherit 原理图 计算 怪异模型|IE模型 div宽度(定死) = 内容宽度+border宽度+paddin ...

  4. C++:bitset用法

    std::bitset是STL的一部分,准确地说,std::bitset是一个模板类,它的模板参数不是类型,而整形的数值(这一特性是ISO C++2003的新特性),有了它我们可以像使用数组一样使用位 ...

  5. Typescript函数

    编程都是需要函数的,因为有了函数就能复用很多东西了.不仅仅能够复用代码,还能保持代码的简洁性和提高编程的逻辑性. 在原生的JavaScript中,函数的定义有三种, function foo() {} ...

  6. 20155239《Java程序设计》实验二(面向对象程序设计)实验报告

    实验内容 初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5.了解设计模式 实验步骤 单元测试 1.三种代码: 伪代码 ...

  7. [WC2010][BZOJ1758]重建计划-[二分+分数规划+点分治]

    Description 传送门 Solution 看到那个式子,显然想到分数规划...(不然好难呢) 然后二分答案,则每条边的权值设为g(e)-ans.最后要让路径长度在[L,U]范围内的路径权值&g ...

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

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

  9. 1245 最小的N个和

    1245 最小的N个和 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond         题目描述 Description 有两个长度为 N 的序列 A 和 B, ...

  10. loj2538 「PKUWC 2018」Slay the Spire

    pkusc 快到了--做点题涨涨 rp. ref我好菜啊QAQ. 可以发现期望只是一个幌子.我们的目的是:对于所有随机的选择方法(一共 \(\binom{2n}{m}\)种),这些选择方法都最优地打出 ...