当前位置:
首页 >
股神小L
发布时间:2025/7/25
50
豆豆
题解 贪心
若当前手中还持有股,则一定会卖出去。
否则,考虑之前卖出的最便宜的股,若售价比当前的股高,就买下这个股,否则我们就把之前卖出的最便宜的股改为买入,这样一定会有股,然后再把这个股卖出即可。
简单题,用堆维护即可。
#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
总结
- 上一篇: 自定义View
- 下一篇: Nowcoder 提高组练习赛-R3