黑盒子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++;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值