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 ...
随机推荐
- Python中级 —— 04网络编程
网络编程 网络编程对所有开发语言都是一样的,Python也不例外.用Python进行网络编程,就是在Python程序本身这个进程内,连接别的服务器进程的通信端口进行通信. TCP编程 TCP建立可靠连 ...
- php图片上传存储源码,可实现预览
<?php header("content-Type: text/html; charset=gb2312"); $uptypes=array('image/jpg', // ...
- STM32(6)——USART串口的使用
1. 串口的基本概念 在STM32的参考手册中,串口被描述成通用同步异步收发器(USART),它提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间进行全双工数据交换.USART利用 ...
- Windos10 mysql-8.0.13安装手顺
一.下载 1.1 官方下载地址:https://dev.mysql.com/downloads/mysql/ ,点击Download 1.2 点击 No thanks,just start my do ...
- Leetcode---栈系列刷题(python3实现)----#496 下一个更大元素I
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...
- ie的盒模型和标准模型
使用 box-sizing:content-box || border-box || inherit 原理图 计算 怪异模型|IE模型 div宽度(定死) = 内容宽度+border宽度+paddin ...
- Oracle入门第六天(下)——高级子查询
一.概述 主要内容: 二.子查询介绍 1.简单子查询(WHERE子查询) SELECT last_name FROM employees WHERE salary > (SELECT salar ...
- 20155217 实验二 Java面向对象程序设计 实验报告
20155217 实验二 Java面向对象程序设计 实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模 ...
- 20155236 2016-2017-2 《Java程序设计》第十周学习总结
20155236 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 程序员所作的事情就是把数据发 ...
- [BZOJ2738]矩阵乘法-[整体二分+树状数组]
Description 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. (N<=500,Q<=60000) Solution 考虑二分答案,问题转化为求矩阵内为1 ...