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 用一个"对顶堆& ...
随机推荐
- MongoDB 增删改查 常用sql总结
本文为博主原创,转载请注明出处: 1.切换到指定数据库:如果不存在则创建 use database 2.查看所有文档 show tables show collections 3.创建表 #创建文档 ...
- spring--JDK动态代理和CGLIB代理的区别
JDK 动态代理和 CGLIB 代理是 Java 中常用的两种动态代理实现方式,它们各有特点和适用场景: JDK 动态代理: JDK 动态代理是基于接口的代理方式,它使用 Java 反射机制来创建代理 ...
- Scan Synthesis Review
Review scan replacement - 将normal DFF替换为mux gate DFF scan stitching - 将DFF连接起来 scan的作用:将测试困难的时序逻辑转变为 ...
- DataGrip连接MySql数据库失败:dataGrip java.net.ConnectException: Connection refused: connect.
1.问题 报错:dataGrip java.net.ConnectException: Connection refused: connect. 详细错误:[08S01] Communications ...
- 永久激活Windows10专业版及其它版本【亲测有效2022-03-16,两种方式】
前言 小编最近使用公司的电脑,开始出现让你激活的提示,而且每次开机都会弹出来,小编忍住了:但是过不了半个月开始给你在桌面上显示了,一直显示和水印一样.小编忍无可忍,必须找个办法给你消灭掉!!,于是小编 ...
- [转帖]【数据库架构】NewSQL和PGXC
数据库架构风格 数据库的基本架构 分库分表方案 分布式事务和跨节点查询 PGXC NewSQL:原生分布式数据库 总结 数据库的两种架构风格:NewSQL(代表Google Spanner).Prxo ...
- MongoDB的安装使用与监控
MongoDB的安装使用与监控 下载 https://www.mongodb.com/try/download/community 我这边习惯于下载 Windows 的 MSI 进行安装 Linux ...
- [转帖]time_zone 是怎么打爆你的MySQL的
https://plantegg.github.io/2023/10/03/time_zone%E6%98%AF%E6%80%8E%E4%B9%88%E6%89%93%E7%88%86%E4%BD%A ...
- iftop的学习与使用
iftop的学习与使用 背景 前段时间一直进行netperf 等网络性能验证工具的学习与使用. 监控很多时候采用了 node-exporter + prometheus + grafana来进行观察 ...
- [转帖]Linux fuse用户态文件系统及其libfuse
https://www.jianshu.com/p/abc5524ac18c 为什么要有用户态文件系统 VFS文件系统可知文件系统在内核态的,应用程序操作文件,统一调用内核态的VFS层抽象接口. 突然 ...