题目

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.

给一个序列,序列里面会有左括号、问号、右括号。对于一个?而言,可以将其替换为一个(,也可以替换成一个),但是都有相应的代价。问:如何替换使得代价最小。前提是替换之后的序列中,括号是匹配的。如果不能替换为一个括号匹配的序列则输出-1。

输入格式

The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn't exceed \(5·10^4\). Then there follow \(m\) lines, where \(m\) is the number of characters "?" in the pattern. Each line contains two integer numbers \(a_i\) and \(b_i (1 ≤ a_i,  b_i ≤ 10^6)\), where \(a_i\) is the cost of replacing the i-th character "?" with an opening bracket, and \(b_i\) — with a closing one.

第一行是序列,序列长度不超过50000,下面m(m是?的数量)行有每行2个数据,第一个是(的代价,第2个是)的代价

输出格式

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.

第一行打印代价,第二行打印替换后的序列。不行输出-1

样例输入

  1. (??)
  2. 1 2
  3. 2 8

样例输出

  1. 4
  2. ()()

题解

使用贪心

先把每个?变成右括号,如果这时候发现这个右括号没有左括号和它匹配,就从当前位置往前在所有没变成左括号的?中选择变为左括号产生的花费最少的一个,转成左括号.

写代码的时候, 扫描每个?,将它赋值为左括号, 然后将其加入优先队列, 找花费最少的时候, 直接从优先队列里找

如果最后还是存在未匹配的括号,就是无解的情况.

另外统计价值之和时要开long long.

代码

  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. const int MAXN = 50005;
  6. char s[MAXN];
  7. int n, lb, rb;
  8. struct Node {
  9. int i, l, r;
  10. bool operator<(const Node &other) const { return r - l < other.r - other.l; }
  11. };
  12. std::priority_queue<Node> pq;
  13. int main() {
  14. scanf("%s", s + 1);
  15. n = strlen(s + 1);
  16. int cnt = 0;
  17. long long sum = 0;
  18. for (int i = 1; i <= n; i++) {
  19. if (s[i] == '(') cnt++;
  20. if (s[i] == '?') {
  21. scanf("%d%d", &lb, &rb);
  22. if (i == 1) {
  23. sum += lb;
  24. s[i] = '(';
  25. cnt++;
  26. continue;
  27. }
  28. sum += rb;
  29. s[i] = ')';
  30. pq.push((Node){i, lb, rb});
  31. }
  32. if (s[i] == ')') {
  33. if (cnt == 0) {
  34. if (pq.empty()) return puts("-1"), 0;
  35. Node t = pq.top();
  36. pq.pop();
  37. if (t.i == n) t = pq.top(), pq.pop();
  38. sum = sum - t.r + t.l;
  39. s[t.i] = '(';
  40. cnt += 2 - (t.i == i);
  41. }
  42. if (s[i] == ')') cnt--;
  43. }
  44. }
  45. if (cnt != 0)puts("-1");
  46. else printf("%lld\n%s", sum, s + 1);
  47. return 0;
  48. }

CF3D Least Cost Bracket Sequence 题解的更多相关文章

  1. CF3D Least Cost Bracket Sequence 贪心

    Least Cost Bracket Sequence CodeForces - 3D 题目描述 This is yet another problem on regular bracket sequ ...

  2. cf3D Least Cost Bracket Sequence

    This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if b ...

  3. CF3D Least Cost Bracket Sequence(2500的实力贪心...

    哎,昨天一直在赶课设..没有写 最近听了一些人的建议,停止高级算法的学习,开始刷cf. 目前打算就是白天懒得背电脑的话,系统刷一遍蓝书紫书白书之类的(一直没系统刷过),回宿舍再上机吧. https:/ ...

  4. 【贪心算法】CF3D Least Cost Bracket Sequence

    题目大意 洛谷链接 给一个序列,序列里面会有左括号.问号.右括号.对于一个?而言,可以将其替换为一个(,也可以替换成一个),但是都有相应的代价.问:如何替换使得代价最小.前提是替换之后的序列中,括号是 ...

  5. Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列

    D. Least Cost Bracket Sequence 题目连接: http://www.codeforces.com/contest/3/problem/D Description This ...

  6. Least Cost Bracket Sequence(贪心)

    Least Cost Bracket Sequence(贪心) Describe This is yet another problem on regular bracket sequences. A ...

  7. 【贪心】【CF3D】 Least Cost Bracket Sequence

    传送门 Description 给一个序列,序列里面会有左括号.问号.右括号.对于一个\(?\)而言,可以将其替换为一个\((\),也可以替换成一个\()\),但是都有相应的代价.问:如何替换使得代价 ...

  8. Least Cost Bracket Sequence,题解

    题目链接 题意: 给你一个含有(,),?的序列,每个?变成(或)有一定的花费,问变成课匹配的括号的最小花费. 分析: 首先如果能变成匹配的,那么就有右括号的个数始终不多于左括号且左右括号数量相等,那就 ...

  9. CF524F And Yet Another Bracket Sequence 题解

    题目链接 算法:后缀数组+ST表+贪心   各路题解都没怎么看懂,只会常数巨大的后缀数组+ST表,最大点用时 \(4s\), 刚好可以过... 确定合法序列长度   首先一个括号序列是合法的必须满足以 ...

随机推荐

  1. 面试三轮我倒在了一道sql题上——sql性能优化

    一.前言 最近小农在找工作,因为今年疫情的特殊原因,导致工作不是特别好找,所以一旦有面试电话,如果可以,都会去试一试,刚好接到一个面试邀请,感觉公司还不错,于是就确定了面试时间,准备了一下就去面试了. ...

  2. jenkins+svn 自动化上线

    一.环境: [root@bimvm01 ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@bimvm01 ~ ...

  3. jdbc+mysql常见报错总结

    1.The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You ...

  4. 实现saas多租户方案比较

    看到一篇比较多租户数据隔离方案的文章,总结挺不错.其实大部分内容在我前几年写的文章都有. 文章翻译自: https://blog.arkency.com/comparison-of-approache ...

  5. SpringBoot整合分布式ZooKeeper和Dubbo

    ZooKeeper ZooKeeper是一个分布式的,开放远吗的分布式应用程序协调服务.它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护.域名服务.分布式同步.组服务等. 服务提供者 ...

  6. Cookie和localStorage的查询、设置、修改及删除

    感谢:链接(视频讲解很详细) cookie:是一种字符串表示的数据,用于在本地记录用户的基本信息(账号,密码等),具有时限性. 数据的具体内容: (图源上文视频链接) localStorage:与co ...

  7. pip 安装使用国内镜像

    pip国内的一些镜像 阿里云 https://mirrors.aliyun.com/pypi/simple/中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple ...

  8. 最后一面挂在volatile关键字上,面试官:重新学学Java吧!

    最后一面挂在volatile关键字上,面试官:重新学学Java吧! 为什么会有volatile关键字? volatile: 易变的; 无定性的; 无常性的; 可能急剧波动的; 不稳定的; 易恶化的; ...

  9. JCreator配置的Java学习环境

    绕不开的配置,很多东西需要它,论精力现在还折腾不来,可总不至于去见马克思的那一天才来啊,该来的就该来不躲避(Py及其Android.BigData都绕不开,总是触动着我)!不想那些庞大耗内存的Ecli ...

  10. 深度解密 Go 语言之 sync.map

    工作中,经常会碰到并发读写 map 而造成 panic 的情况,为什么在并发读写的时候,会 panic 呢?因为在并发读写的情况下,map 里的数据会被写乱,之后就是 Garbage in, garb ...