XTU1266:Parentheses(贪心+优先队列)
传送门
题意
从左到右有n个连续的组,每一组有Li个括号,要么全是左括号,要么全是右括号,以及该组的每一个左括号翻成右括号,
或者右括号翻成左括号的花费Di.可以对这n个组的括号进行翻转,每一个括号都可以选择翻或者不翻,使整个括号序列是一个合法括号序列。
分析
首先读入的时候将所有左括号变成右括号,并将费用变成负费用
遍历组,每次计算当前需要多少左括号,条件是如果当前有n个括号,则左括号的个数需>=(n+1)/2。并且将该组括号入优先队列。取优先队列队首进行处理即可
trick
1.XTU的读入读出都要%I64d,就很GG
2.代码一开始很多预处理没写好,要注意
3.大家不妨做做codeforces 3D
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))
#pragma comment(linker, "/STACK:102400000,102400000")
inline void read(int &x){x=0; char ch=getchar();while(ch<'0') ch=getchar();while(ch>='0'){x=x*10+ch-48; ch=getchar();}}
int n;
struct node
{
ll num,cost;
bool operator<(const node &p)const
{
return (cost==p.cost)?num<p.num:cost>p.cost;
}
}a[100100];
ll ans;
ll pre,sum,tmp;
priority_queue<node>q;
char s[10];
int main()
{
while(scanf("%d",&n)!=EOF)
{
while(!q.empty()) q.pop();
ans=pre=sum=0;
for(int i=1;i<=n;++i)
{
scanf("%I64d%s%I64d",&a[i].num,s,&a[i].cost);
if(s[0]=='(') {ans+=a[i].num*a[i].cost;
a[i].cost=-a[i].cost;}
}
for(int i=1;i<=n;++i)
{
sum+=a[i].num;
tmp=(sum+1)/2-pre;
pre=(sum+1)/2;
node tmp1;
tmp1.cost=a[i].cost;tmp1.num=a[i].num;
q.push(tmp1);
//printf("tmp=%lld\n",tmp);
while(tmp>0&&!q.empty())
{
node tmp2=q.top();q.pop();
//printf("%lld %lld\n",tmp2.num,tmp2.cost);
if(tmp2.num>tmp)
{
tmp2.num-=tmp;
ans+=tmp2.cost*tmp;
q.push(tmp2);
break;
}
else
{
if(tmp2.num==tmp)
{
ans+=tmp2.cost*tmp;break;
}
else
{
tmp-=tmp2.num;
ans+=tmp2.cost*tmp2.num;
}
}
}
}
printf("%I64d\n",ans);
}
return 0;
}
XTU1266:Parentheses(贪心+优先队列)的更多相关文章
- hihoCoder 1309:任务分配 贪心 优先队列
#1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN, ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
- C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列
C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- 贪心+优先队列 HDOJ 5360 Hiking
题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...
- [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)
传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...
- Painting The Fence(贪心+优先队列)
Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...
- CF140C New Year Snowmen(贪心+优先队列)
CF140C 贪心+优先队列 贪心策略:每次取出数量最多的三种球,合成一个答案,再把雪球数都-1再插回去,只要还剩下三种雪球就可以不断地合成 雪球数用优先队列维护 #include <bits/ ...
- BZOJ1029: [JSOI2007]建筑抢修[模拟 贪心 优先队列]
1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec Memory Limit: 162 MBSubmit: 3785 Solved: 1747[Submit][Statu ...
随机推荐
- [Testing] Config jest to test Javascript Application -- Part 3
Run Jest Watch Mode by default locally with is-ci-cli In CI, we don’t want to start the tests in wat ...
- insertion-sort-list——链表、插入排序、链表插入
Sort a linked list using insertion sort. PS:需要考虑left为head且只有一个数时,此时left->==NULL,若right<left则应更 ...
- DirectX11 学习笔记5 - D3DXMath 库
directx11 特别推出了一个数学库 用于向量 矩阵的计算.并且使用128位 同一时候计算4条32位数据 (SIMD) 之前基本是用的directx10math.h这个头文件. 这个数学库既然是 ...
- 20160222.CCPP体系具体解释(0032天)
程序片段(01):宽字符.c+字符串与内存四区.c 内容概要:宽窄字符 ///宽字符.c #include <stdio.h> #include <stdlib.h> #inc ...
- ermissions on /usr/local/mongodb/conf/keyFile are too open
ermissions on /usr/local/mongodb/conf/keyFile are too open > rs.initiate(cfg); { "ok" : ...
- the hard problems when writing a great connector; type cohersion, data partitioning and data locality to name a few
http://rosslawley.co.uk/introducing-a-new=mongodb-spark-connector/
- VC实现趋势图绘制
本文参考pudn上一个完整工程,在pudn搜索“50815867CurveDrawing”即可找到源代码. 上图是使用VS2010重写了该软件后的效果图,下面再贴出关键代码: // Plot.cp ...
- 大O符号
大O 就是 时间复杂度.时间复杂度是大概的描述一个算法的用时(实际上从侧面的表达了他的效率) 最好时间复杂度,通常指在最好情形下,这个算法用时.反之,最坏情况下的就是最坏时间复杂度.通常 冒泡算法 的 ...
- G.易彰彪的一张表
易彰彪最近有点奇怪,一向爱打游戏他最近居然盯着一张全是大小写字母的表在看,好像在找什么东西.他说,这是他女神给他的一张表,他需要回答女神的问题——在忽略大小写(即大写字母和小写字母视为同一字母)的情况 ...
- NOIP2016总结
Day1: T1:模拟: #include<iostream> #include<cstdio> #include<cstdlib> #include<cst ...