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;
}