这样能过?求问
查看原帖
这样能过?求问
481621
Zhang_Wenjie楼主2024/9/18 14:01

我这样做的对顶堆是不是有 O(nm)O(nm) 了,还是说从每个元素被调用的次数的角度算?请教

(要上课了,有点慌乱~)

#include <bits/stdc++.h>
#define re register int 

using namespace std;
const int N = 2e5 + 10;

int n, q, add[N], check[N];

priority_queue<int> a;
priority_queue<int, vector<int>, greater<int> > b;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	cin >> n >> q;
	for (re i = 1; i <= n; i ++) cin >> add[i];
	for (re i = 1; i <= q; i ++) 
	{
		int x; cin >> x;
		check[x] ++;
	}
	
	int k = 0;
	for (re i = 1; i <= n; i ++)
	{
		int x = add[i];
		if (a.empty() || x <= a.top()) a.push(x);
		else b.push(x);
		
		while (check[i])
		{
			check[i] --; k ++;
			while (a.size() > k) b.push(a.top()), a.pop();
			while (a.size() < k) a.push(b.top()), b.pop();
			cout << a.top() << '\n';
//			cout << "after add " << i << ' ' << k << '\n';
		}	
	}
	
	return 0;
}
2024/9/18 14:01
加载中...