博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列
阅读量:6457 次
发布时间:2019-06-23

本文共 1848 字,大约阅读时间需要 6 分钟。

D. Least Cost Bracket Sequence

题目连接:

Description

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.

Sample Input

(??)

1 2
2 8

Sample Output

4

()()

Hint

题意

给你一个括号序列

有问号的地方给你把该问号变成左括号或者右括号的代价。

然后问你最少多少花费可以使得这个序列变得合法。

如果不能的话,输出-1.

题解:

直接暴力扫一遍。

先把全部问号变成右括号,如果当前这个位置的括号值小于0的话,就把y-x最大的那个位置变回左括号就好了。

用一个优先队列维护就好了。

代码

#include
using namespace std;priority_queue
>Q;string s;int main(){ cin>>s; if(s.size()%2==1)return puts("-1"),0; int now=0; long long ans=0; for(int i=0;i
a = Q.top();Q.pop(); ans-=a.first;s[a.second]='('; now+=2; } } if(now!=0)return puts("-1"),0; cout<
<

转载地址:http://uwizo.baihongyu.com/

你可能感兴趣的文章
c# winform 之DataGridViewComboBoxColumn的使用
查看>>
2014.5.20知识点学习:void与void*(转载)
查看>>
一步一步学习SignalR进行实时通信_2_Persistent Connections
查看>>
jQuery中常用的函数方法总结
查看>>
tabbarcontroller-1
查看>>
[Leetcode]376. Wiggle Subsequence
查看>>
去除系统部分属性触摸是出现的色值
查看>>
程序猿告诉你,没问题不用测,信了你就输了(首发公众号:子安之路)
查看>>
Drools源于规则引擎
查看>>
yield的作用
查看>>
NGUI之scroll view的制作和踩坑总结
查看>>
A*寻路算法的实现
查看>>
【转】解决深入学习PHP的瓶颈?
查看>>
ubuntu自带的ibus输入法问题解决方法
查看>>
20140705 testA 二分答案
查看>>
backbone 学习之sync
查看>>
css3 play
查看>>
react 项目全家桶构件流程
查看>>
IIS 7.5版本中一些诡异问题的解决方案
查看>>
java - JDBC
查看>>