思路:
这题是一道贪心题,每个股票在最低谷买最高峰买,就能获得最大利益,这题主要的坑点在于输入要优化,否则会TLE
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,ans;
priority_queue<ll,vector<ll>,greater<ll> >q;
inline ll read()
{
char c=getchar();
ll t=0;
while(c>'9'||c<'0')c=getchar();
while(c>='0'&&c<='9')
{
t=t*10+c-'0';
c=getchar();
}
return t;
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
{
ll x=read();
if(q.empty()||q.top()>=x)q.push(x);
else
{
ans+=x-q.top();
q.pop();
q.push(x);
q.push(x);
}
}
printf("%lld\n",ans);
return 0;
}
来源:zr