Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

优先队列

Posted by luobotianle at 2024-02-16 10:12:54 on Problem 3666 and last updated at 2024-02-16 22:01:41
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator