poj1281

简单题

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

#define maxn 1000005
#define maxm 10004

struct Node1
{
int a;
Node1(
int aa) :
a(aa)
{
}
};

struct Node2
{
int a;
Node2(
int aa) :
a(aa)
{
}
};

int m, n, p;
int now, f[maxn], cnt;
int num[maxm];

priority_queue
<Node1> q1;
priority_queue
<Node2> q2;

bool operator <(const Node1 &a, const Node1 &b)
{
return a.a > b.a;
}

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

void work()
{
cnt
++;
bool found = false;
if (p == 1)
{
Node1 x(
-1);
while (!q1.empty())
{
x
= q1.top();
q1.pop();
if (num[x.a] != 0)
{
found
= true;
break;
}
}
if (num[x.a])
num[x.a]
--;
if (now < n && f[now] == cnt)
{
if (found)
printf(
"%d\n", x.a);
else
printf(
"-1\n");
now
++;
}
}
else
{
Node2 x(
-1);
while (!q2.empty())
{
x
= q2.top();
q2.pop();
if (num[x.a] != 0)
{
found
= true;
break;
}
}
if (num[x.a])
num[x.a]
--;
if (now < n && f[now] == cnt)
{
if (found)
printf(
"%d\n", x.a);
else
printf(
"-1\n");
now
++;
}
}
}

int main()
{
//freopen("t.txt", "r", stdin);
bool first = true;
while (scanf("%d%d", &m, &n) != EOF)
{
while (!q1.empty())
q1.pop();
while (!q2.empty())
q2.pop();
if (first)
first
= false;
else
putchar(
'\n');
for (int i = 0; i < n; i++)
scanf(
"%d", &f[i]);
memset(num,
0, sizeof(num));
p
= 1;
char st[5];
now
= 0;
cnt
= 0;
while (scanf("%s", st), st[0] != 'e')
{
if (st[0] == 'a')
{
int a;
scanf(
"%d", &a);
q1.push(Node1(a));
q2.push(Node2(a));
num[a]
++;
}
else if (st[0] == 'p')
{
int a;
scanf(
"%d", &a);
p
= a;
}
else
work();
}
}
return 0;
}

你可能感兴趣的:(poj)