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. ...
随机推荐
- android平台 cocos2d-x 读取相册数据
现已解决 方案如下: 1.使用 jni 调用 java 方法 启动相册选择框2.使用java将获取的图片保存到本地3.使用Cocos2d-x中 CCImage 读取 JAVA代码如下: //启动图片选 ...
- 表单校验插件(bootstrap-validator)
表单校验插件(bootstrap-validator) 参考文档 http://blog.csdn.net/nazhidao/article/details/51542508 http://blog. ...
- intellij IDEA常见操作
1.中文乱码设置:file - setting - Editor - File Encodings 设置为UTF-8 2.tomcat重新启动:Ctrl-F5,或者左上角 3.删除progect 先c ...
- SQL中EXTRACT() 函数
EXTRACT()("提取"的意思) 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. 就是返回出来具体的年,月,日 2008-12-29 16:25:46.63 ...
- Mongo连接远程数据库
mongo IP+Port CrabyterV5 首先这么操作是基于配置了环境变量的,可以参照http://www.cnblogs.com/daiyonghui/p/5209076.html mong ...
- 使用GitGUI创建上传本地工程
参考链接: 使用Git-GUI创建工程 http://jingyan.baidu.com/article/27fa732683ebf546f8271f2e.html 一.刚创建的github版本库,在 ...
- [CefSharp] 如何在JavaScript中调用C#代码
本例在WinForms下实现,具体流程与WPF一致. 本例仅供调用示例,不代表正常业务书写流程. 1. 创建WinForms项目,并将项目属性设置为x86平台 此处预先设置,避免引用时报错,再花更多的 ...
- react基础篇三
事件处理 React事件绑定属性的命名采用驼峰式写法,而不是小写. 如果采用 JSX 的语法你需要传入一个函数作为事件处理函数,而不是一个字符串(DOM元素的写法) 例如,传统的 HTML: < ...
- Currying vs Partial Application
柯里化相当于函数重构: 偏函数相当于函数适配. So, what is the difference between currying and partial application? As we s ...
- 备份xx
https://www.tuicool.com/articles/V3EBzev https://www.tuicool.com/topics/11080087?st=0&lang=1& ...