Least Cost Bracket Sequence(贪心)
Least Cost Bracket Sequence(贪心)
Describe
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 a i and b i (1 ≤ a i, b i ≤ 106), where a i is the cost of replacing the i-th character "?" with an opening bracket, and b i — 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
()()
Solution
因为左右括号匹配,我们从第一个字符遍历,定义一个sum=0,‘(’ 就 +1 ,‘)’ 就-1,‘?’ 就做相应的处理(下面再说),我们要保证遍历过程中一直让sum>=0,当然可以使用‘?’,如果遍历到一个地方,即使使用'?'sum仍<0,输出-1.
为了让()完美匹配,他给的’(‘ 和 ‘)’ 就不能改了,所以我们要处理'?',从左向右遍历,如果遇到一个‘?’,将’?‘变为‘)’仍满足sum>=0,那么我们就让‘?’变为‘)’(因为我们一直让左括号不少的嘛),如果?变为)之后,sum<0了,我们就在前面(包括这一个)找一个贡献大的变为)的?,使之变为(。
最后判定sum,sum==0正确输出,sum!=0则只可能是’(‘比‘)’多,输出-1。(‘)’比‘(’多的在上面已经判过了)。
Code
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
using namespace std;
const int maxn=5e4+5;
typedef long long ll;
char s[maxn];
int sum;
ll ans;
priority_queue<pair<int,int> > q;
int main(){
scanf("%s",s);
int len=strlen(s),x,y;
for(int i=0;i<len;++i){
if(s[i]=='(')sum++;
else if(s[i]==')'){
sum--;
if(sum<0){
if(q.empty()){printf("-1\n");return 0;}
else{
int xx=q.top().first,yy=q.top().second;q.pop();
ans-=(ll)xx;
s[yy]='(';
sum+=2;
}
}
}
else if(s[i]=='?'){
scanf("%d%d",&x,&y);
q.push(make_pair(y-x,i));//后面要改就先改y-x值大的,这样贡献才最大
s[i]=')';
ans+=(ll)y;
sum--;
if(sum<0){
if(q.empty()){printf("-1\n");return 0;}
else{
int xx=q.top().first,yy=q.top().second;q.pop();
ans-=(ll)xx;
s[yy]='(';
sum+=2;
}
}
}
}
if(sum!=0)printf("-1\n");
else printf("%lld\n%s",ans,s);
return 0;
}
hzoi
Least Cost Bracket Sequence(贪心)的更多相关文章
- CF3D Least Cost Bracket Sequence 贪心
Least Cost Bracket Sequence CodeForces - 3D 题目描述 This is yet another problem on regular bracket sequ ...
- codeforces 3D . Least Cost Bracket Sequence 贪心
题目链接 给一个字符串, 由( ) 以及? 组成, 将?换成( 或者 ) 组成合法的括号序列, 每一个?换成( 或者 ) 的代价都不相同, 问你最小代价是多少, 如果不能满足输出-1. 弄一个变量nu ...
- Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列
D. Least Cost Bracket Sequence 题目连接: http://www.codeforces.com/contest/3/problem/D Description This ...
- CF3D Least Cost Bracket Sequence 题解
题目 This is yet another problem on regular bracket sequences. A bracket sequence is called regular, i ...
- cf3D Least Cost Bracket Sequence
This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if b ...
- 【贪心】【CF3D】 Least Cost Bracket Sequence
传送门 Description 给一个序列,序列里面会有左括号.问号.右括号.对于一个\(?\)而言,可以将其替换为一个\((\),也可以替换成一个\()\),但是都有相应的代价.问:如何替换使得代价 ...
- CF3D Least Cost Bracket Sequence(2500的实力贪心...
哎,昨天一直在赶课设..没有写 最近听了一些人的建议,停止高级算法的学习,开始刷cf. 目前打算就是白天懒得背电脑的话,系统刷一遍蓝书紫书白书之类的(一直没系统刷过),回宿舍再上机吧. https:/ ...
- 【贪心算法】CF3D Least Cost Bracket Sequence
题目大意 洛谷链接 给一个序列,序列里面会有左括号.问号.右括号.对于一个?而言,可以将其替换为一个(,也可以替换成一个),但是都有相应的代价.问:如何替换使得代价最小.前提是替换之后的序列中,括号是 ...
- CodeForces 3 D.Least Cost Bracket Sequence【贪心+优先队列】
Description 给出一个括号序列,中间有一些问号,将第i个问号换成左括号代价是a[i],换成右括号代价是b[i],问如果用最少的代价将这个括号序列变成一个合法的括号序列 Input 第一行一个 ...
随机推荐
- Redis学习与应用-位图
什么是位图 位图bitmap是通过一个bit来表示某个元素对应的值或者状态,是由一组bit位组成,每个bit位对应0和1两个状态,虽然内部还是采用string类型进行存储,但是redis提供了直接操作 ...
- PHP中级篇 Apache配置httpd-vhosts虚拟主机总结及注意事项
经常使用Apache虚拟主机进行开发和测试,但每次需要配置虚拟主机时都习惯性的ctrl+c和ctrl+v,这次由于重装系统,需要配置一个新的PHP开发环境虚拟主机,于是总结一下Apaceh配置http ...
- 基于centos7搭建kvm
其他的和安装一般的系统没有差别 安装完成后. 1]使用ping www.baidu.com 2]修改静态ip,也可以不修改 3]下载brctlyum -y install bridge-utils 4 ...
- .net 使用TCP模拟UDP广播通信加强广播通信的稳定性
应用场景:当每一台终端开启程序后发出消息,其他终端必须收到消息然后处理 思路1:使用UDP广播. 缺点:UDP广播信号不稳定,无法确定每一台机器能接收到信号 思路2:将一台主机作为服务器,使用 ...
- flutter在2019年会有怎样的表现?
2019独角兽企业重金招聘Python工程师标准>>> Flutter的趋势 在移动端,受成本和效率的驱使,跨平台一站式开发慢慢成为一个趋势.从Hybird,RN,WEEX,Flut ...
- solr7.4创建core,导入MySQL数据,中文分词
#solr版本:7.4.0 一.新建Core 进入安装目录下得server/solr/,创建一个文件夹,如:new_core 拷贝server/solr/configsets/_default/con ...
- Linux监听磁盘使用情况
前阵子服务器磁盘写满了,导致项目出了很多奇怪的问题,比如文件上传不了(这个很好理解),还有登录时验证码无法加载(现在依旧不知道原因,项目的验证码图片是只在内存中生成的BufferedImage对象,不 ...
- 数学--数论--hdu 6216 A Cubic number and A Cubic Number (公式推导)
A cubic number is the result of using a whole number in a multiplication three times. For example, 3 ...
- USACO Training Section 1.1黑色星期五Friday the Thirteenth
题目描述 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至1900+N- ...
- 无向图双连通分量BCC(全网最好理解)
不是标题党,之前我也写过一篇比较全的,但是对于初学者不友好.传送门? 双连通分量(Biconnected component): 1.边双联通 E-BCC 2.点双连通 V-BCC 双 ...