社区讨论

求调

P3378【模板】堆参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mirblu27
此快照首次捕获于
2025/12/04 18:56
3 个月前
此快照最后确认于
2025/12/06 17:25
2 个月前
查看原帖
为什么只有92aaa %>_<%
CPP
#include<iostream>
#include<queue> 
using namespace std;

const int MAXN=100005;
 
int n,op; 
  
struct priority{
	int w[MAXN],tot;
	int top(){
		return  w[1]; 
	} 
	void modify(int x){
		if(x==1||w[x]>=w[x/2]){
			return; 
		} 
		swap(w[x],w[x/2]); 
		modify(x/2); 
	} 
	void push(int x){
		w[++tot]=x;
		modify(tot); 
	} 
	void repair(int x){
		if(x*2>tot){
			return; 
		} 
		int tar=x*2;
		if(x*2+1<=tot&&w[x*2+1]<w[tar]){
			tar=x*2+1; 
		} 
		if(w[x]>w[tar]){
			swap(w[x],w[tar]); 
			repair(tar); 
		} 
	} 
	void pop(){
		swap(w[1],w[tot--]); 
		repair(1); 
	} 
} q; 

int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>op;
		if(op==1){
			int x;
			cin>>x;
			q.push(x); 
		} else if(op==2){
			cout<<q.top()<<endl; 
		} else{
			q.pop(); 
		} 
	} 
	return 0; 
}

回复

1 条回复,欢迎继续交流。

正在加载回复...