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

一个最大堆一个最小堆,188ms

Posted by KatrineYang at 2016-10-24 13:44:28 on Problem 1442
#include <iostream>
#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;

struct cmpDa{
 bool operator()(const int &a,const int &b)//这里的&是引用
 {
  return a>b;//最大堆
 }
};

struct cmpXiao{
	bool operator()(const int &a,const int &b)//这里的&是引用
	 {
	  return a<b;//最小堆
	 }
};

priority_queue< int, vector<int>, cmpXiao > zuo;
priority_queue< int, vector<int>, cmpDa > you;
int M, N;
int A[30003], u[30003];

void add(int s){
	bool ze = zuo.empty(), ye = you.empty();
	if(ze){
		you.push(s);
	}
	else if((!ye && s >= you.top()) || (!ze && s >= zuo.top())){
		you.push(s);
	}
	else{
		int tt = zuo.top();
		zuo.pop();
		you.push(tt);
		zuo.push(s);
	}
}

int get(){
	int ans = you.top();
	you.pop();
	zuo.push(ans);
	return ans;
}

int main() {
	scanf("%d%d", &M, &N);
	for(int i = 1; i <= M; i++) scanf("%d", &A[i]);
	for(int i = 1; i <= M; i++) u[i] = 0;
	int maxHave = 0;
	for(int i = 1; i <= N; i++){
		int t;
		scanf("%d", &t);
		u[t]++;
		if(t > maxHave) maxHave = t;
	}
	for(int i = 1; i <= maxHave; i++){
		add(A[i]);
		for(int j = 0; j < u[i]; j++) printf("%d\n", get());
	}
	return 0;
}

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