第十一个点TLE,求助
查看原帖
第十一个点TLE,求助
609811
accccccc楼主2022/8/10 13:43
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <string>
#define ll long long
using namespace std;
#define INF 0x3f3f3f3f
#define pii pair<int,int>
const int N = 1e6 + 10; 
int h[N], cnt;
int n;
void down(int u) {
	int t = u;
	//与左子节点比较 
	if(u * 2 <= cnt && h[u * 2] < h[t])
		t = u * 2;
	//与右子节点比较 
	if(u * 2 + 1 <= cnt && h[u * 2 + 1] < h[t])
		t = u * 2 + 1;
	if(t != u) {
		swap(h[t], h[u]);
		down(t);
	}
} 
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	while(n--) {
		int op;
		cin >> op;
		if(op == 1) { //往堆里插入一个数 
			int x;
			cin >> x;
			h[++cnt] = x;
			for(int i = cnt / 2; i; --i) down(i); 
		} else if(op == 2) { //输出数列中最小的数
			cout << h[1] << "\n";
		} else { //删除数列中最小的数 
			h[1] = h[cnt--];
			down(1);
		}
	}
	return 0;
}
2022/8/10 13:43
加载中...