D. Least Cost Bracket Sequence

题目连接:

http://www.codeforces.com/contest/3/problem/D

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<bits/stdc++.h>
using namespace std; priority_queue<pair<long long,int> >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<s.size();i++)
{
if(s[i]=='(')
now++;
else if(s[i]==')')
now--;
else
{
int x,y;scanf("%d%d",&x,&y);
now--;s[i]=')';ans+=y;
Q.push(make_pair(y-x,i));
}
if(now<0)
{
if(Q.size()==0)break;
pair<int,int> a = Q.top();Q.pop();
ans-=a.first;s[a.second]='(';
now+=2;
}
}
if(now!=0)return puts("-1"),0;
cout<<ans<<endl;
cout<<s<<endl;
}

Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列的更多相关文章

  1. 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

    题目传送门 /* 题意:求最长括号匹配的长度和它的个数 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 详细解释:http://bl ...

  2. Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

    C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  3. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  4. Codeforces Beta Round #18 (Div. 2 Only)

    Codeforces Beta Round #18 (Div. 2 Only) http://codeforces.com/contest/18 A 暴力 #include<bits/stdc+ ...

  5. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  6. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  7. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  8. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  9. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

随机推荐

  1. .gitignore 文件添加或更新后规则无效的解决方案

    项目已经提交之后,突然想忽略某个文件或目录 A,于是在 .gitignore 里添加了忽略规则.但是提交(commit)之后,发现一旦修改了 A,git 同样会检测到 A 的变化(changes) , ...

  2. H5对安卓WeView开发中的影响

     1.body,或者html 高度为100% 会导致下拉直接触发原生的刷新控件,而不是webView滑动到顶部后刷新,以及不会执行onScrollChanged 方法,并且getScrollY 总是返 ...

  3. 【hihocoder】sam1-基本概念

    这题有毒…… 原本只是想复习下sam,于是写…… 后来发现自己傻了不知道怎么维护endpos…… 一气之下直接kmp拉倒,mdzz UPD:现在我好像会维护endpos了…… #include< ...

  4. HDU 6109 数据分割 并查集,SET

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6109 题意:中文题面 解法:每次都贪心地尝试将尽量多的条件放进当前这组,遇到第一个与已有条件冲突时,就 ...

  5. 2017多校第6场 HDU 6105 Gameia 博弈

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6105 题意:Alice和Bob玩一个游戏,喷漆!现在有一棵树上边的节点最开始都没有被染色.游戏规则是: ...

  6. C++ STL结构总结

    1. 什么是STL 它的全名是stand template library, 标准模板库,主要是将一些结构和算法写成模板,以便能够实现对任意类型的对象都可以操作,而不需要再一次去写一些算法及结构. 它 ...

  7. JAVA中的数据存储(堆及堆栈)

    转自:http://www.iteye.com/topic/6345301.寄存器:最快的存储区, 由编译器根据需求进行分配,我们在程序中无法控制.2. 栈:存放基本类型的变量数据和对象的引用,但对象 ...

  8. 把web项目部署到tomcat上

    首先在服务器搭建JDK环境:https://www.cnblogs.com/lb809663396/p/5855877.html 然后把tomcat文件包复制到服务器上,访问http://localh ...

  9. WP的SEO工具汇总

    Baidu Sitemap Generator  百度站点地图生成工具 https://wordpress.org/plugins/baidu-sitemap-generator/ This pulg ...

  10. Go语言建立一个最简单的服务端点

    handlers/handlers.go package handlers import ( "encoding/json" "net/http" ) func ...