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轮呢?我 ...
随机推荐
- 将UBB编码转成标准的HTML代码
/// <summary> /// 将UBB编码转成标准的HTML代码 /// </summary> /// <param name="argString&qu ...
- 在AlexNet中LRN 局部响应归一化的理
在AlexNet中LRN 局部响应归一化的理 一.LRN技术介绍: Local Response Normalization(LRN)技术主要是深度学习训练时的一种提高准确度的技术方法.其中caffe ...
- IQueryable与IEnumerable使用区别
IQueryable会将查询语法转化为SQL查询语句,去数据库查询:IEnumerable则查询整张表,加载到内存中,再进行筛选. 所以,当查询的数据量较大的时候,则使用IQueryable.反之,数 ...
- rabbitmq类
1.accept.php消费者代码需要在命令行执行 2.'username'=>'asdf','password'=>'123456' 改成自己的帐号和密码 RabbitMQCommand ...
- mysql导入数据
1.准备sql文件:第一句话就是指定要操作的数据库,然后是insert语句,或者update语句或者delete语句 2.登录数据库,并执行: source sql文件位置 不要直接 ...
- odoo:Actions
actions定义了系统对于用户的操作的响应:登录.按钮.选择项目等. 一:窗口action(ir.actions.act_window ) 最常用的action类型,用于将model的数据展示出来. ...
- Ajax 导出Excel 方式
1.使用iframe 加载 使用get方式 <iframe id="comdownshow" height="0" width="0" ...
- 洛谷 P2886 [USACO07NOV]牛继电器Cow Relays
题面 解题思路 ## floyd+矩阵快速幂,跟GhostCai爷打赌用不用离散化,最后完败..GhostCai真是tql ! 有个巧妙的方法就是将节点重新编号,因为与节点无关. 代码 #includ ...
- 原生微信小程序的生命周期
小程序的生命周期函数:onLaunch:function(){当启动小程序时触发小程序只会启动1次,一般为初次打开时一般只会触发一次},onShow:function(){当小程序从后台切入到前台时触 ...
- DSMM之数据处理安全
一.背景 数据安全生命周期分为采集.传输.存储.处理.交换.销毁几个阶段,其中数据处理阶段是整个周期的核心阶段,数据处理安全与否直接关系到整体数据安全.那么今天分享内容就是数据处理安全的相关要求和实现 ...