xtu 1266 Parentheses 2017湘潭邀请赛G
来源:互联网 发布:淘宝个性化标签 算法 编辑:程序博客网 时间:2024/06/04 20:09
Parentheses
Bobo has a very long sequence divided into (
" or ")
".
As the sequence may not be valid parentheses sequence, Bobo can change a character in the (
" to ")
" (and vice versa) with cost
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
1≤n≤105 1≤l1+l2+⋯+ln≤109 l1+l2+⋯+ln is even.1≤di≤109 - The sum of
n does not exceed106 .
Output
For each case, output an integer which denotes the result.
Sample Input
41 ( 11 ( 21 ( 31 ) 42500000000 ) 1000000000500000000 ( 1000000000
Sample Output
2500000000000000000
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.
#include <stdio.h>#include <algorithm>#include <vector>#include <string.h>#include <queue>#include <cmath>using namespace std; #define ll __int64#define N 100005struct p{ ll l,d; char c; bool operator < (const p&r)const { return d>r.d; }} a[N]; int main(){ int n; while(~scanf("%d",&n)) { priority_queue<p>q; ll ans=0; for(int i=1; i<=n; i++) { scanf("%I64d %c %I64d",&a[i].l,&a[i].c,&a[i].d); if(a[i].c=='(') { ans+=a[i].l*a[i].d; a[i].d=-a[i].d; } } ll len=0;///已经确定了多少个左括号 ll sum=0;///总长 ll temp;///temp为需要的左括号 for(int i=1; i<=n; i++) { sum+=a[i].l; q.push(p {a[i].l,a[i].d}); ll temp=(sum+1)/2; if(len<temp) { temp=temp-len; while(!q.empty()) { p t=q.top(); q.pop(); if(t.l>=temp) { ans+=temp*t.d; t.l-=temp; len+=temp; temp=0; if(t.l!=0) q.push(p {t.l,t.d}); break; } else { len+=t.l; temp-=t.l; ans+=t.l*t.d; } } if(temp>0) { ans+=(temp+1)/2*a[i].d; if(temp/2) q.push(p{temp/2,a[i].d}); } } } printf("%I64d\n",ans); } return 0;}
- xtu 1266 Parentheses 2017湘潭邀请赛G
- 湘潭邀请赛G- Parentheses(贪心,思维)
- xtu 1264 Partial Sum 2017湘潭邀请赛E
- 2016湘潭邀请赛 XTU 1243 2016
- xtu 1267 Highway 湘潭邀请赛H
- xtu 1268 Strange Optimization 湘潭邀请赛I
- xtu 1268 Strange Optimization 湘潭邀请赛I
- 湘潭邀请赛 Parentheses 贪心 优先队列
- 【XTU】2016湘潭邀请赛 I Substring Query【AC自动机+分块】
- 2016湘潭邀请赛 xtu 1243 矩阵快速幂
- XTU 1203 A simple problem (2014 湖南湘潭邀请赛 A题)(数学)
- 2017 ccpc 湘潭邀请赛 H highway
- 2017 湘潭大学邀请赛H题--Highway
- ACM_ICPC 湘潭邀请赛
- 湘潭邀请赛总结
- 湘潭邀请赛A
- 2015湘潭邀请赛小记
- 2015 湘潭邀请赛 Fraction
- HttpClient 4.3详细教程
- AFL(American Fuzzy Lop)使用(一)
- linux grep
- sparkSQL元数据缓存踩的坑
- 微信小程序从入门到放弃(五)
- xtu 1266 Parentheses 2017湘潭邀请赛G
- 多线程NSOperation--自定义非并行的 NSOperation(二)
- SpringMVC用ajax到前台乱码解决办法
- vxWorks内核解读六--初始化
- Java常用的八种排序算法与代码实现
- java中getter和setter方法的理解
- MFC控件大小随窗体大小而改变
- 卷积神经网络
- java中的单例设计模式