序列

黑题纪念

自己的是\(DP\)的思路,但数据加强版明显不能\(DP\)
结合两篇题解终于明白了思路:

1
2

错点:当堆顶的元素需要退栈的时候(向下移动),当前的元素\(x\)要进堆两次。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define LL long long
using namespace std;
priority_queue<LL> q;
LL n,x,ans;
int main()
{
	scanf("%lld",&n);
	for(LL i=1;i<=n;i++)
	{
		scanf("%lld",&x);
		if(q.empty()||q.top()<=x)
		 q.push(x);
		if(x<q.top())
		{
			ans+=q.top()-x;
			q.pop(); q.push(x); q.push(x);
		}
	}
	printf("%lld\n",ans);
	return 0;
}
posted @ 2019-08-21 16:48  蟹蟹王  阅读(128)  评论(0编辑  收藏  举报