D. Least Cost Bracket Sequence
time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard 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.

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.

Examples
input
  1. (??)
    1 2
    2 8
output
  1. 4
    ()()

这题的思路我没想出来,又是看了题解才会的。。

此题思路:http://blog.csdn.net/baidu_29410909/article/details/50967218

ac代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <cmath>
  6. #include <string>
  7. #include <set>
  8. #include <queue>
  9. using namespace std;
  10. typedef long long ll;
  11. string s;
  12. int main() {
  13. ios::sync_with_stdio(false);
  14. cin.tie(0);cout.tie(0);
  15. priority_queue<pair<int,int> >q;
  16. cin>>s;
  17. ll cnt=0,ans=0,m=0,x,y;
  18. for(int i=0;i<s.length();++i) {
  19. if(s[i]=='(') {
  20. ++cnt;
  21. }
  22. else if(s[i]==')') {
  23. --cnt;
  24. }
  25. else {
  26. s[i]=')';
  27. cin>>x>>y;
  28. ans+=y;
  29. q.push(make_pair(y-x,i));
  30. --cnt;
  31. m++;
  32. }
  33. pair<int,int> t;
  34. if(cnt<0) {
  35. if(q.empty()) break;
  36. t=q.top();
  37. q.pop();
  38. ans-=t.first;
  39. s[t.second]='(';
  40. cnt+=2;
  41. }
  42. }
  43. if(cnt!=0) cout<<-1<<endl;
  44. else {
  45. cout<<ans<<endl;
  46. cout<<s<<endl;
  47. }
  48. return 0;
  49. }

  

codeforces 3D (非原创)的更多相关文章

  1. codeforces 6E (非原创)

    E. Exposition time limit per test 1.5 seconds memory limit per test 64 megabytes input standard inpu ...

  2. 【非原创】LightOJ - 1284 Lights inside 3D Grid【概率期望】

    学习博客: 戳这里 戳这里 戳这里 戳这里 题意: 在一个三维的空间,每个点都有一盏灯,开始全是关的, 现在每次随机选两个点,把两个点之间的全部点,开关都按一遍:问k次过后开着的灯的期望数量: 题解: ...

  3. Linux下high CPU分析心得【非原创】

    非原创,搬运至此以作笔记, 原地址:http://www.cnitblog.com/houcy/archive/2012/11/28/86801.html 1.用top命令查看哪个进程占用CPU高ga ...

  4. CSS样式命名整理(非原创)

    非原创,具体出自哪里忘了,如果侵害您的利益,请联系我. CSS样式命名整理 页面结构 容器: container/wrap 整体宽度:wrapper 页头:header 内容:content 页面主体 ...

  5. 非原创。使用ajax加载控件

    非原创.来自博客园老赵. public class ViewManager<T> where T : System.Web.UI.UserControl { private System. ...

  6. Java 表达式解析(非原创)

    因项目需要,在网上找来一套表达式解析方法,由于原来的方法太过于零散,不利于移植,现在整理在同一文件内: 文件中包含5个内部类,源码如下: import java.util.ArrayList; imp ...

  7. Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创)

    Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创) 由于java interface中声明的字段在编译时会自动加上static final的修饰符,即声明为常量.因而inter ...

  8. 用RD,GR,BL三个方法内代码生成一张图片(非原创,我只是完整了代码)

    我公开以下图片的源代码,,是ppm格式的,,自己找到能打开的工具.. (非原创,我加工的代码,可直接执行运行输出,缩略图能看到效果)  这是原博客 http://news.cnblogs.com/n/ ...

  9. tp5.1 phpspreadsheet- 工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西,)

    phpspreadsheet-工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西)1. composer require phpoffice/phpspreadsheet2. 看最下面的两 ...

  10. Vue 仿QQ左滑删除功能(非原创)

    非原创,摘选来源:http://www.jb51.net/article/136221.htm. 废话不多说,相当实用,先记录. Html代码: <div class="contain ...

随机推荐

  1. MongoDB数据库,一些的筛选过滤查询操作和db.updae()更新数据库记录遇到的坑。

    缘由:使用MongoDB时遇到一些需要查询/更新操作指定某些字段的业务场景 查询和更新指定字段就需要进行简单的筛选和过滤,也能在大数据量时减少查询消耗时间 1. 查询数据库某些指定字段,同时默认返回_ ...

  2. 1.5V转5V的最少电路的芯片电路图

    PW5100满足1.5V转5V的很简洁芯片电路,同时达到了最少的元件即可组成DC-DC电路1.5V转5V的升压转换器系统. PW5100在1.5V转5V输出无负载时,输入效率电流极低,典型值10uA. ...

  3. Py层次递进与文件修改大程序,模块,name与file

    层次的递进与返回 #输入quit的时候返回上一阶层,输入exit退出所有的循环 tag=True while tag==True: level1=input('level1:') if level1= ...

  4. 手动添加Ini4idea,解决pycharm无法打开ini文件

    1. 查看本地pycharm的版本号 help -> about 2. 进入官网:http://plugins.jetbrains.com,选中pycharm及相关版本,搜索ini,切到版本,下 ...

  5. Netty编解码器(理论部分)

    背景知识 在了解Netty编解码之前,先回顾一下JAVA的编解码: 编码(Encode):在java中称之为序列化,把内存中易丢失的数据结构或对象状态转换成另一种可存储(存储到磁盘),可在网络间传输的 ...

  6. Frame of Reference and Roaring Bitmaps

    https://www.elastic.co/cn/blog/frame-of-reference-and-roaring-bitmaps http://roaringbitmap.org/ 2015 ...

  7. SumatraPDF设置护眼背景

    高级选项中: 1 FixedPageUI [ 2 TextColor = #000000 3 BackgroundColor = #C7EDCC 4 SelectionColor = #f5fc0c ...

  8. loj10008家庭作业

    题目描述 老师在开学第一天就把所有作业都布置了,每个作业如果在规定的时间内交上来的话才有学分.每个作业的截止日期和学分可能是不同的.例如如果一个作业学分为10 ,要求在6 天内交,那么要想拿到这 10 ...

  9. Prometheus-process exporter-进程监控

    Prometheus-process exporter-进程监控 1.下载安装 2.修改配置文件 3.启动 相关内容原文地址: 博客园:落烨无痕:process exporter 配置项解释 huan ...

  10. Java,Scala:JDBCUtil,MySqlUtil,PhoenixJDBC

    Java,Scala:JDBCUtil,MySqlUtil,PhoenixJDBC pom.xml添加依赖 Java:方式一(亲测实用) 方式二:Scala 方式三:Java PhoenixJDBCU ...