CF865D Buy Low Sell High

传送门

感觉自己真的蠢
这题用堆来做
对于每个数都考虑当前时间点强制卖出,所以每次选择堆中最小的去统计答案
这样不一定最优,所以考虑将除了第一个点外的其他点加两次
这样统计答案时,如果堆中找到的是当前点,那么直接去掉,不产生贡献,说明当前点只能买入
否则找到其他点,就视作在当前点卖出,无脑在当前点卖出当然不一定优,但是下次踢掉当前点时,可以视作当前点的卖出操作被撤销了
这样的话第二次踢出一个点才视为在这个点买入,在当前点卖出
现在说明为什么第一个点只加\(1\)次:
因为第一个点没办法卖出,所以省略了一次插入
代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
void read(int &x) {
	char ch; bool ok;
	for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
	for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=3e5+10;
int n;long long ans;
priority_queue<int>q;
int main()
{
	read(n);int g;read(g);q.push(-g);
	for(rg int i=2,x;i<=n;i++)
	{
		read(x);
		q.push(-x),q.push(-x);
		ans+=x+q.top();q.pop();
	}
	printf("%lld\n",ans);
}
posted @ 2019-04-01 21:05  蒟蒻--lichenxi  阅读(276)  评论(0编辑  收藏  举报