传送门

Description

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

Input

第一行是序列,序列长度不超过\(5~\times10^4\),下面m(m是\(m\)的数量)行有每行\(2\)个数据,第一个是\((\)的代价,第2个是\()\)的代价

Output

第一行输出最小代价

第二行输出被转换的序列

Sample Input

(??)
1 2
2 8

Sample Output

4
()()

Solution

显然DP可做,然而数据范围太大。

DP不能就尝试一下贪心。

考虑一个特殊性质:对于一个括号序列,如果想要保证他合法,则必须保证在任意一个位置,其前缀左括号数必须不小于右括号数。否则一定不合法。同时这个条件是括号序列合法的充要条件。于是考虑按照这个性质贪心,对于任意一个可以修改的位置,贪心的修改成右括号。当序列不合法时,贪心选取修改代价最小的位置改成左括号。因为修改左括号不会使序列不合法,所以这个贪心使正确的。

Code

#include<queue>
#include<cstdio>
#include<cstring>
#define rg register
#define ci const int
#define cl const long long int typedef long long int ll; namespace IO {
char buf[90];
} template<typename T>
inline void qr(T &x) {
rg char ch=getchar(),lst=' ';
while(ch>'9'||ch<'0') lst=ch,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
if(lst=='-') x=-x;
} template<typename T>
inline void qw(T x,const char aft,const bool pt) {
if(x<0) x=-x,putchar('-');
int top=0;
do {
IO::buf[++top]=x%10+'0';
x/=10;
} while(x);
while(top) putchar(IO::buf[top--]);
if(pt) putchar(aft);
} template<typename T>
inline T mmax(const T a,const T b) {return a > b ? a : b;}
template<typename T>
inline T mmin(const T a,const T b) {return a < b ? a : b;}
template<typename T>
inline T mabs(const T a) {return a < 0 ? -a : a;} template<typename T>
inline void mswap(T &a,T &b) {
T temp=a;a=b;b=temp;
} const int maxn = 50010; struct M {
int l,r;
};
M MU[maxn]; struct Zay {
int pos,v;
inline bool operator<(const Zay &_others) const {
return this->v > _others.v;
}
Zay (ci _a=0,ci _b=0) {pos=_a,v=_b;}
}; char st[maxn];
std::priority_queue<Zay>Q; int main() {
scanf("%s",st+1);
int l=strlen(st+1);
if(l&1) return puts("-1")&0;
for(rg int i=1;i<=l;++i) if(st[i] == '?') {
qr(MU[i].l);qr(MU[i].r);
}
rg int cnt=0;ll _ans=0;
for(rg int i=1;i<=l;++i) {
if(st[i] == '(') ++cnt;
else if(st[i] == ')') --cnt;
else {
--cnt;Q.push(Zay(i,MU[i].l-MU[i].r));
st[i]=')';_ans+=MU[i].r;
}
while(cnt<0) {
if(Q.empty()) return puts("-1")&0;
int h=Q.top().pos;Q.pop();
_ans+=MU[h].l-MU[h].r;
st[h]='(';
cnt+=2;
}
}
if(cnt > 0) return puts("-1")&0;
qw(_ans,'\n',true);
printf("%s",st+1);
return 0;
}

Summary

可能需要贪心时,寻找题目的特殊性质,按照特殊性质进行贪心。

【贪心】【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, i ...

  3. cf3D Least Cost Bracket Sequence

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

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

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

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

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

  6. Least Cost Bracket Sequence(贪心)

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

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

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

  8. codeforces 3D . Least Cost Bracket Sequence 贪心

    题目链接 给一个字符串, 由( ) 以及? 组成, 将?换成( 或者 ) 组成合法的括号序列, 每一个?换成( 或者 ) 的代价都不相同, 问你最小代价是多少, 如果不能满足输出-1. 弄一个变量nu ...

  9. CodeForces 3 D.Least Cost Bracket Sequence【贪心+优先队列】

    Description 给出一个括号序列,中间有一些问号,将第i个问号换成左括号代价是a[i],换成右括号代价是b[i],问如果用最少的代价将这个括号序列变成一个合法的括号序列 Input 第一行一个 ...

随机推荐

  1. 第四十篇 Python之设计模式总结-简单工厂、工厂方法、抽象工厂、单例模式

    一. 简单工厂 简单工厂模式(Simple Factory Pattern):是通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类. 简单工厂的用处不大,主要就是一个if... ...

  2. Linux命令应用大词典-第28章 硬件管理

    28.1 lscpu:显示有关CPU架构的信息 28.2 nproc:显示当前进程可用的CPU数目 28.3 chcpu:配置CPU

  3. [JSON].exists( keyPath )

    语法:[JSON].exists( keyPath ) 返回:[True | False] 说明:检测指定键名路径是否存在 示例: Set jsonObj = toJson("{div:{' ...

  4. [HNOI2018]转盘

    [HNOI2018]转盘 给你一个 \(n\) 元环, 你可以在 \(0\) 时刻从任意一个位置出发, 每一秒可以选择往后或者留在原地每个点有个参数 \(T_i\) , 当你走到 \(i\) 的时间 ...

  5. [leetcode-783-Minimum Distance Between BST Nodes]

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

  6. POJ 1921 Paper Cut(计算几何の折纸问题)

    Description Still remember those games we played in our childhood? Folding and cutting paper must be ...

  7. C++并行编程2

    启动线程 查看线程构造函数,接受一个返回值为void的函数.如下: void do_some_work(); std::thread my_thread(do_some_work); 也可以接受一个重 ...

  8. c# 中base64字符串和图片的相互转换

    c#base64字符串转图片用到了bitmap类,封装 GDI+ 位图,此位图由图形图像及其特性的像素数据组成. Bitmap 是用于处理由像素数据定义的图像的对象. 具体bitmap类是什么可以自己 ...

  9. Qt-excel文件操作方法

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt-excel文件操作方法     本文地址:http://techieliang.com/ ...

  10. 转 PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)

    PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)   通过curl_setopt()函数可以方便快捷的抓取网页(采集很方便),curl_setopt 是php的一个 ...