题目链接: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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

  4. 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know

    题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...

  5. 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 ...

  6. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

    题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...

  7. Codeforces Round #288 (Div. 2)

    A. Pasha and Pixels     题意就是给一个n*m的矩阵,k次操作,一开始矩阵全白,一次操作可以染黑一个格子,问第几次操作可以使得矩阵中存在一个2*2的黑色矩阵.直接模拟即可 代码: ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. autofac 注入普通服务和WCF服务

    using Autofac;using Autofac.Builder;using Autofac.Core; //实现Autofac扩展 public static AutofacRegisterW ...

  2. spring boot properties

    [转载] 代码从开发到测试要经过各种环境,开发环境,测试环境,demo环境,线上环境,各种环境的配置都不一样,同时要方便各种角色如运维,接口测试, 功能测试,全链路测试的配置,hardcode 肯定不 ...

  3. aircrack-ng test

    Aircrack-ng工具包有很多工具,我用到的工具主要有以下几个: airmon-ng 处理网卡工作模式 airodump-ng 抓包 aircrack-ng 破解 aireplay-ng 发包,干 ...

  4. MySQL的外键是什么和它的作用

    从上图可以看见,表1添加一个外键,这个外键就是表2中的学号字段,那么这样表1就是主表,表2就是子表.所以结合2张表就能保持数据的一致性.完整性. 外键的一些事项:1.表1可以有一个或者多个外键,也可以 ...

  5. header的安全配置指南

    0x00 背景 在统计了Alexa top 100万网站的header安全分析之后(2012年11月 - 2013年3月 - 2013年11月),我们发现其实如何正确的设置一个header并不是一件容 ...

  6. IM即时通讯

    即时通讯,由于项目需求和不可抗力因素用的融云 当然我更倾向于用环信亲加等 使用融云遇到的那些坑: 1.集成时的坑: ,编译环境要求太高    项目中有很多旧的东西  达不到其标准 直接用最新版,出错, ...

  7. flask笔记---url、变量规则

    1.路由: route() 装饰器用于把一个函数绑定到一个 URL,可以动态变化 URL 的某些部分,还可以为一个函数指定多个规则,从而方便用户访问与记忆. 例子: @app.route('/') # ...

  8. oss cmd

    osscmd是基于python 2.5.4(其他版本没有试过),用来操作OSS的,可使用命令行来上传和下载文件. 下载地址:http://storage.aliyun.com/leo/osscmd.t ...

  9. js中,还真不了解 console

    参考链接: https://segmentfault.com/a/1190000000481884

  10. jquery插件写法

    //传统写法 //全局方法 ;(function($){ $.method = function(){ } //or $.obj = { method1:function(){}, method2:f ...