【u119】中位数
Time Limit: 1 second
Memory Limit: 128 MB
【问题描述】
给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数。即前1,3,5,……个数的中位数。
【输入格式】
输入文件median.in的第1行为一个正整数N,表示了序列长度。 第2行包含N个非负整数A[i] (A[i] ≤ 10^9)。
【输出格式】
输出文件median.out包含(N + 1) / 2行,第i行为A[1], A[2], …, A[2i – 1]的中位数。
【数据规模】
对于20%的数据,N ≤ 100; 对于40%的数据,N ≤ 3000; 对于100%的数据,N ≤ 100000。
Sample Input1
7
1 3 5 7 9 11 6
Sample Output1
1
3
5 6 【题解】 维护一个大根堆和一个小根堆。 一开始把第一二元素加入到大根堆的第一个位置。然后直接输出第一个元素。 对于之后输入的元素。进行判断。如果大于大根堆的根,则放到小根堆。小于或等于则放到大根堆。 然后放完之后。进行一次判断。看一下这两个堆它们的差的绝对值是否会大于2.如果大于2的话。就把数字 多的那个堆中的根放到另外一个堆中。这样的判断也做完之后。就可以输出中位数了。比如当前i=3,则中 位数的位置就在2,先判断一下2是否等于大根堆的大小,如果是就输出大根堆的根,否则输出小根堆的根。 难点的话在堆的操作上。多熟悉就懂了。 【代码】#include <cstdio> int dagendui[100001],xiaogendui[100001],n,pos; //拼音分别对应了大根堆和小根堆 void up_tiaozheng(int a[100001],int p,int what) //往上调整。 what对应了a堆是什么堆
{ //0为大根堆 1为小根堆
int x = a[p]; //获取需要调整的数字
int i = p,j = p/2; //往上调整 所以是除2表示指向其父亲节点
//printf("p=%d\n",p);
while (j > 0) //如果没有越过树的范围
{
if (what ==0) //大根堆 的调整方法
{
if (x > a[j]) //如果这个值比父亲节点大。那么就不符合大根堆的定义了。
{ //往上走
a[i] = a[j];
i = j;
j = i/2;
}
else //否则就找到了一个合适的位置 直接结束即可。
break;
}
else //小根堆
{
if (x <a[j]) //如果父亲节点比儿子节点大。则不符合小根堆的定义。
{ //同样进行调整
a[i] = a[j];
i = j;
j = i/2;
}
else //如果找到合适的位置就直接结束调整
break;
}
}
a[i] = x;
pos = i; //要记录这个数字最后放到了哪里。
} void down_tiaozheng(int a[100001],int p,int what) //往下调整。
{
int x = a[p]; //获取需要调整的数字。
int i = p,j = p*2;
while (j <=a[0] )
{
if (what ==0) //大根堆
{
if (j < a[0] && a[j+1] > a[j]) //大根堆往下调整的话,儿子要找大的。
j++;
if (x < a[j])
{
a[i] = a[j];
i = j;
j = i*2;
}
else
break;
}
else //小根堆
{
if (j < a[0] && a[j+1] < a[j]) //小根堆要调整则要找小的。
j++;
if (x > a[j])
{
a[i] = a[j];
i = j;
j = i*2;
}
else
break;
}
}
a[i] = x;
} void input_data()
{
scanf("%d",&n);
scanf("%d",&dagendui[1]); //先把第一个元素放到大根堆的根节点。
dagendui[0] = 1;
printf("%d\n",dagendui[1]);
for (int i = 2;i <= n;i++)
{
int dd;
scanf("%d",&dd);//输入的数据和大根堆的根节点比较
if (dd > dagendui[1]) //根据比较的结果放到大根堆或小根堆。
{
xiaogendui[0]++;
xiaogendui[xiaogendui[0]]=dd;
up_tiaozheng(xiaogendui,xiaogendui[0],1); //放到末尾要先向上调整再向下调整
down_tiaozheng(xiaogendui,pos,1);
}
else
{
dagendui[0]++;
dagendui[dagendui[0]] = dd;
up_tiaozheng(dagendui,dagendui[0],0);
down_tiaozheng(dagendui,pos,0); //同理
}
if (dagendui[0] > xiaogendui[0]) //如果它们的大小之差的绝对值大于2则需要调整
{
if ((dagendui[0] - xiaogendui[0]) > 2) //大根堆数字比较多
{ //就把大根堆的根节点放到小根堆中去
xiaogendui[0]++;
xiaogendui[xiaogendui[0]] = dagendui[1];
up_tiaozheng(xiaogendui,xiaogendui[0],1);
down_tiaozheng(xiaogendui,pos,1);
dagendui[1] = dagendui[dagendui[0]];
dagendui[0]--;
down_tiaozheng(dagendui,1,0);
}
}
else
if (dagendui[0] < xiaogendui[0])
{
if ((xiaogendui[0]-dagendui[0]) > 2)
{ //如果小根堆中的数字更多。则把小根堆的根节点放到大根堆中去。
dagendui[0]++;
dagendui[dagendui[0]] = xiaogendui[1];
up_tiaozheng(dagendui,dagendui[0],0);
down_tiaozheng(dagendui,pos,0);
xiaogendui[1] = xiaogendui[xiaogendui[0]];
xiaogendui[0]--;
down_tiaozheng(xiaogendui,1,1);
}
}
if ( (i%2)==1) //如果是奇数 则输出大根堆的根节点或小根堆的根节点。
{
int tt = (i+1)/2;
if (tt ==dagendui[0]) //大根堆的大小和所需要输出的第tt个数字相同就可以直接输出根节点
printf("%d\n",dagendui[1]);
else //否则的话就是放在小根堆的根节点了。
printf("%d\n",xiaogendui[1]);
}
} } int main()
{
//freopen("F:\\rush.txt","r",stdin);
input_data();
return 0;
}
【u119】中位数的更多相关文章
- [LeetCode] Find Median from Data Stream 找出数据流的中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- [LeetCode] 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 ...
- BZOJ1303 [CQOI2009]中位数图
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 在MySQL中,如何计算一组数据的中位数?
要得到一组数据的中位数(例如某个地区或某家公司的收入中位数),我们首先要将这一任务细分为3个小任务: 将数据排序,并给每一行数据给出其在所有数据中的排名. 找出中位数的排名数字. 找出中间排名对应的值 ...
- AC日记——中位数 洛谷 P1168
题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数.[color=red]即[/color] ...
- [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)
题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...
- LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法,问题拓展 难度:1
思路:设现在可用区间在nums1是[s1,t1),nums2:[s2,t2) 1.当一个数组可用区间为0的时候,由于另一个数组是已经排过序的,所以直接可得 当要取的是最小值或最大值时,也直接可得 2. ...
- BZOJ 1303 CQOI2009 中位数图 水题
1303: [CQOI2009]中位数图 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2340 Solved: 1464[Submit][Statu ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
随机推荐
- bitmap-setBounds方法参数研究
对于如下的代码,一直有点不明白它具体每一步操作的影响.今天就稍微研究下.代码如下 xml代码 <RelativeLayout xmlns:android="http://schemas ...
- matlab 可视化 —— 高级 api(montage)、insertObjectAnnotation、insertMaker
1. montage:同时显示多个图像 thumbnails = train_x(:, :, :, 1:100); thumbnails = imresize(thumbnails, [64, 64] ...
- AVCaptureSession音频视频采集
// // AudioVideoCaptureViewController.m // live // // Created by lujunjie on 2016/10/31. // Copyrigh ...
- ORACLE10g R2【单实例 FS→单实例FS】
ORACLE10g R2[单实例FS→单实例FS] 本演示案例所用环境: primary standby OS Hostname pry std OS Version RHEL5.8 RHEL5. ...
- android之路Gallery 画廊
Gallery是一个内部元素能够水平滚动,而且能够把当前选择的子元素定位在它中心的布局组件. 我们还是直接看看样例的执行效果. watermark/2/text/aHR0cDovL2Jsb2cuY3N ...
- 使用 JS 关闭警告框及监听自定义事件(amaze ui)
使用 JS 关闭警告框及监听自定义事件(amaze ui) 一.总结 1.jquery匿名函数:第8行,jquery匿名函数,$(function(){});,有没有很简单,只是少了jquery的前面 ...
- Oracle批量插入在C#中的应用
public void SetUserReportResult(int[] reportId, bool isReceive, string result) { if (reportId == nul ...
- HttpClient请求发送的几种用法二:
public class HttpClientHelper { private static readonly HttpClientHelper _instance = new H ...
- 【UIL框架】Universal-Image-Loader全然解析(一)之介绍与使用具体解释
转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/50439814 本文出自:[江清清的博客] (一).前言: [好消息] ...
- ZOJ 2476 Total Amount 字符串
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1476 题目大意: 给你n串数字组成的字符串,要求输出他们相加的和. 如:n= ...