Files
python/TangDou/AcWing_TiGao/T6/PaiXu/106.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

44 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
int T;
int main() {
// 加快读入
ios::sync_with_stdio(false), cin.tie(0);
cin >> T;
while (T--) {
int n, m;
cin >> m >> n;
// 对于每个数据集,第一行输出两个整数,分别代表数据集的编号以及
// 输出中位数的个数(应为数据个数加一的二分之一),数据之间用空格隔开。
printf("%d %d\n", m, (n + 1) / 2);
priority_queue<int, vector<int>, greater<int>> up; // 小顶堆
priority_queue<int> down; // 默认大顶堆
// 对顶堆
int cnt = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (!up.size() || x >= up.top())
up.push(x);
else
down.push(x);
if (up.size() > down.size() + 1) down.push(up.top()), up.pop();
if (down.size() > up.size()) up.push(down.top()), down.pop();
// 奇数才输出
if (i & 1) {
printf("%d ", up.top());
if (++cnt % 10 == 0) puts(""); // 题目要求十个一换行
}
}
// 最后不足10个,也需要输出一个的换行
if (cnt % 10) puts("");
}
return 0;
}