Codeforces 3D
D. Least Cost Bracket Sequencetime limit per test1 second
memory limit per test64 megabytes
inputstandard input
outputstandard output
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.
InputThe 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.
OutputPrint 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 test(s)input(??)
1 2
2 8output4
()()
Accepted Code:
from heapq import * s = list(raw_input());
ans, heap = 0, [];
b = 0
for i, v in enumerate(s):
if v == '(':
b += 1
elif v == ')':
b -= 1
else :
b -= 1;
x, y = map(int, raw_input().split());
ans += y;
s[i] = ')'
heappush(heap, (x-y, i));
if b < 0:
if (not heap):
print -1
quit()
b += 2;
x, y = heappop(heap);
ans += x;
s[y] = '(';
if b:
print '-1';
else :
print ans
print ''.join(s)
Codeforces 3D的更多相关文章
- 【Codeforces 3D】Least Cost Bracket Sequence
Codeforces 3 D 题意:有一个括号序列,其中一些位置是问号,把第\(i\)个问号改成(需要\(a_i\)的代价,把它改成)需要\(b_i\)的代价. 问使得这个括号序列成立所需要的最小代价 ...
- codeforces 3D . Least Cost Bracket Sequence 贪心
题目链接 给一个字符串, 由( ) 以及? 组成, 将?换成( 或者 ) 组成合法的括号序列, 每一个?换成( 或者 ) 的代价都不相同, 问你最小代价是多少, 如果不能满足输出-1. 弄一个变量nu ...
- codeforces 3D (非原创)
D. Least Cost Bracket Sequence time limit per test 1 second memory limit per test 64 megabytes input ...
- XTU1266:Parentheses(贪心+优先队列)
传送门 题意 从左到右有n个连续的组,每一组有Li个括号,要么全是左括号,要么全是右括号,以及该组的每一个左括号翻成右括号, 或者右括号翻成左括号的花费Di.可以对这n个组的括号进行翻转,每一个括号都 ...
- CF3D Least Cost Bracket Sequence 贪心
Least Cost Bracket Sequence CodeForces - 3D 题目描述 This is yet another problem on regular bracket sequ ...
- CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)
E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces 681C. Heap Operations 优先队列
C. Heap Operations time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表
E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...
- Codeforces - 346A - Alice and Bob - 简单数论
http://codeforces.com/problemset/problem/346/A 观察了一下,猜测和他们的最大公因数有关,除以最大公因数前后结果是不会变的. 那么怎么证明一定是有n轮呢?我 ...
随机推荐
- Spring MVC(八)--控制器接受简单列表参数
有些场景下需要向后台传递一个数组,比如批量删除传多个ID的情况,可以使用数组传递,数组中的ID元素为简单类型,即基本类型. 现在我的测试场景是:要从数据库中查询minId<id<maxId ...
- 6大主流开源SQL引擎总结,遥遥领先的是谁?
根据 O’Reilly 2016年数据科学薪资调查显示,SQL 是数据科学领域使用最广泛的语言.大部分项目都需要一些SQL 操作,甚至有一些只需要SQL.本文就带你来了解这些主流的开源SQL引擎!背景 ...
- Django logging 的配置方法
做开发离不开日志,以下是我在工作中写Django项目常用的logging配置. BASE_LOG_DIR = os.path.join(BASE_DIR, "log") LOGGI ...
- MyBatis注解开发-@Insert和@InsertProvider(转)
@Insert和@InsertProvider都是用来在实体类的Mapper类里注解保存方法的SQL语句.不同的是,@Insert是直接配置SQL语句,而@InsertProvider则是通过SQL工 ...
- C# 读写 Photoshop PSD文件 操作类
分析了PSD的文件....才发现PSD的RGB色彩保存也是 先红 蓝 绿 这样保存的 ....麻烦的.. 另外8BIM好象没什么用..可以直接跳过..直接获取最后的图形信息就可以了.. 我只对一些PS ...
- Ubuntu中使用Nginx+rtmp搭建流媒体直播服务
一.背景 本篇文章是继上一篇文章<Ubuntu中使用Nginx+rtmp模块搭建流媒体视频点播服务>文章而写,在上一篇文章中我们搭建了一个点播服务器,在此基础上我们再搭建一个直播服务器, ...
- charles-过滤网络请求方法
方法一:在主界面的中部的 Filter 栏中填入需要过滤出来的关键字.例如我们的服务器的地址是:https://www.baidu.com , 那么只需要在 Filter 栏中填入 https://w ...
- 封装了一个HOOKAPI的类。。。
我惊奇地发现,我手竟然这么生了..... 很久很久没有写这方面的东西,现在写着竟然这么手生....哎.... 这玩艺,还得天天练....
- 从0开始学习ssh之basedao
用于所有dao里边会有许多相同的方法,例如save,update等等.应此设计一个basedao,所有dao都继承它.这样可以省去许多工作量. basedao如下 package cn.itcast. ...
- 验证occ和vtk整合工作的demo
在编译occ通过过后,我需要验证occ是否能够正常结合vtk进行开发工作 使用CMake进行环境变量设置: CMakeList.txt PROJECT (IGESReader) #VTK Part: ...