求调
查看原帖
求调
1528563
lyt_tcsn楼主2025/1/23 11:34
#include <bits/stdc++.h>
using namespace std;
class Heap {
public:
	int len;
	int a[100005];
	Heap() {}
	void up(int x) {
		if (x == 1 || a[x] > a[x / 2]) return;
		swap(a[x], a[x / 2]);
		up(x / 2);
	}
	void down(int x) {
		if (x * 2 > len) return;
		int t = x * 2;
		if (x * 2 + 1 <= len)
			t = a[x * 2] < a[x * 2 + 1] ? x * 2 : x * 2 + 1;
		if (a[x] > a[t]) {
			swap(a[x], a[t]);
			down(t);
		}
	}
	bool empty() {
		return len == 0;
	}
	void add(int x) {
		a[++len] = x;
		up(len);
	}
	int top() {
		return a[1];
	}
	void pop() {
		swap(a[1], a[len--]);
		down(1);
	}
};
int main() {
	Heap h;
	int n, op;
	scanf("%d", &n);
	while (n--) {
		scanf("%d", &op);
		switch (op) {
		case 1:
			int x;
			scanf("%d", &x);
			h.add(x);
			break;
		case 2:
			printf("%d\n", h.top());
			break;
		case 3:
			h.pop();
		}
	}
	return 0;
}
2025/1/23 11:34
加载中...