悬关,(关洛谷,老师能看到最好,不知道怎么进智灵班讨论区

题目描述:
幼儿园里的小朋友在玩排队游戏,他们会根据老师的要求排队。

老师共进行

n 次操作,操作分为以下三种:

1 x : 将一名身高为

x 的小朋友加入队尾

2 : 输出队列最前面的小朋友的身高,并让他出队列,保证进行该操作时队列非空

3 : 将队列里的小朋友按照身高升序排序

输入格式:
第一行,包含一个正整数

n,表示操作次数。

加下来

n行按照以下格式之一输入操作:

1 x

2

3

输出格式:
对应操作进行输出。

样例1输入:
9
1 1
1 3
1 2
3
2
2
1 0
3
2
样例1输出:
1
2
0

约定与解释:
对于100%的数据,
1



2
×
1
0
5
;
0



1
0
9
1≤n≤2×10
5
;0≤x≤10
9

样例1解释:

第1个操作后,队列为
[
1
]
[1];

第2个操作后,队列为
[
1
,
3
]
[1,3];

第3个操作后,队列为
[
1
,
3
,
2
]
[1,3,2];

第4个操作后,队列为
[
1
,
2
,
3
]
[1,2,3];

第5个操作后,队列为
[
2
,
3
]
[2,3];

第6个操作后,队列为
[
3
]
[3];

第7个操作后,队列为
[
3
,
0
]
[3,0];

第8个操作后,队列为
[
0
,
3
]
[0,3];

第9个操作后,队列为
[
3
]
[3]。

#include<bits/stdc++.h>
using namespace std;
priority_queue<int> a;
queue<int> x;
int main(){
	int n,p1,p2;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>p1;
		if(p1==1){
			cin>>p2;
			x.push(p2);
		}else if(p1==2){
			if(!a.empty()){
				cout<<a.top();
				a.pop();
			}else{
				cout<<x.front();
				a.pop();
			}  
		}else{
			while(!x.empty()){
				a.push(x.front());
				x.pop();
			}
		}   
	}
	return 0;
}
2 个赞

题目是排队?

1 个赞

所以我戳那了? :scream:

1 个赞

求调

1 个赞

这是从大到小排的,你要重置一下优先队列:

priority_queue<long long,vector<long long>,greater<long long> > a;
1 个赞

可以的话给个解决方案

1 个赞

懂了,谢谢!

1 个赞

必须开long long吗?

不知道,最好开

1 个赞

我还是绰了QWQ

#include<bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,greater<int> > a;
queue<int> x;
int main(){
	int n,p1,p2;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>p1;
		if(p1==1){
			cin>>p2;
			x.push(p2);
		}else if(p1==2){
			if(!a.empty()){
				cout<<a.top();
				a.pop();
			}else{
				cout<<x.front();
				a.pop();
			}  
		}else{
			while(!x.empty()){
				a.push(x.front());
				x.pop();
			}
		}   
	}
	return 0;
}

你试试我的代码

1 个赞
#include<bits/stdc++.h>
using namespace std;
priority_queue<long long,vector<long long>,greater<long long> > a;
queue<int> x;
int main(){
	long long n,p1,p2;
	cin>>n;
	for(long long i=1;i<=n;i++){
		cin>>p1;
		if(p1==1){
			cin>>p2;
			x.push(p2);
		}else if(p1==2){
			if(!a.empty()){
				cout<<a.top();
				a.pop();
			}else{
				cout<<x.front();
				x.pop();//我这打成a.pop()了,但好像还有问题
			}  
		}else{
			while(!x.empty()){
				a.push(x.front());
				x.pop();
			}
		}   
	}
	return 0;
}
1 个赞