2017-5-14 湘潭市赛 Parentheses 转化思想+贪心 使括号序列合法的最小花费。满足前面左括号的数量>=有括号的数量。
Parentheses
Accepted : Submit :
Time Limit : MS Memory Limit : KB Parentheses Bobo has a very long sequence divided into n consecutive groups. The i-th group consists of li copies of character ci where ci is either "(" or ")". As the sequence may not be valid parentheses sequence, Bobo can change a character in the i-th group from "(" to ")" (and vice versa) with cost di. He would like to know the minimum cost to transform the sequence into a valid one. Note: An empty string is valid.
If S is valid, (S) is valid.
If U,V are valid, UV is valid. Input The input contains zero or more test cases and is terminated by end-of-file. For each test case: The first line contains an integer n. The i-th of the following n lines contains li,ci,di. ≤n≤
≤l1+l2+⋯+ln≤
l1+l2+⋯+ln is even.
≤di≤
The sum of n does not exceed . Output For each case, output an integer which denotes the result.
Sample Input (
(
(
) )
( Sample Output Note For the first sample, Bobo should change only the character in the second group. For the second sample, Bobo should change half of characters in both groups. Source
XTU OnlineJudge /**
题目:Parentheses
链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1266
题意:从左到右有n个连续的组,每一组有Li个括号,要么全是左括号,要么全是右括号,以及该组的每一个左括号翻成右括号,
或者右括号翻成左括号的花费Di.可以对这n个组的括号进行翻转,每一个括号都可以选择翻或者不翻,使整个括号序列是一个合法括号序列。
思路: 我们知道合法括号序列,满足前面左括号的数量>=有括号的数量。前2k个,那么左括号数量>=k个。
首先把所有左括号翻成右括号,那么所有的括号都变成右括号了。可以先计算花费。然后把那些左括号变成右括号的
那些Di变成-Di。那么以后再对它进行翻转的时候,相当于一开始就没有翻转。可以抵消原先加上的花费。 从左到右对所有右括号贪心,满足前面左括号的数量>=有括号的数量的这种规则去处理。维护一个堆存储那些可以翻成左括号的位置,
如果当前需要翻成一个左括号,那么比较当前位置和堆里的,选择一个花费更小的翻成左括号计算花费。同时更新堆。 举个荔枝:
假设没有组的情况。只是纯粹的若干个括号;
位置:
1 2 3 4 5 6 7 8
) ) ) ) ) ) ) )
一开始第一个位置,显然必须翻成左括号,由于堆为空,所以当前翻成左括号。堆仍然为空。
然后对pos=2处理,此时位置2,应该要至少一个左括号,因为前面pos=1已经翻成左括号了,所以当前这个暂时不用翻成左括号。把它扔进堆里。
pos=3;至少要有两个左括号,还差一个。堆顶的是pos=2的花费,当前是pos=3的花费。比较花费更小的,把它翻成左括号,然后更新堆。依次处理。 当然题目是分组的: 第一组:把所有右括号更新到堆。然后计算当前这个位置需要多少个左括号,从堆中取出来。
然后把第二组所有右括号放入堆,然后计算第二组末尾这个位置需要多少个左括号。然后减去原来已经有的左括号数量。
表示还要多少左括号,从堆中取出。依次处理即可。 */ #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const int maxn = 1e5+;
struct node1
{
LL num;
char ch[];
LL cost;
}a[maxn]; struct node
{
LL num;
LL cost;
bool operator < (const node&k)const{
if(cost!=k.cost) return cost>k.cost;///先出小的。
return num>k.num;
}
}t;
priority_queue<node> qu;
/// priority_queue<P, vector<P>, greater<P> > qu;
int main()
{
int n;
while(scanf("%d",&n)==)
{
LL ans = ;
for(int i = ; i < n; i++){
scanf("%I64d%s%I64d",&a[i].num,a[i].ch,&a[i].cost);
if(a[i].ch[]=='('){
ans += a[i].num*a[i].cost;
a[i].cost = -a[i].cost;
}
}
while(!qu.empty()) qu.pop();
LL pre = , sum = , need;
for(int i = ; i < n; i++){
t.num = a[i].num;
t.cost = a[i].cost;
qu.push(t);
sum += a[i].num;
need = (sum+)/;
need = need - pre;
pre = (sum+)/;
while(need>&&!qu.empty()){
t = qu.top();
qu.pop();
if(t.num>need){
t.num -= need;
qu.push(t);
ans += t.cost*need;
break;
}else
{
if(t.num==need){
ans += t.cost*need;
break;
}else
{
need -= t.num;
ans += t.cost*t.num;
}
}
}
}
printf("%I64d\n",ans); }
return ;
}
2017-5-14 湘潭市赛 Parentheses 转化思想+贪心 使括号序列合法的最小花费。满足前面左括号的数量>=有括号的数量。的更多相关文章
- Old Sorting(转化成单调序列的最小次数,置换群思想)
Old Sorting Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit S ...
- Gitlab一键端的安装汉化及问题解决(2017/12/14目前版本为10.2.4)
Gitlab的安装汉化及问题解决 一.前言 Gitlab需要安装的包太TM多了,源码安装能愁死个人,一直出错,后来发现几行命令就装的真是遇到的新大陆一样... ... 装完之后感觉太简单,加了汉化补丁 ...
- [CERC2017]Intrinsic Interval——扫描线+转化思想+线段树
[CERC2017]Intrinsic Interval https://www.luogu.org/blog/ywycasm/solution-p4747# 这种“好的区间”,见得还是比较多的了. ...
- hdu6212[区间dp] 2017青岛ACM-ICPC网络赛
原题: BZOJ1032 (原题数据有问题) /*hdu6212[区间dp] 2017青岛ACM-ICPC网络赛*/ #include <bits/stdc++.h> using name ...
- 2017 icpc 沈阳网络赛
cable cable cable Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- ACM-ICPC 2017 西安赛区现场赛 K. LOVER II && LibreOJ#6062. 「2017 山东一轮集训 Day2」Pair(线段树)
题目链接:西安:https://nanti.jisuanke.com/t/20759 (计蒜客的数据应该有误,题目和 LOJ 的大同小异,题解以 LOJ 为准) LOJ:https://l ...
- B. Dispersed parentheses 记忆化搜索 + 括号序列的状压表示
http://codeforces.com/gym/100633/problem/B B. Dispersed parentheses time limit per test 2 seconds me ...
- Java 第十一届 蓝桥杯 省模拟赛 合法括号序列
合法括号序列 题目 问题描述 由1对括号,可以组成一种合法括号序列:(). 由2对括号,可以组成两种合法括号序列:()().(()). 由4对括号组成的合法括号序列一共有多少种? 答案提交 这是一道结 ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- Saga的实现模式——控制者(Saga implementation patterns – Controller)
https://lostechies.com/jimmybogard/2013/03/14/saga-implementation-patterns-controller/ 之前的文章中我们介绍了观察 ...
- 使用.Net中的WeakDictionary — ConditionalWeakTable
有的时候,我们需要给某些数据添加一些附加信息,一种常用的做法是使用一个Dictionary在填充这些附加信息如: var data = new Data(); var tag = new Tag ...
- SQL:将查询结果插入到另一个表的三种情况!
一:如果要插入目标表不存在: select * into 目标表 from 表 where ... 二:如果要插入目标表已经存在: insert into 目的表 select * from 表 wh ...
- OpenCV 64位时 应用程序无法正常启动0x000007b 问题解决
这问题根本不是DirectX问题,不知道网上怎么这么这样的回复.而且也不亲自验证一下.下面将自己花很多时间才解决的方式整理一下. 因为一般情况下你配置的OpenCV加入系统环境变量的都是X86下的bi ...
- iptables禁止外网访问redis server服务默认端口6379的命令
//只允许127.0.0.1访问6379 iptables -A INPUT -s 127.0.0.1 -p tcp --dport 6379 -j ACCEPT //其他ip访问全部拒绝 iptab ...
- 引用日志log4net.dll的web.config配置
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigu ...
- iOS开发学习 阶段过程简述
下面就简单介绍一下我iOS开发的感受,也是学习iOS开发的一个体系架构. 1 iOS开发环境 1.1 开发环境 标准的配置是Mac OS X + Xcode. MacOSX的话首选用苹果电脑,macm ...
- RUEI 13.1.1版本在OEL 5.7上的安装
准备工作 ntp的工作和同步 /sbin/chkconfig --list | grep ntpd ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off /sb ...
- html DOM 的继承关系
零散的知识聚合在一起,就会形成力量,就有了生命力. 如各种语言的开发框架, 都是右各个碎片化的功能聚合在一起,构成有机地整体,便有了强大的力量.will be powerful! 如: jquery ...
- 安装dubbo-admin报错 URIType BeanCreationException
安装dubbo-admin报错 URIType BeanCreationException 学习了:https://blog.csdn.net/lsm135/article/details/52725 ...