CodeForces 508E Arthur and Brackets 贪心
题目:
2 seconds
128 megabytes
standard input
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!
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).
4
1 1
1 1
1 1
1 1
()()()()
3
5 5
3 3
1 1
((()))
3
5 5
3 3
2 2
IMPOSSIBLE
3
2 3
1 4
1 4
(())()
题意:
按从左到右的顺序给出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 贪心的更多相关文章
- Codeforces 508E Arthur and Brackets 区间dp
Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...
- 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 ...
- 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 ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- Arthur and Brackets CodeForces - 508E (贪心,括号匹配)
大意: n个括号, 位置未知, 给出每对括号左右间距, 求输出一个合法括号序列. 最后一个括号间距一定为1, 从右往左遍历, 每次括号划分越小越好. #include <iostream> ...
- 【codeforces 508E】Artur and Brackets
[题目链接]:http://codeforces.com/problemset/problem/508/E [题意] 让你构造一个括号字符串; 使得每个从左往右数第i个左括号在这个括号序列中与之匹配的 ...
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets
题目链接:http://codeforces.com/contest/508/problem/E 输入一个n,表示有这么多对括号,然后有n行,每行输入一个区间,第i行的区间表示从前往后第i对括号的左括 ...
- 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 ...
随机推荐
- 修复 Cydia 不能上网的问题
使用 h3lix 越狱 10.3.3 的 iPhone5,进入 Cydia 不能联网解决方法:打开 Cydia,进入已安装列表,点击 Cydia Installer 卸载,然后看到桌面上就没有 Cyd ...
- 2.4 自己编写一个vivi驱动程序
学习目标:从零编写一个vivi驱动程序,并测试: 一. vivi驱动应用程序调用过程 上节对xawtv对vivi程序调用欧城进行了详细分析,可总结为以下流程: 二.仿照vivi.c编写myvivi.c ...
- Django模型定义参考
字段 对字段名称的限制 字段名不能是Python的保留字,否则会导致语法错误 字段名不能有多个连续下划线,否则影响ORM查询操作 Django模型字段类 字段类 说明 AutoField 自增ID字段 ...
- x01.polls: 学习 django
开发一个 Web 应用:x01.polls,可能比想像的还要容易一些,这完全得益于 django 框架. 1.安装 django: sudo pip3 install django 2.阅读 djan ...
- dns欺骗之ettercap
ettercap是一个基于ARP地址欺骗方式的网络嗅探工具,主要适用于局域网. ettercap是一款现有流行的网络抓包软件,它利用计算机在局域网内进行通信的ARP协议的缺陷进行攻击,在目标与服务器之 ...
- NoSQL入门第一天——NoSQL入门与基本概述
一.课程大纲 二.入门概述 1.为什么用NoSQL 单机MySQL的年代: 一个网站的访问量一般都不大,用单个数据库完全可以轻松应付. 我们来看看数据存储的瓶颈是什么? 1.数据量的总大小 一个机器放 ...
- 20155218 2016-2017-2《Java程序设计》课程总结
20155218 2016-2017-2<Java程序设计>课程总结 目录 每周作业链接汇总 实验报告链接汇总 博客中的经验与收获 代码托管 课堂项目实践 课程收获与不足 给开学初的你和学 ...
- 20155239《Java程序设计》实验一(Java开发环境的熟悉)实验报告
实验内容及步骤 使用JDK编译.运行简单的java程序 2.使用IDEA编辑.编译.运行.调试Java程序 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发 先建立一个文件夹命名为Co ...
- 2016-2017-20155329 《Java程序设计》第十周学习总结
学号 2016-2017-20155329 <Java程序设计>第十周学习总结 教材学习内容总结 学习目标 了解计算机网络基础 OSI分层(7层):物理层.数据链路层.网络层.传输层.会话 ...
- 20145226夏艺华 网络对抗技术 EXP7 网络欺诈技术防范
20145226夏艺华 网络对抗技术 EXP7 网络欺诈技术防范 实践内容 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法. · 简单应用SET工具建立冒名网站 · ett ...