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 ...
随机推荐
- 2017-2018-1 20179205《Linux内核原理与设计》第七周作业
<Linux内核原理与设计>第七周作业 视频学习及操作分析 创建一个新进程在内核中的执行过程 fork.vfork和clone三个系统调用都可以创建一个新进程,而且都是通过调用do_for ...
- python中的argparse模块
argparse干什么用的? 答:参数设置,比如python demo.py -h 诸如此类的. 开始学习这个模块: parser = argparse.ArgumentParser() #使用这个模 ...
- C语言将字符串转换成对应的数字(十进制、十六进制)【转】
转自:http://wawlian.iteye.com/blog/1315133 问题1:讲一个十进制数字的字符串表示转换成对应的整数.举例:将“”转换成整数1234. C代码 收藏代码 /*将字符串 ...
- VPS L2TP配置
原文地址:https://raymii.org/s/tutorials/IPSEC_L2TP_vpn_with_Ubuntu_14.04.html 只要保证ipsec verify没错,基本都可以成功 ...
- centos7下opencv的安装
os:centos7 opencv:opencv3.0.0 for linux reference:http://www.cnblogs.com/xixixing/p/6096057.html det ...
- 怎么删除Windows服务
1,首先找到服务名字. 2,在cmd中进到c:下面 3,sc delete 名字. 删除成功
- leetcode 之Reverse Nodes in k-Group(22)
这题有点繁琐,在更新指针时很容易出错. ListNode *reverseKGroup(ListNode *head, int k) { )return head; ListNode dummy(-) ...
- 小程序css
样式导入 @import /** common.wxss **/ .small-p { padding:5px; } /** app.wxss **/ @import "common.wxs ...
- LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver
1. Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ...
- debian 7 安装 rz sz lrzsz
https://blog.csdn.net/hchaoyangming/article/details/51582749