Codeforces Round #288 (Div. 2) E. Arthur and Brackets
2015-03-05 13:31
274 查看
题目链接:http://codeforces.com/contest/508/problem/E
输入一个n,表示有这么多对括号,然后有n行,每行输入一个区间,第i行的区间表示从前往后第i对括号的左括号跟右括号之间的距离在这个区间的范围内,问是否存在这样的括号序列.
我的做法是比较繁琐,稍微看了下别人的代码,比我的短,应该有更简单 的做法.我的做法是从后往前构造括号序列,每次添加一对括号之前,先把当前的括号序列扫一遍,例如这个括号序列:(())()((())) ,很显然,现在我要新增一对括号上去的话,距离只能是:1,5,7,13,我扫一遍的目的就是扫出这些可能距离,然后判断这些可能的距离中的最小的而且满足在区间里面的距离,然后按照这个距离插入一对新的括号.为什么要最小的呢?因为虽然当前这些距离都是可行的,但是右括号插入的位置越靠后,产生的可行的距离的数目也就变少了,所以要靠前插入,给后面的插入制造更多的机会.
View Code
输入一个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[610][2],temp[2000]; int reader(Linklist* head,int* temp) { int flag = 0,t = 0,f = 0; Linklist *p = head->next; while(p != NULL) { if(flag == 0) { temp[f++] = t; t = 0; } t++; flag += p->flag; p = p->next; } if(flag == 0) temp[f++] = t; return f; } void insert(Linklist* head,int tot) { int f = 0; Linklist *q = new Linklist; q->flag = 1; q->next = head->next; head->next = q; Linklist *p = head; while(tot--) p = p->next; Linklist *t = new Linklist; t->flag = -1; 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 = 0;i < n;++i) scanf("%d%d",&inter[i][0],&inter[i][1]); int flag = 0,t = 0; Linklist *head = new Linklist; head->flag = 0; //初始化头节点,头节点不存信息 head->next = NULL; int ans = 1; for(int i = n - 1;i >= 0;--i) { int num = reader(head,temp); //返回的是temp中数值的个数 int ff = 0,tot = 1; for(int j = 0;j < num;++j) { tot += temp[j]; if(tot >= inter[i][0] && tot <= inter[i][1]) { insert(head,tot); //执行在0位置插入'('跟在tot位置插入')' ff = 1; //已找到满足的条件,退出 break; } } if(ff == 0) { ans = 0; break; } } if(ans) { Linklist *p = head->next; while(p) { printf(p->flag == 1? "(":")"); p = p->next; } puts(""); } else puts("IMPOSSIBLE"); clean(head); //回收内存 } return 0; }
View Code
相关文章推荐
- Codeforces Round #288 (Div. 2)E. Arthur and Brackets
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets [dp 贪心]
- Codeforces Round #288 (Div. 2)E. Arthur and Brackets
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心 区间dp
- Codeforces Round #343 (Div. 2) Problem C——Famil Door and Brackets
- Codeforces Round #343 (Div. 2) C. Famil Door and Brackets(DP)
- CodeForces #308 Div2 E(552E Vanya and Brackets)
- BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
- Codeforces Round #293 (Div. 2) E. Arthur and Questions
- Codeforces Round #293 (Div. 2) -- E. Arthur and Questions (思路 + 贪心)
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 搜索bfs
- codeforces 508 E. Arthur and Brackets
- codeforces288 E. Arthur and Brackets
- Codeforces Round #343 (Div. 2) (C. Famil Door and Brackets(DP))
- Codeforces Round #223 (Div. 1) C. Sereja and Brackets
- codeforces Round #223 (Div.1) C. Sereja and Brackets【线段树】
- 【线段树】 Codeforces Round #223 (Div. 1) C - Sereja and Brackets
- Codeforces Round #297 (Div. 2) D. Arthur and Walls
- Codeforces Round #297 (Div. 2) D - Arthur and Walls