题目链接: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. python04 面向对象编程02

    为啥要用类而不用函数呢 记住两个原则: 减少重复代码 代码会经常变更 2    会对变量或字符串的合法性检测(在实例初始化的时候能够统一初始化各个实例的变量,换做函数来说,要弄出同样的变量那么在初始化 ...

  2. 八数码问题(紫薯P199)

    #include <algorithm> #include <cstdio> #include <cstring> #include <iostream> ...

  3. 【原】CSS3 Media在常用设备的设置值

    摘要:今天的一个小小的项目中,在各种手机上样式都显示正常,唯独iphone4s的有些许问题.产品经理又说iphone4s用户还挺多的,无奈,只能查一查iphone4s的media值,顺便做个小小总结; ...

  4. wpf 线程

    一.线程概述:[引用MSDN] 通常,WPF 应用程序从两个线程开始:一个用于处理呈现,一个用于管理 UI.呈现线程有效地隐藏在后台运行,而 UI 线程则接收输入.处理事件.绘制屏幕以及运行应用程序代 ...

  5. UnityShader:HSV(色相,饱和度,亮度)转换

    http://blog.csdn.net/costfine/article/details/46930473 发现其实美术调整颜色的时候大部分都是调整的HSV,因为可以方便的分别调整色相(hue).饱 ...

  6. 关于win10输入法问题(打不出中文)解决方法

    提问过windous10打不出字,通过安装第三方输入法和所有有关切换的快捷键都没用,现在找到了解决方法 win键+x,打开命令提示符,输入ctfmon,回车 这样就能看到桌面右下角的输入法上面的叉叉消 ...

  7. [webgrid] – header - (How to Add custom html to Header in WebGrid)

    How to Add custom html to Header in WebGrid MyEvernote Link Posted on March 30, 2013by mtryambake Ho ...

  8. 开源面向对象数据库 db4o 之旅,第 1 部分: 初识 db4o

    前言 业界对持久存储领域的追求从未停止过,为了更方便.更容易地用对象表达我们的思维,开源领域和商业领域都涌现了许多新技术, ORM 的出现恰恰说明了这点.最近一年,业界也在反思,到底 ORM 给我们带 ...

  9. android自定义控件(6)-详解在onMeasure()方法中如何测量一个控件尺寸

    今天的任务就是详细研究一下protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法.如果只是说要重写什么方法有什么 ...

  10. svn三大目录trunk、branch和tag

    SVN中Branch和tag的比较在SVN中Branch和tag在一个功能选项中,在使用中也往往产生混淆.在实现上,branch和tag,对于svn都是使用copy实现的,所以他们在默认的权限上和一般 ...