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 ...
随机推荐
- redis理解
1. Redis是什么 redis是nosql的一种. 这个问题的结果影响了我们怎么用Redis.如果你认为Redis是一个key value store, 那可能会用它来代替MySQL:如果认为它是 ...
- 9月20日上午JavaScript函数
函数 一. 函数定义 函数又叫方法,在程序里面函数是用来执行某些特定功能的代码.为了减少重复使用代码,可以把特定功能的代码做成函数,需要使用时拿出来调用.alert();就是一个很常见的.简单的函数 ...
- HTML5学习预览
HTML5中,新增了很多input元素的类型 email email类型用于应该包含 e-mail 地址的输入域. url url 类型用于应该包含 URL 地址的输入 ...
- PHP 出现 502 解决方案
原文:http://www.ahlinux.com/php/10319.html nginx+php 出现502 bad gateway,一般这都不是nginx的问题,而是由于 fastcgi或者ph ...
- assert()函数用法总结
assert()函数用法总结 assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h> ...
- hdu 4006 The kth great number(优先队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 题目大意: 第一行 输入 n k,后有 n 行,对于每一行有两种状态 ,①“I x” : 插入 ...
- C# Pointer types
https://msdn.microsoft.com/en-us/library/y31yhkeb.aspx
- JS中两个重要的方法 call & apply 学习
正题: Function.prototype.apply(instance,args) //args 数组 Function.prototype.Call(instance,a1,a2) //a1 ...
- Database Initialization Strategies in Code-First:
You already created a database after running your Code-First application the first time, but what ab ...
- Ajax– 刷新页面 【转】
jquery刷新页面(局部及全页面刷新) 2009-12-31 11:39:32| 分类: javascript|举报|字号 订阅 局部刷新: 这个方法就多了去了,常见的有以下几种: $.get方法 ...