NC50940 Running Median
题目
题目描述
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of the elements received so far.
输入描述
The first line of input contains a single integer \(P(1 \leq P \leq 1000)\) , which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by an odd decimal integer \(M (1 \leq M \leq 9999)\) , giving the total number of signed integers to be processed. The remaining line(s) in the dataset consists of the values, 10 per line, separated by a single space. The last line in the dataset may contain less than 10 values.
输出描述
For each data set the first line of output contains the data set number, a single space and the number of medians output (which should be one-half the number of input values plus one). The output medians will be on the following lines, 10 per line separated by a single space. The last line may have less than 10 elements, but at least 1 element. There should be no blank lines in the output.
示例1
输入
3
1 9
1 2 3 4 5 6 7 8 9
2 9
9 8 7 6 5 4 3 2 1
3 23
23 41 13 22 -3 24 -31 -11 -8 -7
3 5 103 211 -311 -45 -67 -73 -81 -99
-33 24 56
输出
1 5
1 2 3 4 5
2 5
9 8 7 6 5
3 12
23 23 22 22 13 3 5 5 3 -3
-7 -3
题解
知识点:优先队列。
用两个优先队列维护中位数左边和右边的序列,因为中位数一定在序列的中间位置,左边比他大,右边比他小且数量几乎一样(相差不超过 \(1\) )。因此左边维护大顶堆,右边维护小顶堆,每次存入数据后比较两序列的大小,那边比那边大超过 \(1\) 就把队头转移直至平衡,就可以维护一个动态中位数了。
时间复杂度 \(O(n \log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool solve() {
int p, m;
cin >> p >> m;
cout << p << ' ' << (m + 1) / 2 << '\n';
priority_queue<int> pq1;
priority_queue<int, vector<int>, greater<int>> pq2;
int tmp;
cin >> tmp;
pq1.push(tmp);
cout << pq1.top() << ' ';
for (int i = 2;i <= m;i++) {
cin >> tmp;
if (tmp <= pq1.top()) pq1.push(tmp);
else pq2.push(tmp);
if (pq1.size() > 1 + pq2.size()) {
pq2.push(pq1.top());
pq1.pop();
}
else if (pq1.size() + 1 < pq2.size()) {///注意,不要size相减,会ULL溢出,-1变最大值
pq1.push(pq2.top());
pq2.pop();
}
if (i & 1) cout << (pq1.size() > pq2.size() ? pq1.top() : pq2.top()) << ' ';
if (!(i % 20)) cout << '\n';
}
cout << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
NC50940 Running Median的更多相关文章
- hdu 3282 Running Median
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...
- POJ 3784.Running Median
2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...
- HDU 3282 Running Median 动态中位数,可惜数据范围太小
Running Median Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- 【POJ 3784】 Running Median (对顶堆)
Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...
- 【POJ3784】Running Median
Running Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3406 Accepted: 1576 De ...
- POJ3784 Running Median
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1670 Accepted: 823 Description For th ...
- Running Median POJ - 3784 (对顶堆/优先队列 | 链表)
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...
- POJ 3784 Running Median(动态维护中位数)
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- POJ 3784 Running Median【维护动态中位数】
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- POJ3784:Running Median
浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=3784 用一个"对顶堆& ...
随机推荐
- MySQL高可用搭建方案之(MHA)
有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 首发博客地址 原文地址 MHA架构介绍 MHA是Master High Av ...
- [转帖]RHEL/CentOS 7中的网络暨network.service与NetworkManager.service详解
在RHEL/CentOS 6及以前的版本中,网络功能是通过一系列网络相关的脚本文件实现,如/etc/init.d/network文件,及如下/sbin/if*文件等. [root@myserver ~ ...
- [转帖]KVM调整磁盘大小
https://www.jianshu.com/p/5ca598424eb9 一台win10的虚拟机磁盘空间不足了,需要调整磁盘的大小.上网搜索KVM调整磁盘大小,结果得出的博客都说只有raw格式的能 ...
- [转帖]事务上的等待事件 —— enq: TX - contention
TX锁是保护事务的,事务结束时便会释放.因此,为获得TX锁为等待的会话,要等到拥有锁的会话的事务结束为止. SQL> select name,parameter1,parameter2,para ...
- [转帖]《Linux性能优化实战》笔记(二)—— CPU 上下文切换(上)
上一篇的最后一个例子,在多个进程竞争CPU时,我们看到每个进程实际上%usr部分只有20%多,70%多是在wait,但是load远远高于单个进程使用CPU达到100%. 这让我想到之前看的RWP公开课 ...
- Nginx与Tomcat作为前端服务器的性能比较
Nginx与Tomcat作为前端服务器的性能比较 摘要 最近总遇到使用tomcat还是使用nginx进行前端文件访问的争论 想着出差周末在酒店, 可以自己进行一下简单的测试. 希望能够对未来的工作进行 ...
- [转载]Linux常用的可插拔认证模块(PAM)pam_limits.so、pam_rootok.so和pam_userdb.so的详解
Linux常用的可插拔认证模块(PAM)pam_limits.so.pam_rootok.so和pam_userdb.so的详解 https://blog.51cto.com/udb1680/1846 ...
- awk的简要使用
原文地址:https://www.lujun9972.win/blog/2020/08/23/在命令行进行简单的统计分析/index.html 目录 使用awk获取最小值.最大值.中位数和平均值 使用 ...
- Ant Design Vue中Table对齐方式显示省略号
Ant Design Vue中Table对齐方式显示省略号 <template> <!-- bordered 表示表格中的边框 pagination="false" ...
- Windows10磁盘占用100%和内存占用高
前言 公司配备了两台电脑,两台电脑都是安装的win10系统,一台是磁盘占用高,另一台是内存可用低. 具体情况如下: 一台外网机 8g内存,安装win10 专业版,开机一天后经常出现内存不够用,但其实都 ...