题目链接: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. POJ1185炮兵阵地(状态压缩 + dp)

    题目链接 题意:给出一张n * m的地图,其中 有的地方能放大炮,有的地方不能,大炮与上下左右两个单位范围内会相互攻击,问最多能放几个大炮 能放大炮为1不能放大炮为0,把每一行看做一个状态,要除去同一 ...

  2. RAL 标准颜色表(RAL Color Chart)

    根据经典RAL系统,本网页显示RAL标准颜色的概览.RAL用于信息,为图画和涂层定义标准颜色.现在,它是最流行的中欧颜色标准.这些颜色广泛用于建筑学,建筑物,工业和道路安全. 图表中的RAL颜色尽可能 ...

  3. Linux系统信息查看命令大全

    系统# uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue # 查看操作系统版本# cat /proc/cpuinfo # 查看CPU信息# hostna ...

  4. Runner站立会议07

    开会时间:21.10~21.30 地点:基教负一 今天做了什么:看网上下载的日历代码 明天准备做什么:继续看代码 遇到的困难:下载的代码有很多看不懂的地方,很多包.函数等都不知道 会议图: 燃尽图:

  5. 跟我一起玩转Sencha Touch 移动 WebApp 开发(一)

    1.目录 移动框架简介,为什么选择Sencha Touch? 环境搭建 创建项目框架,框架文件简介 创建简单Tabpanel案例 自定义图标的方式 WebApp产品测试和发布 HTML5离线缓存 发布 ...

  6. Robot Framework--01 创建简单工程示例

    1.新建Project: 填写name,选择Type为Dirctory,路径根据自己需要选择,建议最好不要在中文路径下,以免发生问题:

  7. C#根据时间产生有序的GUID编码

    public static Guid GenerateGuid() { byte[] guidArray = Guid.NewGuid().ToByteArray(); , , ); DateTime ...

  8. [Unity] 常用技巧收集

    Unity 屏幕旋转 void Update () { //处理横向两个方向旋转 if(Input.deviceOrientation == DeviceOrientation.LandscapeLe ...

  9. Linux消息队列应用

    #include"sys/types.h" #include "sys/msg.h" #include "unistd.h" #includ ...

  10. NSOperation GCD 对比 (附NSOperation演练)

    项目中使用NSOperation的优点是NSOperation是对线程的高度抽象,在项目中使用它,会使项目的程序结构更好子类化NSOperation的设计思路,是具有面向对象的优点(复用.封装),使得 ...