C-Making the Grade
优先队列的 O ( n l o g n ) O(nlogn) O(nlogn) 神仙解法(分析待补充,现在讲不灵清)
可以先看一下大佬的:ButterflyDew 的博客
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N];
priority_queue < int,vector<int>,less<int> > q;
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int ans1=0,ans2=0;
for(int i=1;i<=n;i++)
{
q.push(a[i]);
if(a[i]<q.top())
{
ans1+=q.top()-a[i];
q.pop();
q.push(a[i]);
}
}
while(!q.empty()) q.pop();
for(int i=n;i>=1;i--)
{
q.push(a[i]);
if(a[i]<q.top())
{
ans2+=q.top()-a[i];
q.pop();
q.push(a[i]);
}
}
cout<<min(ans1,ans2)<<endl;
return 0;
}