题目

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
2 8

样例输出

4
()()

题解

使用贪心

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

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

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

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

代码

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
const int MAXN = 50005;
char s[MAXN];
int n, lb, rb;
struct Node {
int i, l, r;
bool operator<(const Node &other) const { return r - l < other.r - other.l; }
};
std::priority_queue<Node> pq;
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
int cnt = 0;
long long sum = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == '(') cnt++;
if (s[i] == '?') {
scanf("%d%d", &lb, &rb);
if (i == 1) {
sum += lb;
s[i] = '(';
cnt++;
continue;
}
sum += rb;
s[i] = ')';
pq.push((Node){i, lb, rb});
}
if (s[i] == ')') {
if (cnt == 0) {
if (pq.empty()) return puts("-1"), 0;
Node t = pq.top();
pq.pop();
if (t.i == n) t = pq.top(), pq.pop();
sum = sum - t.r + t.l;
s[t.i] = '(';
cnt += 2 - (t.i == i);
}
if (s[i] == ')') cnt--;
}
}
if (cnt != 0)puts("-1");
else printf("%lld\n%s", sum, s + 1);
return 0;
}

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. 痞子衡嵌入式:降低刷新率是定位LCD花屏显示问题的第一大法(i.MXRT1170, 1280x480 LVDS)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是i.MXRT1170上LCD花屏显示问题的分析解决经验. 痞子衡最近这段时间在参与一个基于i.MXRT1170的大项目(先保个密),需要 ...

  2. css3图片防止变形

    1.object-fit 由于图片原始大小都不一样,强行设定大小值会导致拉伸,如果不设定大小则参差不齐. 之前我们大多数用的 大多数都是利用background-size: cover 来避免对图片造 ...

  3. ReentrantLock原理分析

    一 UML类图 通过类图ReentrantLock是同步锁,同一时间只能有一个线程获取到锁,其他获取该锁的线程会被阻塞而被放入AQS阻塞队列中.ReentrantLock类继承Lock接口:内部抽象类 ...

  4. Spark Streaming,Flink,Storm,Kafka Streams,Samza:如何选择流处理框架

    根据最新的统计显示,仅在过去的两年中,当今世界上90%的数据都是在新产生的,每天创建2.5万亿字节的数据,并且随着新设备,传感器和技术的出现,数据增长速度可能会进一步加快. 从技术上讲,这意味着我们的 ...

  5. Flutter实战】文本组件及五大案例

    老孟导读:大家好,这是[Flutter实战]系列文章的第二篇,这一篇讲解文本组件,文本组件包括文本展示组件(Text和RichText)和文本输入组件(TextField),基础用法和五个案例助你快速 ...

  6. ODEINT 求解常微分方程(1)

    An example of using ODEINT is with the following differential equation with parameter k=0.3, the ini ...

  7. <OPTEE>Trusted Application结构分析

    最近又开始和Trusted Zone打起了交道,需要把Linaro开发的开源安全系统optee os移植到实验室的老板子上.不过导师要求我先开发一个应用,在普通环境和安全环境分别有一个程序,称为hos ...

  8. @uoj - 310@ 【UNR #2】黎明前的巧克力

    目录 @description@ @solution@ @accepted code@ @details@ @description@ Evan 和 Lyra 都是聪明可爱的孩子,两年前,Evan 开 ...

  9. 不适合使用Mycat的场景

    1.非分片字段查询 Mycat中的路由结果是通过分片字段和分片方法来确定的.例如下图中的一个Mycat分库方案: 根据 tt_waybill 表的 id 字段来进行分片 分片方法为 id 值取 3 的 ...

  10. 重学 Java 设计模式:实战享元模式「基于Redis秒杀,提供活动与库存信息查询场景」

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 程序员‍‍的上下文是什么? 很多时候一大部分编程开发的人员都只是关注于功能的实现,只 ...