题目大意

给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[3], …, A[2k - 1]的中位数。即前1,3,5,……个数的中位数。

题解

要找到中位数我们需要的序列是单调不减的,故可以用二叉平衡树解决。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAX_NODE = 100010; struct SplayTree
{
private:
struct Node
{
Node *LeftSon, *RightSon, *Father;
int Key, Size, Count; Node(Node *fa, int key) : Father(fa), LeftSon(NULL), RightSon(NULL), Key(key), Size(1), Count(1){} bool IsLeftSon()
{
return Father->LeftSon == this;
} void Refresh()
{
Size = (LeftSon ? LeftSon->Size : 0) + (RightSon ? RightSon->Size : 0) + Count;
} bool IsRoot()
{
return Father == NULL || (Father->LeftSon != this && Father->RightSon != this);
}
}*Root; void Rotate(Node *cur)
{
Node *gfa = cur->Father->Father;
Node **gfaSon = gfa ? (cur->Father->IsLeftSon() ? &gfa->LeftSon : &gfa->RightSon) : &Root;
Node **faSon = cur->IsLeftSon() ? &cur->Father->LeftSon : &cur->Father->RightSon;
Node **curSon = cur->IsLeftSon() ? &cur->RightSon : &cur->LeftSon;
*faSon = *curSon;
if (*faSon)
(*faSon)->Father = cur->Father;
*curSon = cur->Father;
(*curSon)->Father = cur;
*gfaSon = cur;
(*gfaSon)->Father = gfa;
(*curSon)->Refresh();
cur->Refresh();
} void PushDown() {} void Splay(Node *cur)
{
PushDown();
while (cur->Father)
{
if (!cur->Father->IsRoot())
Rotate(cur->Father->IsLeftSon() == cur->IsLeftSon() ? cur->Father : cur);
Rotate(cur);
}
} int GetKeyByRank(Node *cur, int rank)
{
int rootSize, leftSize = (cur->LeftSon ? cur->LeftSon->Size : 0);
if (rank <= leftSize)
return GetKeyByRank(cur->LeftSon, rank);
else if (rank <= (rootSize = leftSize + cur->Count))
return cur->Key;
else
return GetKeyByRank(cur->RightSon, rank - rootSize);
} public:
void Insert(int key)
{
Node **cur = &Root, *fa = NULL;
while (*cur)
{
fa = *cur;
if (key == (*cur)->Key)
{
(*cur)->Count++;
Splay(*cur);
return;
}
else if (key < (*cur)->Key)
cur = &(*cur)->LeftSon;
else if (key > (*cur)->Key)
cur = &(*cur)->RightSon;
}
*cur = new Node(fa, key);
Splay(*cur);
} int GetKeyByRank(int rank)
{
return GetKeyByRank(Root, rank);
}
}g; int main()
{
static int A[MAX_NODE];
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", A + i);
for (int i = 1; i <= n; i += 2)
{
g.Insert(A[i]);
printf("%d\n", g.GetKeyByRank(i / 2 + 1));
g.Insert(A[i + 1]);
}
return 0;
}

luogu1168 中位数的更多相关文章

  1. [luogu1168]中位数_优先队列

    中位数 题目大意:输出读入的前2*k+1个数的中位数.一共有n个数,按照读入顺序. 注释:$1\le n \le 10^9$. 想法:这是优先队列的一个应用qwq.我们弄两个堆.小根堆和大根堆,保证: ...

  2. [Luogu]中位数

    Description Luogu1168 Solution 一种神奇的做法:开一个大根堆和小根堆,保证大根堆比小根堆多1个元素,且大根堆堆顶元素比小根堆堆顶元素小,那么大根堆堆顶就是中位数.插入的时 ...

  3. [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 ...

  4. [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 ...

  5. BZOJ1303 [CQOI2009]中位数图

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  6. 在MySQL中,如何计算一组数据的中位数?

    要得到一组数据的中位数(例如某个地区或某家公司的收入中位数),我们首先要将这一任务细分为3个小任务: 将数据排序,并给每一行数据给出其在所有数据中的排名. 找出中位数的排名数字. 找出中间排名对应的值 ...

  7. AC日记——中位数 洛谷 P1168

    题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数.[color=red]即[/color] ...

  8. [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)

    题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...

  9. LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法,问题拓展 难度:1

    思路:设现在可用区间在nums1是[s1,t1),nums2:[s2,t2) 1.当一个数组可用区间为0的时候,由于另一个数组是已经排过序的,所以直接可得 当要取的是最小值或最大值时,也直接可得 2. ...

随机推荐

  1. G - Wrong Subtraction

    Problem description Little girl Tanya is learning how to decrease a number by one, but she does it w ...

  2. org.apache.catalina.core.StandardContext startInternal SEVERE: Error listenerStart

    问题:文件明明存在,资源找不到,报错 解决方法:原因是没有build path,这有点像.net里边的build .点击相应的文件夹选择build path ,解决问题

  3. File入门及路径名问题

    package com.io.file; import java.io.File; /** * @author 王恒 * @datetime 2017年4月20日 下午2:53:29 * @descr ...

  4. js-事件处理(重点)

    1:各种常用事件: 2:简单用法: <body onLoad="javascript:alert('hello world');" onUnload="javasc ...

  5. java Web(2)

    Servlet与web容器的配合: 1)客户端向Web服务器发起一个HTTP请求. 2)HTTP请求被Web服务器接受,如果请求的是静态页面,则由Web服务器负责处理.如果请求的是Java Web组件 ...

  6. Windows 10 新功能

    一.与 Cortana 集成的便笺 借助便笺,你可捕捉并保存绝妙创意或记录重要细节.便笺现已与 Cortana 集成,让你能够设置整个设备中的提醒. (一)   先来了解一下微软小娜Cortana. ...

  7. C# 获得剪贴板内容和 richTextBox部分文本设置颜色

    try { MemoryStream vMemoryStream = iData.GetData("Html Format") as MemoryStream; if (vMemo ...

  8. matlab学习GUI可调的界面窗口

    创建一个GUI界面,在此依然利用GUI_01的窗口来演示 发现它的最大化窗口不可调 在GUI绘制中,工具--->选择GUI选项---->选择第二个成比例 再运行就可以调控大小了

  9. 【airtest, python】报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')),解决方法如下

    环境及设备 mac, xcode , iphonex 问题 最近出现一个让人费解的问题,airtest 没跑多长时间,服务就断掉,而且总是报“requests.exceptions.Connectio ...

  10. 在vue中,让表格td下的textraea自适应高度

    1.效果图 2.数据是动态获取的,因此存在一个异步的问题,解决的思路是数据获取到渲染在textarea中以后,获取文字的真实高度,然后把这个高度给textarea 3.具体代码以及步骤 (1)再cre ...