2016 UESTC Training for Data Structures I - 郭大侠与线上游戏 CDOJ 1339 pb_ds黑科技

本文介绍了一种使用C++ pb_ds库解决在线游戏中队列中位数问题的方法,通过Tree-Like Container实现快速查询队列中位数,同时讨论了在不使用特定库时的备选方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I - 郭大侠与线上游戏

就是给你一个队列,支持队尾插入和队头弹出,或者询问当前队列里的数的中位数

别人跟我讲stl黑科技能过,然后我就套pb_ds,就过了

这个pb_ds只在linux下可以,但是评测姬一般都是linux下的,所以都能用,本地用就用mingw,然后我用的是Tree-Like Container,可以查询区间第k大,然后k就取中位数就好了

 

如果不用这个黑科技的话,可以用两个set/优先队列/堆来维护,具体的做法可以百度 堆求中位数


代码:

#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define maxn 1000005
typedef tree<string, null_type, less<string>, rb_tree_tag> Tree;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> set_t;
int a[maxn];
int size,pos;
int main() {
	set_t s;
	int n;
	scanf("%d", &n);
	int op, x, k;
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &op);
		if (op == 1)
		{
			scanf("%d", &x);
			a[size++] = x;
			s.insert(x);
		}
		else if (op == 2)
		{
			s.erase(a[pos]);
			++pos;
		}
		else if (op == 3)
		{
			k = (size - pos) / 2;
			printf("%d\n", *s.find_by_order(k));
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值