题解 CF865D 【Buy Low Sell High】
反悔贪心。
先贪心地考虑,每次考虑股票
但是这个贪心显然是错误的,考虑反悔。比如
代码通俗易懂。实现的时候小根堆我是用负数实现的。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
priority_queue<ll>q;
ll n,x,ans;
int main()
{
scanf("%lld",&n);
for (int i=1;i<=n;i++)
{
scanf("%lld",&x);
if (q.size()&&-q.top()<x)
{
ans+=x+q.top();
q.pop();
q.push(-x);
}
q.push(-x);
}
cout << ans << endl;
return 0;
}