社区讨论
AC了但是不懂
CF865DBuy Low Sell High参与者 3已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @lohuzd06
- 此快照首次捕获于
- 2023/11/03 08:09 2 年前
- 此快照最后确认于
- 2023/11/03 13:49 2 年前
为什么需要将满足条件的点在优先队列里 push 两边才行,只 push 一遍的话只能过第一个样例
CPP#include<bits/stdc++.h>
#define N 300010
#define int long long
using namespace std;
int ans,n,p[N];
priority_queue<int,vector<int>,greater<int> >q;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n;
for(int i = 1;i<=n;i++)
cin>>p[i];
for(int i = 1;i<=n;i++){
q.push(p[i]);
if(q.size()&&q.top()<p[i]){
ans+=p[i]-q.top();
q.pop();
q.push(p[i]);
}
}
cout<<ans;
return 0;
}
回复
共 5 条回复,欢迎继续交流。
正在加载回复...