| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
优先队列#include<iomanip> #include<iostream> #include<queue> #include<string.h> #include<algorithm> using namespace std; typedef long long ll; const int N=2005; int n,a[N]; priority_queue<int,vector<int>,greater<int> > q; priority_queue<int,vector<int>,less<int> > p; int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } int ans1=0,ans2=0; for(int i=1;i<=n;i++){ if(q.empty())q.push(a[i]); else{ if(a[i]<=q.top())q.push(a[i]); else{ ans1+=abs(q.top()-a[i]); q.pop(); q.push(a[i]);q.push(a[i]); } } } for(int i=1;i<=n;i++){ if(p.empty())p.push(a[i]); else{ if(a[i]>=p.top())p.push(a[i]); else{ ans2+=abs(p.top()-a[i]); p.pop(); p.push(a[i]);p.push(a[i]); } } } cout<<min(ans1,ans2); return 0; } 转载自https://www.cnblogs.com/lefy959/p/16573371.html Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator