| ||||||||||
| 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 | |||||||||
一个最大堆一个最小堆,188ms#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator