Codeforces Round #288 (Div. 2) E. Arthur and Brackets
题目链接:http://codeforces.com/contest/508/problem/E
输入一个n,表示有这么多对括号,然后有n行,每行输入一个区间,第i行的区间表示从前往后第i对括号的左括号跟右括号之间的距离在这个区间的范围内,问是否存在这样的括号序列.
我的做法是比较繁琐,稍微看了下别人的代码,比我的短,应该有更简单 的做法.我的做法是从后往前构造括号序列,每次添加一对括号之前,先把当前的括号序列扫一遍,例如这个括号序列:(())()((())) ,很显然,现在我要新增一对括号上去的话,距离只能是:1,5,7,13,我扫一遍的目的就是扫出这些可能距离,然后判断这些可能的距离中的最小的而且满足在区间里面的距离,然后按照这个距离插入一对新的括号.为什么要最小的呢?因为虽然当前这些距离都是可行的,但是右括号插入的位置越靠后,产生的可行的距离的数目也就变少了,所以要靠前插入,给后面的插入制造更多的机会.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<deque>
using namespace std;
typedef struct node
{
int flag;
node *next;
}Linklist;
int n,inter[][],temp[];
int reader(Linklist* head,int* temp)
{
int flag = ,t = ,f = ;
Linklist *p = head->next;
while(p != NULL)
{
if(flag == )
{
temp[f++] = t;
t = ;
}
t++;
flag += p->flag;
p = p->next;
}
if(flag == )
temp[f++] = t;
return f;
}
void insert(Linklist* head,int tot)
{
int f = ;
Linklist *q = new Linklist;
q->flag = ;
q->next = head->next;
head->next = q;
Linklist *p = head;
while(tot--)
p = p->next;
Linklist *t = new Linklist;
t->flag = -;
t->next = p->next;
p->next = t;
}
void clean(Linklist *p)
{
if(p->next == NULL)
{
delete p;
return ;
}
clean(p->next);
}
void print(Linklist *head)
{
Linklist *p = head->next;
while(p!= NULL)
{
printf("%d ",p->flag);
p = p->next;
}
puts("");
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i = ;i < n;++i)
scanf("%d%d",&inter[i][],&inter[i][]);
int flag = ,t = ;
Linklist *head = new Linklist;
head->flag = ; //初始化头节点,头节点不存信息
head->next = NULL;
int ans = ;
for(int i = n - ;i >= ;--i)
{
int num = reader(head,temp); //返回的是temp中数值的个数
int ff = ,tot = ;
for(int j = ;j < num;++j)
{
tot += temp[j];
if(tot >= inter[i][] && tot <= inter[i][])
{
insert(head,tot); //执行在0位置插入'('跟在tot位置插入')'
ff = ; //已找到满足的条件,退出
break;
}
}
if(ff == )
{
ans = ;
break;
}
}
if(ans)
{
Linklist *p = head->next;
while(p)
{
printf(p->flag == ? "(":")");
p = p->next;
}
puts("");
}
else puts("IMPOSSIBLE");
clean(head); //回收内存
}
return ;
}
Codeforces Round #288 (Div. 2) E. Arthur and Brackets的更多相关文章
- 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 Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...
- Codeforces Round #288 (Div. 2)
A. Pasha and Pixels 题意就是给一个n*m的矩阵,k次操作,一开始矩阵全白,一次操作可以染黑一个格子,问第几次操作可以使得矩阵中存在一个2*2的黑色矩阵.直接模拟即可 代码: ...
- Codeforces Round #311 (Div. 2) C. Arthur and Table Multiset
C. Arthur and Table Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...
- Codeforces Round #297 (Div. 2) D. Arthur and Walls [ 思维 + bfs ]
传送门 D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input stan ...
随机推荐
- SaltStack与ZeroMQ(二)
SaltStack与ZeroMQ SaltStack底层是基于ZeroMQ进行高效的网络通信. ZeroMQ简介 ØMQ (也拼写作ZeroMQ,0MQ或ZMQ)是一个为可伸缩的分布式或并发应用程序设 ...
- Yocto开发笔记之《Tip-设置程序开机启动》(QQ交流群:519230208)
QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 IMX6UL,转载请注明出处 ============================================== ...
- WinForm------TextEdit控件去掉换行符
//将换行转为空格 string str = this.DetailEdit.Text.Replace("\r\n"," ");
- tomcat十大安全优化措施
1.telnet管理端口保护 使用telnet连接进来可以输入SHUTDOWN可以直接关闭tomcat,极不安全,必须关闭.可以修改默认的管理端口8005改为其他端口,修改SHUTDOWN指令为其他字 ...
- c++ http请求
平常我们要访问某个URL一般都是通过浏览器进行:提交一个URL请求后,浏览器将请求发向目标服务器或者代理服务器,目标服务器或者代理服务器返回我们所需要的数据,浏览器接收到这些数据后保存成文件并进行显示 ...
- easyui numberbox一些常用属性,方法
1.value="1234567.89"//数字框中的值 2.precision:2//精度(小数点后两位) 3.groupSeparator:','//(组分隔符) 4.deci ...
- Java线程操作方法
取得和设置线程名称 //================================================= // File Name : Thread_demo //--------- ...
- svn 强制用户添加注释 和 允许用户修改注释
当我们用TortoiseSVN提交代码时,有很多人不喜欢写注释,导致以后代码版本多,也不清楚哪个版本到底改了什么东西.所以在提交的时候,我会强制要求添加注释.这是如何实现的?这个话题就涉及到了svn的 ...
- Orchard源码分析(7):ASP.NET MVC相关
概述 Orchard归根结底是一个ASP.NET MVC(以后都简称为MVC)应用,但在前面的分析中,与MVC相关内容的涉及得很少.MVC提供了非常多的扩展点,本文主要关注Orchard所做的扩展.主 ...
- [Bug]IIs Cannot read configuration file due to insufficient permissions
摘要 在部署站点的时候,遇到这样的问题Cannot read configuration file due to insufficient permissions 解决办法 在服务器上部署站点,浏览的 ...