题意:N天不同的价格,问你最好的方式得到最多的钱。
优先队列模拟,从前往后,所有遍历过的都放入优先队列当中,高价卖的那天可能后面还有更小的可以去卖,那么好办法就是让这天可以后悔。那么再push一次就可以后悔了。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define en '\n'
int rd(){
int tt;scanf("%d",&tt);return tt;
}
priority_queue<int,vector<int>,greater<int> >q;
signed main(){
#ifdef swt
freopen("input2.txt","r",stdin);
#endif // swt
int n;
cin>>n;
ll ans=0;
while(n--){
int tem=rd();
if(q.size() and q.top()<tem){
ans+=tem-q.top();
q.pop();
q.push(tem);
}
q.push(tem);
}
cout<<ans<<en;
return 0;
}
本文介绍了一种利用优先队列实现的股票买卖策略算法。通过将每日股价存入优先队列,并在遇到更高价时卖出,实现后悔机制,以获取最大利润。该算法适用于处理N天内不同价格的数据,通过不断调整队列状态,找到最佳买卖时机。


被折叠的 条评论
为什么被折叠?



