Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列
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 优先队列的更多相关文章
- 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence
题目传送门 /* 题意:求最长括号匹配的长度和它的个数 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 详细解释:http://bl ...
- 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 ...
- 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]中有多少个数 ...
- Codeforces Beta Round #18 (Div. 2 Only)
Codeforces Beta Round #18 (Div. 2 Only) http://codeforces.com/contest/18 A 暴力 #include<bits/stdc+ ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
随机推荐
- python模块 zipfile
zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是很常见的zip格式,所以这个模块使用频率也是比较高的zipfile里有两个非常重要的class, 分别是ZipFile和Zip ...
- Call Mode feature
起源 user 在插著 充電器 打電話的狀況下, 為了安全起見, 避免 充電器在這時損害手機,間接造成 user 的傷害, 而有了這 feature, 在 battery voltage Vbat & ...
- 64_l3
libguac-client-ssh-0.9.13-3.20170521git6d2cfda...> 23-May-2017 09:58 64570 libguac-client-ssh-0.9 ...
- java之基本数据类型与引用数据类型
基本数据类型 需要注意的是字符是基本数据类型,但是字符串不是基本数据类型. 引用数据类型 类.接口类型.数组类型.枚举类型.注解类型. (上面说的字符串String属于引用数据类型中“类”的范畴) 两 ...
- C语言 ,两个字符串参数,判断是否包含另一个字符串,返回所在位置
char * cyp(char *s1,char *s2) { char *p = NULL; char *q = NULL; char *q1 = NULL; while(*s1!='\0') { ...
- 微信openid和UnionID (多公众号如何判断是否是同一人)
以下内容源于网络,因为非博客园信息,有想收藏,只能直接复制粘贴了,还希望原稿人员理解. 微信公众平台更新,为开发者提供UnionID机制 经开发者反馈,由于同一公司下多个公众号之间需要用户帐号互通,微 ...
- Android点击图标重新启动问题
原文:http://blog.csdn.net/jianiuqi/article/details/54091181 项目中的小问题:发现应用打包安装后按home键切换到后台后,点击应用图标又重新打开了 ...
- AC日记——「HNOI2017」礼物 LiBreOJ 2020
#2020. 「HNOI2017」礼物 思路: A题进程: 一眼出式子->各种超时过不去->看题解明白还有fft这个东西->百度文库学习fft->学习dft->学习fft ...
- centos7yum安装tomcat
执行安装命令 [root@localhost ~]# yum -y install tomcat 查询tomcat是否安装成功 [root@localhost ~]# rpm -q tomcat to ...
- Linux下使进程在后台运行
怎么样使程序在后台执行 /////////////////// nohup ./nn > nn.log 2 > &1 & //////////// 方法有很多, ...