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 用一个"对顶堆& ...
随机推荐
- HTTP 及 http 请求解析过程
本文为博主原创,未经允许不得转载: HTTP 全称为:超文本传输协议(HyperText Transfer Protocol,HTTP),一种无状态的,以请求/应答方式运行的协议, 它使用可扩展的语义 ...
- 【TouchGFX】visual studio 工程中 SIMULATOR 宏定义位置
- [转帖]Linux中的Page cache和Buffer cache详解
1.内存情况 在讲解Linux内存管理时已经提到,当你在Linux下频繁存取文件后,即使系统上没有运行许多程序,也会占用大量的物理内存.这是因为当你读写文件的时候,Linux内核为了提高读写的性能和速 ...
- [转帖]关于redis,你需要了解的几点!
github:https://github.com/windwant 博客园 首页 新随笔 联系 订阅 管理 随笔 - 227 文章 - 4 评论 - 36 阅读 - 73万 一.关于 re ...
- [转帖]shell脚本变量详解(自定义变量、环境变量、变量赋值、变量运算、变量内容替换)
https://developer.aliyun.com/article/885658 简介: shell变量 shell变量是指用一个特定的字符串去表示不固定的内容 1.变量的类型 1.1自定义变量 ...
- vue3封装搜索表单组件
seacrch 表单完成的功能 1.根据配置json配置项自动生成表单 ok 2.是响应式的排版 ok 3.点击搜索按钮会向上抛出值 ok 4.点击重置按钮会自动清空数据,不需要父组件额外的处理 ok ...
- 【JS 逆向百例】猿人学系列 web 比赛第五题:js 混淆 - 乱码增强,详细剖析
逆向目标 猿人学 - 反混淆刷题平台 Web 第五题:js 混淆,乱码增强 目标:抓取全部 5 页直播间热度,计算前 5 名直播间热度的加和 主页:https://match.yuanrenxue.c ...
- Gorm 关联关系介绍与基本使用
目录 一 Belongs To(一对一) 1.1 Belongs To 1.2 重写外键 1.3 重写引用(一般不用) 1.4 Belongs to 的 CRUD 1.5 预加载 1.6 外键约束 二 ...
- P7031 [NWRRC2016] Anniversary Cake
题目简述 有一块 \(n \times m\) 的长方形蛋糕.蛋糕上有两个蜡烛,分别用 \((x_1,y_1)\) 和 \((x_2,y_2)\) 表示.现在有一把刀要把蛋糕切成两半,请问切入的终点和 ...
- 【一】AI Studio 项目详解【(一)VisualDL工具、环境使用说明、脚本任务、图形化任务、在线部署及预测】PARL
相关文章 [一]-环境配置+python入门教学 [二]-Parl基础命令 [三]-Notebook.&pdb.ipdb 调试 [四]-强化学习入门简介 [五]-Sarsa&Qlear ...