黑盒子POJ1442

黑盒子POJ1442

思路

题干在这:POJ1442
对顶堆,讨论讨论即可

ac代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<cstring>
#include<vector>
#define ll long long
#define N 30010
using namespace std;
struct lef {
	ll val;
	friend bool operator <(const lef& a, const lef& b) {
		return a.val < b.val;
	}
};
struct rig {
	ll val;
	friend bool operator <(const rig& a, const rig& b) {
		return a.val > b.val;
	}
};
priority_queue<lef> low;
priority_queue<rig> high;
ll a[N], u[N];
int main() {
	int m, n;
	cin >> m >> n;
	for (int i = 1; i <= m; i++)
		scanf_s("%lld", a + i);
	for (int i = 1; i <= n; i++)
		scanf_s("%lld", u + i);
	int p = 1;
	int i = 1;
	while (1) {
		if (u[p] > u[p - 1]) {
			while (i <= u[p]) {
				if ((int)low.size() < p) {
					if (high.size() == 0 || a[i] <= high.top().val) {
						lef x = { a[i] };
						low.push(x);
					}
					else {
						ll s = high.top().val;
						lef mid = { s };
						high.pop();
						low.push(mid);
						rig x = { a[i] };
						high.push(x);
					}
				}
				else if (a[i] < low.top().val) {
					ll s = low.top().val;
					rig mid = { s };
					low.pop();
					high.push(mid);
					lef x = { a[i] };
					low.push(x);
				}
				else {
					rig x = { a[i] };
					high.push(x);
				}
				i++;
			}
		}
		else {
			ll s = high.top().val;
			lef mid = { s };
			high.pop();
			low.push(mid);
		}
		printf("%lld\n", low.top().val);
		if (p == n)
			break;
		p++;
	}
}
发布了27 篇原创文章 · 获赞 1 · 访问量 480

猜你喜欢

转载自blog.csdn.net/qq_45616764/article/details/104277311