股神小L

 

题解 贪心

若当前手中还持有股,则一定会卖出去。

否则,考虑之前卖出的最便宜的股,若售价比当前的股高,就买下这个股,否则我们就把之前卖出的最便宜的股改为买入,这样一定会有股,然后再把这个股卖出即可。

简单题,用堆维护即可。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define M 200020
#define mid ((l+r)>>1)
using namespace std;
LL read(){
	LL nm=0,fh=1; char cw=getchar();
	for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh;
	for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0');
	return nm*fh;
}
LL n,m,ans;
priority_queue<LL> Q;
int main(){
	n=read();
	for(LL tot=0,i=1;i<=n;i++){
		m=read();
		if(tot) tot--,ans+=m,Q.push(-m);
		else if(!Q.empty()&&m>-Q.top()){
		    ans+=m+(Q.top()*2ll);
			Q.pop(),Q.push(-m),tot++;
		}
		else tot++,ans-=m;
	}
	printf("%lld\n",ans); return 0;
}

  

 

转载于:https://www.cnblogs.com/OYJason/p/9693180.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值