欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

股神小L

发布时间:2025/7/25 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 股神小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

总结

以上是生活随笔为你收集整理的股神小L的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。