码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 508 E. Arthur and Brackets

时间:2015-07-31 18:23:56      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

这个题,哈哈,很无语。


题意:给出所有左括号跟右括号之间合法的距离,求出一个合法的括号序列。


由于括号肯定是快点匹配完最好,所以维护一个栈,若当前的左括号可以被匹配那就匹配否则丢一个左括号进去。
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int a[600],b[600],l[600];
char ans[1210];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>a[i]>>b[i];
	n*=2;
	int m=n/2,sum=-1;
	stack<int>sk;
	for(int i=0;i<n;i++)
	{
		if(sk.size())
		{
			int t=sk.top();
			if(l[t]+a[t]<=i&&i<=l[t]+b[t])
			{
				ans[i]=')';
				sk.pop();
				continue;
			}
		}
		sum++;
		if(sum==m)
		{
			puts("IMPOSSIBLE");
			return 0;
		}
		ans[i]='(';
		l[sum]=i;
		sk.push(sum);
	}
	ans[n]='\0';
	puts(ans);
}



由于中了dp毒,强行dp做的话,就是这样,dp[l][r]:区间[l,r]的左括号是否可以丢在一起匹配。


dp[l][r]=dp[l][k]&&dp[k+1][r],相当于把[l,k]的左括号全部丢进由第l个左括号跟相对它距离为k的右括号之间进行合法判断,区间[k+1,r]类似,所以k收到了第l个左括号的限制,最后就不断dp好了。


#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int a[610],b[610];
int dp[610][610];
int flag[610][610];
int dfs(int l,int r)
{
	if(dp[l][r]!=-1)
		return dp[l][r];
	if(l>r)
		return dp[l][r]=1;
	for(int i=l;i<=r;i++)
		if(a[l]-1<=(i-l)*2&&(i-l)*2<=b[l]-1&&dfs(l+1,i)&&dfs(i+1,r))
		{
			flag[l][r]=i;
			return dp[l][r]=1;
		}
	return dp[l][r]=0;
}
void print(int l,int r)
{
	if(l>r)
		return;
	putchar('(');
	print(l+1,flag[l][r]);
	putchar(')');
	print(flag[l][r]+1,r);
}
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>a[i]>>b[i];
	memset(dp,-1,sizeof(dp));
	if(dfs(0,n-1))
		print(0,n-1);
	else
		puts("IMPOSSIBLE");
}



time limit per test
2 seconds
memory limit per test
128 megabytes
input
standard input
output
standard output

Notice that the memory limit is non-standard.

Recently Arthur and Sasha have studied correct bracket sequences. Arthur understood this topic perfectly and become so amazed about correct bracket sequences, so he even got himself a favorite correct bracket sequence of length 2n. Unlike Arthur, Sasha understood the topic very badly, and broke Arthur‘s favorite correct bracket sequence just to spite him.

All Arthur remembers about his favorite sequence is for each opening parenthesis (‘(‘) the approximate distance to the corresponding closing one (‘)‘). For the i-th opening bracket he remembers the segment [li,?ri], containing the distance to the corresponding closing bracket.

Formally speaking, for the i-th opening bracket (in order from left to right) we know that the difference of its position and the position of the corresponding closing bracket belongs to the segment [li,?ri].

Help Arthur restore his favorite correct bracket sequence!

Input

The first line contains integer n (1?≤?n?≤?600), the number of opening brackets in Arthur‘s favorite correct bracket sequence.

Next n lines contain numbers li and ri (1?≤?li?≤?ri?<?2n), representing the segment where lies the distance from the i-th opening bracket and the corresponding closing one.

The descriptions of the segments are given in the order in which the opening brackets occur in Arthur‘s favorite sequence if we list them from left to right.

Output

If it is possible to restore the correct bracket sequence by the given data, print any possible choice.

If Arthur got something wrong, and there are no sequences corresponding to the given information, print a single line "IMPOSSIBLE" (without the quotes).

Sample test(s)
input
4
1 1
1 1
1 1
1 1
output
()()()()
input
3
5 5
3 3
1 1
output
((()))
input
3
5 5
3 3
2 2
output
IMPOSSIBLE
input
3
2 3
1 4
1 4
output
(())()

版权声明:本文为博主原创文章,未经博主允许不得转载。

codeforces 508 E. Arthur and Brackets

标签:

原文地址:http://blog.csdn.net/stl112514/article/details/47173031

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!