cf3D Least Cost Bracket Sequence
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.
Example
(??)
1 2
2 8
4
()()
给一个包含'(' ')' '?'的括号序列,其中'?'需要替换成'(' ')'使得它变成一个合法的括号序列,每个'?'都有一个变成‘(’ ')'的费用,要使得费用最小
不妨先把所有'?'用')'替换,然后再考虑把其中的一些')'改成'('。这样改回'('的费用就是ai-bi
如果'?'改成了')'导致前k个字符不是合法的括号序列了,就把前k个字符中找个ai-bi最小的改掉,这个维护个堆就好
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
priority_queue<pa,vector<pa>,greater<pa> >q;
char s[];
int pos[];
int l[],r[];
int n,p;
LL ans;
int main()
{
scanf("%s",s+);n=strlen(s+);
for (int i=;i<=n;i++)
{
if (s[i]=='(')p++;
else if (s[i]==')')
{
p--;
if (p<)
{
if (q.empty()){puts("-1");return ;}
p+=;
pa now=q.top();q.pop();
ans+=now.first;s[now.second]='(';
}
}else if (s[i]=='?')
{
int l=read(),r=read();
s[i]=')';p--;
ans+=r;q.push(mkp(l-r,i));
if (p<)
{
if (q.empty()){puts("-1");return ;}
p+=;
pa now=q.top();q.pop();
ans+=now.first;s[now.second]='(';
}
}
}
while (p<)
{
p+=;
pa now=q.top();q.pop();
ans+=now.first;s[now.second]='(';
}
p=;
for (int i=;i<=n;i++)
{
if (s[i]=='(')p++;else p--;
if (p<){puts("-1");return ;}
}
if (p!=){puts("-1");return ;}
printf("%lld\n",ans);
puts(s+);
}
cf 3D
cf3D Least Cost Bracket Sequence的更多相关文章
- CF3D Least Cost Bracket Sequence 贪心
Least Cost Bracket Sequence CodeForces - 3D 题目描述 This is yet another problem on regular bracket sequ ...
- 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(2500的实力贪心...
哎,昨天一直在赶课设..没有写 最近听了一些人的建议,停止高级算法的学习,开始刷cf. 目前打算就是白天懒得背电脑的话,系统刷一遍蓝书紫书白书之类的(一直没系统刷过),回宿舍再上机吧. https:/ ...
- 【贪心算法】CF3D Least Cost Bracket Sequence
题目大意 洛谷链接 给一个序列,序列里面会有左括号.问号.右括号.对于一个?而言,可以将其替换为一个(,也可以替换成一个),但是都有相应的代价.问:如何替换使得代价最小.前提是替换之后的序列中,括号是 ...
- Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列
D. Least Cost Bracket Sequence 题目连接: http://www.codeforces.com/contest/3/problem/D Description This ...
- Least Cost Bracket Sequence(贪心)
Least Cost Bracket Sequence(贪心) Describe This is yet another problem on regular bracket sequences. A ...
- 【贪心】【CF3D】 Least Cost Bracket Sequence
传送门 Description 给一个序列,序列里面会有左括号.问号.右括号.对于一个\(?\)而言,可以将其替换为一个\((\),也可以替换成一个\()\),但是都有相应的代价.问:如何替换使得代价 ...
- 3 D. Least Cost Bracket Sequence
题目大意: 这是一个规则的字符括号序列.一个括号序列是规则的,那么在序列里面插入‘+’ 和 ‘1’ 会得到一个正确的数学表达式. 合法:(())(), (),(()(())) 不合法:)(,((),( ...
- codeforces 3D . Least Cost Bracket Sequence 贪心
题目链接 给一个字符串, 由( ) 以及? 组成, 将?换成( 或者 ) 组成合法的括号序列, 每一个?换成( 或者 ) 的代价都不相同, 问你最小代价是多少, 如果不能满足输出-1. 弄一个变量nu ...
随机推荐
- 最常见的 5 个导致节点重新启动、驱逐或 CRS 意外重启的问题 (文档 ID 1524455.1)
适用于: Oracle Database - Enterprise Edition - 版本 10.1.0.2 到 11.2.0.3 [发行版 10.1 到 11.2]本文档所含信息适用于所有平台 用 ...
- javase(6)_异常
一.异常的概念 1.java异常是Java提供的用于处理程序中错误的一种机制. 2.所谓错误是程序在运行过程中发生的一些异常事件(如:除0,数组下标越界,文件不存在等). 3.Java程序的执行过程中 ...
- url地址数据参数转化JSON对象(js三种方法实现)
当我们用get方法提交表单时,在url上会显示出请求的参数组成的字符串,例如:http://localhost:3000/index.html?phone=12345678901&pwd=12 ...
- LeetCode 字符串的排列
给定两个字符串 s1 和 s2,写一个函数来判断 s2 是否包含 s1 的排列. 换句话说,第一个字符串的排列之一是第二个字符串的子串. 示例1: 输入: s1 = "ab" s2 ...
- POJ-3669-流星雨
这题的话,卡了有两个小时左右,首先更新地图的时候越界了,我们进行更新的时候,要判断一下是不是小于零了,越界就会Runtime Error. 然后bfs 的时候,我没有允许它搜出300以外的范围,然后就 ...
- [POJ]1164 The Castle
//markdown复制进来一堆问题 还是链接方便点 POJ 1164 The Castle 首先想到用9个方格来表示一个房间,如此一来复杂许多,MLE代码如下: //Writer:GhostCai ...
- RN原生方法setNativeProps
https://facebook.github.io/react-native/docs/direct-manipulation.html setNativeProps可以直接修改底层native组件 ...
- 格式化输出,基本运算符,流程控制主if
5.5自我总结 一.格式化输出 1.占位符 a = 1 b = 2 print('%S %s'%(a,b)) #1 2 print('%s %s'%(1,2)) #1 2 2.format格式化 a ...
- (转)TDD的iOS开发初步以及Kiwi使用入门
本文转自“瞄神”博客 TDD的iOS开发初步以及Kiwi使用入门 测试驱动开发(Test Driven Development,以下简称TDD)是保证代码质量的不二法则,也是先进程序开发的共识.App ...
- Java的9种基本数据类型以及封装类
Java的9种基本数据类型以及封装类 基本类型 大小(单位/字节) 默认值 封装类 byte 1 (byte)0 Byte short 2 (short)0 Short int 4 0 Integer ...