题目:

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. Some cool FireMonkey multi-device components

    http://blogs.embarcadero.com/davidi/2014/01/16/43281 There are many available Delphi and C++Builder ...

  2. MySQL数据库实验:任务一 创建数据库和表

    目录 任务一 创建数据库和表 [实训目的与要求] [实训原理] [实训步骤] 一.熟悉MySQL环境 二.利用MySQL命令行窗口创建数据库及表 三.利用界面工具创建数据库及表 任务一 创建数据库和表 ...

  3. ESP32 学习笔记 - Ubuntu安装

    最近买了一个ESP32的开发板,以前只玩过ESP8266,没经验只能自己一步步摸索 现在把学习的过程记录下来,以免过了一段时间就忘记了 第一步安装 VMware 可以在我的网盘下载 链接:https: ...

  4. 4.26-python学习笔记(变量及命名、字符串、格式化字符串print,format,%...)

    参考书目:<Learn Python The Hard Way> cars=100 print('there are ',cars,'cars available.') ##练习5 更多变 ...

  5. 安装虚拟机与初触linux心得

    安装虚拟机与初触linux心得 安装虚拟机 不知道是电脑问题还是软件问题,我安装虚拟机的过程异常坎坷,首先我在官网,360,太平洋等地方下载的virtualbox5.0以后的软件普遍有问题,问题是打不 ...

  6. Linux 用C语言实现简单的shell(1)

    发一波福利,操作系统的实验内容,大家可以借鉴一下,不过我的代码可能也存在一定的问题. 因为在一开始老师是一节一节课教的,当时并不知道后面还会用输入输出重定向,管道等一系列问题,我的兴趣也不在这个方面也 ...

  7. # 20155337 2016-2017-2 《Java程序设计》第十周学习总结

    20155337 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 •网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就 ...

  8. P2P平台投宝金融跑路?为什么我没有中雷!

    编者按:市场有风险,投资需谨慎.最近,安徽合肥P2P平台"投保金融"跑路倒闭了. 今天(2016年11月8日)下午,在朋友圈看到一个分享,投宝金融跑路了,新闻媒体已经传开了. 这个 ...

  9. 【转载】TCP/IP 之 大明王朝邮差

    原文:TCP/IP 之 大明王朝邮差 原创 2016-05-12 刘欣 码农翻身 前言: 本文主要想说一下TCP的知识, 比喻有不恰当之处,敬请包涵. 大明王朝天启四年, 清晨. 天色刚蒙蒙亮,我就赶 ...

  10. 解决非controller使用,@Autowired或者@Resource注解注入Mapper接口为null的问题

    知识点:在service层中注入其它的service接口或者mapper接口都是可以的 但是在封装的Utils工具类中或者非controller普通类中使用@Autowired@Resource注解注 ...