【POJ3784】Running Median
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3406 | Accepted: 1576 |
Description
Input
Output
Sample Input
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
Sample Output
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
解析:
动态维护中位数
方法:
建立两个二叉堆:一个小根堆,一个大根堆。在依次读入这个整数序列的过程中,设当前序列长度为M,我们始终保持:
1、序列中从小到大排名为1~M/2的整数存储在大根堆中:
2、序列中从小到大排名为M/2+1~M的整数存储在小根堆中。
任何时候,如果某一个堆中的元素过多,打破了这个性质,就取出该堆的堆顶插入另一个堆。这样一来,序列的中位数就是小根堆的堆顶。
每次新读入一个数值X后,若X比中位数小,则插入大根堆,否则插入小根堆,在插入之后检查并维护上述性质即可。这就是“对顶堆”算法。
(本题对格式要求严格)
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int T,n,m,a[]; priority_queue<int,vector<int>, greater<int> > q;//从小到大输出:小顶堆 priority_queue<int> p;//从大到小输出 :大顶堆 int main()
{
scanf("%d",&T);
while(T--)
{
while(!q.empty())q.pop();
while(!p.empty())p.pop();
scanf("%d%d",&m,&n);
printf("%d %d\n",m,(n+)/);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
q.push(a[]);
printf("%d",a[]);
int cnt=;
for(int i=;i<=n;i++)
{
if(a[i]>q.top()) q.push(a[i]);
else p.push(a[i]);
if(i%!=){
while(p.size()>(i/))
{
q.push(p.top());
p.pop();
}
while(q.size()>(i-(i/)))
{
p.push(q.top());
q.pop();
}
cnt++;
if(cnt%==) printf("\n%d",q.top());
else printf(" %d",q.top());
}
}
puts("");//换行坑人......
}
}
【POJ3784】Running Median的更多相关文章
- 【POJ 3784】 Running Median (对顶堆)
Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...
- 【POJ 3784】 Running Median
[题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆 ...
- 【LeetCode】4. Median of Two Sorted Arrays(思维)
[题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...
- 【PAT】1029. Median (25)
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- 【leetcode】4. Median of Two Sorted Arrays
题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...
- 【LeeetCode】4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 【AtCoder】【DP】【思维】Prefix Median(AGC012)
模的是这位神犇的代码:Atcoder AGC012F : Prefix Median 题意: 在动态中位数那道题上做了一些改动.给你一个序列a,可以将a重新任意排序,然后对于a序列构造出b序列. 假设 ...
- 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
随机推荐
- [C6] Andrew Ng - Convolutional Neural Networks
About this Course This course will teach you how to build convolutional neural networks and apply it ...
- 简化wcf的service设置
使用wcf作为c/s的通信桥梁,就免不了要写wcf service,更免不了写一些配置. 但是在把系统中所有的.net remoting service都转换成wcf service以后,发现居然有2 ...
- A1089 Insert or Merge (25 分)
一.技术总结 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...
- 修改gradle中央仓库,加快jar包下载速度
打开gradle项目的build.gradle文件 找到repositories,注释掉mavenCentral(),使用阿里云的仓库地址 repositories { //mavenCentral( ...
- golang基础之初识
golang 简介 很久以前,有一个IT公司,这公司有个传统,允许员工拥有20%自由时间来开发实验性项目.在2007的某一天,公司的几个大牛,正在用c++开发一些比较繁琐但是核心的工作,主要包括庞大的 ...
- LeetCode 733: 图像渲染 flood-fill
题目: 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间. An image is represented by a 2-D array of int ...
- pytest 常见用法
前言 之前一篇文章简单介绍了 pytest 以及 fixture :https://www.cnblogs.com/shenh/p/11572657.html .实际在写自动化测试脚本中,还会有一些很 ...
- ICP&TPS:最近邻
经过了一段时间的研bai究gei...终于可以偷得几天闲了. 这里来补个档. 无论是ICP还是TPS,缺乏锚点的前提下.你总是要通过找另一个曲面的最近的点来实现你的work beimat:点数*3,f ...
- 分布式 master/slave 框架
https://helix.apache.org/ https://stackoverflow.com/questions/16401412/apache-helix-vs-yarn https:// ...
- tsconfig.json配置项详解
{ "compilerOptions": { "allowUnreachableCode": true, // 不报告执行不到的代码错误. "allo ...