POJ 3784.Running Median
2015-07-16
问题简述:
动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数。
原题链接:http://poj.org/problem?id=3784
解题思路:
求取中位数的方法常常想到使用堆来实现:取一个大顶堆,一个小顶堆,使大顶堆的堆顶记录中位数,因此,要时刻保持大顶堆堆顶元素小于小顶堆堆顶元素,且大顶堆元素个数等于小顶堆元素个数或等于小顶堆元素个数加一。
以下有两种堆得实现方法:
一:直接使用STL中的函数(make_heap,push_heap,pop_heap,sort_heap)实现堆;
二:使用优先队列(priority_queue)实现堆;
方法一源码:
/*
OJ: POJ
ID: 3013216109
TASK: 3784.Running Median
LANG: C++
NOTE: 堆(STL)
*/
#include <cstdio>
#include <algorithm>
using namespace std; const int MAX=;
int a[MAX],b[MAX],c[MAX]; bool cmp(int a,int b) {
return a>b;
} int main()
{
int t,n,m,x;
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&m);
int a_len=,b_len=,k=,i=;
while(m--) {
i++;
scanf("%d",&x);
if(a_len==) {
a[]=x;
c[k++]=x;
a_len++;
continue;
} if(x<=a[]) {
a[a_len++]=x;
push_heap(a,a+a_len);
}
else {
b[b_len++]=x;
push_heap(b,b+b_len,cmp);
} while(a_len>b_len+) {
b[b_len++]=a[];
pop_heap(a,a+a_len);
a_len--;
push_heap(b,b+b_len,cmp);
}
while(a_len<b_len) {
a[a_len++]=b[];
pop_heap(b,b+b_len,cmp);
b_len--;
push_heap(a,a+a_len);
} if(i%==)
c[k++]=a[];
} printf("%d %d\n",n,k);
for(int i=;i<k;i++) {
if(i>&&i%==) putchar('\n');
if(i%) putchar(' ');
printf("%d", c[i]);
}
printf("\n");
}
return ;
}
方法二源码:
/*
OJ: POJ
ID: 3013216109
TASK: 3784.Running Median
LANG: C++
NOTE: 堆(优先队列)
*/
#include <cstdio>
#include <queue>
#define MAX 10005
using namespace std; priority_queue<int,vector<int>,less<int> > a; //大顶堆
priority_queue<int,vector<int>,greater<int> > b; //小顶堆
vector<int> c; int main()
{
int t,n,m,x;
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&m);
while(!a.empty()) a.pop();
while(!b.empty()) b.pop();
c.clear();
for(int i=;i<m;i++) {
scanf("%d",&x);
if(a.empty()) {
a.push(x);
c.push_back(x);
continue;
}
if(x<=a.top())
a.push(x);
else
b.push(x); while(a.size()>b.size()+) {
b.push(a.top());
a.pop();
}
while(a.size()<b.size()) {
a.push(b.top());
b.pop();
} if(i%==&&i!=)
c.push_back(a.top());
} printf("%d %d\n",n,(m+)/);
for(int i=;i<c.size();i++) {
if(i>&&i%==) putchar('\n');
if(i%) putchar(' ');
printf("%d", c[i]);
}
printf("\n");
}
return ;
}
POJ 3784.Running Median的更多相关文章
- 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 ...
- POJ 3784 Running Median (动态中位数)
题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...
- POJ 3784 Running Median (模拟水过带翻译)
Description Moscow is hosting a major international conference, which is attended by n scientists fr ...
- 【POJ 3784】 Running Median (对顶堆)
Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...
- hdu 3282 Running Median
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...
- HDU 3282 Running Median 动态中位数,可惜数据范围太小
Running Median Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- 【POJ3784】Running Median
Running Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3406 Accepted: 1576 De ...
- 【POJ 3784】 Running Median
[题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆 ...
随机推荐
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- C#中经常使用的几种读取XML文件的方法
XML文件是一种经常使用的文件格式,比如WinForm里面的app.config以及Web程序中的web.config文件,还有很多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖 ...
- C++面试题一大波
//打印1到最大的n位数. //题目:输入数字n.按顺序打印出从1到最大的n位十进制数.比方: //输入3.则打印出1.2.3一直到最大的3位数999. //[陷阱]:这个题目非常easy想到的办法就 ...
- Android Afinal框架学习(二) FinalActivity 一个IOC框架
框架地址:https://github.com/yangfuhai/afinal 相应的源代码: net.tsz.afinal.annotation.view.* FinalActivity Fina ...
- go-vim配置
一.环境准备: 系统环境说明: [root@docker golang]# cat /etc/redhat-release CentOS Linux release (Core) [root@dock ...
- Linux下文件及目录的一些操作(附递归遍历目录源码)
1.获取当前工作目录 #include <unistd.h> 1.char *getcwd(char *buf,size_t size); 2. 3.其中,buf为缓冲区地址,size为给 ...
- C++_前置声明
为什么要有前置声明? eg: -定义一个类 class A,这个类里面使用了类B的对象b,然后定义了一个类B,里面也包含了一个类A的对象a,就成了这样: //a.h #include "b. ...
- linux杂记(十二?) 关于账号和密码的二三事
关于密码的二三事 关于账号和密码的二三事 久了不更linux的相关知识,实在是懒得想内容点(纯粹是懒).那么今天就来谈谈关于linux密码和账号的重要概念. 假如你的主机遭到入侵,那么对方的第一个侵入 ...
- System类基础
取时间差: public class SystemDemo01 { public static void main(String[] args) { long startTim ...
- junit的安装和使用
一.junit的安装: junit-4.11.jar: http://www.java2s.com/Code/Jar/j/Downloadjunit411jar.htm hamcrest-core.j ...