您的位置:首页 > 产品设计 > UI/UE

Codeforces 3D Least Cost Bracket Sequence [贪心]

2016-10-11 19:54 507 查看
D. Least Cost Bracket Sequence

time limit per test 1 second

memory limit per test 64 megabytes

inputs tandard input

output standard output

This is yet another problem on regular bracket sequences.

A bracket sequence is called regular, if by inserting “+” and “1” into it we get a correct mathematical expression. For example, sequences “(())()”, “()” and “(()(()))” are regular, while “)(“, “(()” and “(()))(” are not. You have a pattern of a bracket sequence that consists of characters “(“, “)” and “?”. You have to replace each character “?” with a bracket so, that you get a regular bracket sequence.

For each character “?” the cost of its replacement with “(” and “)” is given. Among all the possible variants your should choose the cheapest.

Input

The first line contains a non-empty pattern of even length, consisting of characters “(“, “)” and “?”. Its length doesn’t exceed 5·104. Then there follow m lines, where m is the number of characters “?” in the pattern. Each line contains two integer numbers ai and bi (1 ≤ ai,  bi ≤ 106), where ai is the cost of replacing the i-th character “?” with an opening bracket, and bi — with a closing one.

Output

Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second.

Print -1, if there is no answer. If the answer is not unique, print any of them.

Examples

input

(??)

1 2

2 8

output

4

()()

把所有 “?” 替换掉并匹配,使代价最小。

一开始想的是DP。。。。。

后来发现这样就有后效性了。。。。。

贪心。

假设当前所有问号都是 ‘)’ ,当右括号个数多于左括号个数时,需要从前面选择一个由 ‘?’ 变成的 ‘)’ 重新替换为 ‘(’ ,因为后面的都无法对左括号造成贡献。

那么如果某时候需要左括号的时候队列为空,或者最后括号数不匹配,无解。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#define AUTO "%I64d"
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
const int maxn = 50005;
char s[maxn];
struct Node
{
int l,r;
int val;
int id;
bool operator < (const Node t) const { return val < t.val; }
Node (const int _l,const int _r,const int _val,const int _id)
{ l=_l; r=_r; val=_val;id=_id; }
};
LL ans = 0;
priority_queue <Node> que;
int main()
{
#ifndef ONLINE_JUDGE
freopen("match.in","r",stdin);
freopen("match.out","w",stdout);
#endif
scanf("%s",s);
int lens = strlen(s);
int tot = 0;
for(int i=0;i<lens;i++)
{
if(s[i]=='(') tot++;
else if(s[i]==')') tot--;
else if(s[i]=='?')
{
int l,r;
scanf("%d%d",&l,&r);
que.push(Node(l,r,r-l,i));
ans += r;
s[i] = ')'; tot--;
}
if(tot<0)
{
if(que.empty()) { printf("-1"); exit(0); }
Node tmp = que.top(); que.pop();
ans -= tmp.val; s[tmp.id]='(';
tot+=2;
}
}
if(!tot) printf(AUTO "\n%s",ans,s);
else printf("-1");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 贪心