Description
给定一个队列q,队列中的每个元素都有两种属性name和pri。对于队列有3中操作分别为: 1 name pri 代表向队列中添加(name,pri)这个元素。 2 代表输出队列中pri最高的元素的name并将该元素移除队列。 3 代表输出队列中pri最低的元素的name并将该元素移除队列。 假设队列初始为空,且所有元素的name值各不相同,pri值也各不相同。给定若干操作,对于2和3操作输出相应元素的name。
Input
每行输入分别代表一种操作,若输入为0则结束。(1<=name,pri<=10^6)
Output
对于每个2和3操作,输出对应的值。若队列中没有元素则输出0。
Sample Input
2
1 20 14
1 30 3
2
1 10 99
3
2
2
0
Sample Output
0
20
30
10
0
直接开了个结构题数组模拟,感觉就在T的边缘,l,r两个标记“队列”的头和尾,每次更新即可。
#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define mod 998244353
#define INF 0x3f3f3f
#define fi first
#define se second
#define it iterator
#define ins insert
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define np next_permutation
#define pq priority_queue
#define ll long long
#define ull unsigned long long
#define mem(a) memset(a,0,sizeof(a))
#define cio ios::sync_with_stdio(false)
#define gcd __gcd
ll lgcd(ll a,ll b){return b == 0? a:lgcd(b, a % b);}
int lowbit(int x){return x&(-x);}
#define T int t;scanf("%d",&t);while(t--)
#define Ce cout << endl
#define CE(n) cout << n << endl
#define C(n) cout << n;
#define CY cout << "YES" <<
#define CN cout << "NO" << endl
#define Cy cout << "Yes" << endl
#define Cn cout << "No" << endl
struct node
{
int n, pr;
}s[1000010];
bool cmp(node a, node b)
{
return a.pr < b.pr;
}
int main()
{
int op;
int l = 0;
int r = 0;
while(cin>>op){
if(op==0) break;
if(op==1){
int x, y;
cin >> x >> y;
s[r].n = x;
s[r].pr = y;
r++;
}else if(op==2){
if(r<=l){
CE(0);
}else{
sort(s+l,s+r,cmp);
CE(s[r-1].n);
r--;
}
}else if(op==3){
if(r<=l){
CE(0);
}else{
sort(s+l,s+r,cmp);
CE(s[l].n);
l++;
}
}
}
return 0;
}