poj3481

priority_queue如果想同时建立小根堆和大根堆需要这样写

priority_queue<Elem, vector<Elem> , greater<Elem> > pq3; //小根
priority_queue<Elem, vector<Elem> , less<Elem> > pq2;//大根

并重载大于和小于号

View Code
#include <iostream>
#include
<cstdlib>
#include
<cstring>
#include
<cstdio>
#include
<queue>
#include
<vector>
using namespace std;

#define maxn 1000005

struct Elem
{
int id, p;
Elem()
{
}
Elem(
int ii, int pp) :
id(ii), p(pp)
{
}
};

int vis[maxn];

priority_queue
<Elem, vector<Elem> , greater<Elem> > pq3;
priority_queue
<Elem, vector<Elem> , less<Elem> > pq2;

bool operator <(const Elem &a, const Elem &b)
{
return a.p < b.p;
}

bool operator >(const Elem &a, const Elem &b)
{
return a.p > b.p;
}

int main()
{
//freopen("t.txt", "r", stdin);
int x;
memset(vis,
-1, sizeof(vis));
while (scanf("%d", &x), x)
{
if (x == 1)
{
int a, b;
scanf(
"%d%d", &a, &b);
vis[a]
= b;
pq2.push(Elem(a, b));
pq3.push(Elem(a, b));
}
else if (x == 2)
{
Elem a;
if (pq2.empty())
{
printf(
"0\n");
continue;
}
do
{
a
= pq2.top();
pq2.pop();
}
while (!pq2.empty() && a.p != vis[a.id]);
if (a.p == vis[a.id])
{
printf(
"%d\n", a.id);
vis[a.id]
= -1;
}
else
printf(
"0\n");
}
else
{
Elem a;
if (pq3.empty())
{
printf(
"0\n");
continue;
}
do
{
a
= pq3.top();
pq3.pop();
}
while (!pq3.empty() && a.p != vis[a.id]);
if (a.p == vis[a.id])
{
printf(
"%d\n", a.id);
vis[a.id]
= -1;
}
else
printf(
"0\n");
}
}
return 0;
}

你可能感兴趣的:(poj)