luogu1168 中位数
题目大意
给出一个长度为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 中位数的更多相关文章
- [luogu1168]中位数_优先队列
中位数 题目大意:输出读入的前2*k+1个数的中位数.一共有n个数,按照读入顺序. 注释:$1\le n \le 10^9$. 想法:这是优先队列的一个应用qwq.我们弄两个堆.小根堆和大根堆,保证: ...
- [Luogu]中位数
Description Luogu1168 Solution 一种神奇的做法:开一个大根堆和小根堆,保证大根堆比小根堆多1个元素,且大根堆堆顶元素比小根堆堆顶元素小,那么大根堆堆顶就是中位数.插入的时 ...
- [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. ...
随机推荐
- js基础---数组方法
数组数据的排序及去重 sort无形参的排序方式 arr1=[2,12,3,15]; var a=arr1.sort();console.log(arr1);console.log(a);//排序会改变 ...
- SAP computer之RAM
RAM The RAM is a 16 X 8 static TTL RAM. We can program the RAM by means of the address and data swit ...
- Assembly之instruction之MOV
MOV[.W] Move source to destinationMOV.B Move source to destination Syntax MOV src,dst or M ...
- SQL Server对数据进行修改
SQL Server对数据进行修改,修改数据库中的数据. auto"> <tr style="background:red"> <td>编号 ...
- VS2013配置编译Caffe-Win10_X64
原文链接:http://blog.csdn.net/joshua_1988/article/details/45036993 有少量修改................ 2014年4月的时候自己在公司 ...
- 【sqli-labs】 less13 POST - Double Injection - Single quotes- String -twist (POST型单引号变形双注入)
报错 闭合掉括号 这关登录成功之后不显示登录的用户名密码了
- (转)基于Metronic的Bootstrap开发框架经验总结(5)--Bootstrap文件上传插件File Input的使用
http://www.cnblogs.com/wuhuacong/p/4774396.html Bootstrap文件上传插件File Input是一个不错的文件上传控件,但是搜索使用到的案例不多,使 ...
- 【转载】使用JSONObject生成和解析json
1. json数据类型 类型 描述 Number 数字型 String 字符串型 Boolean 布尔型 Array 数组 Object 对象 null 空值 (1)json中不区分整数.小数等类型, ...
- SUSE 11 SP3 搭建weblogic服务
环境的搭建和业务需求相关,仅供参考 环境: SUSE 11 SP3 安装步骤 创建一个weblogic组 创建一个用户名为weblogic的用户, 创建相关目录 上传jdk,脚本等 安装 创建用户及其 ...
- 绝对好用的浏览器json解析网址
你们是否经常在浏览器输入请求地址解析遇到中文乱码的情况,今天我找到了一个好用的浏览器解析json网址,绝对好用. 1.直接输入网址 http://pro.jsonlint.com/ 2.输入要解析的j ...