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. ...
随机推荐
- ACM_螺旋填数
螺旋填数 Time Limit: 2000/1000ms (Java/Others) Problem Description: 一天,小明在研究蜗牛的壳时,对其螺旋状的花纹感到十分有趣.于是他回到了家 ...
- mygenerator().next() AttributeError: 'generator' object has no attribute 'next'
def mygenerator(): print ("start ...") yield 5 mygenerator() print ("mygenerator():&q ...
- jQuery获取及设置单选框、多选框、文本框
获取一组radio被选中项的值 var item = $("input[@name=items][@checked]").val(); 获取select被选中项的文本 var it ...
- set statistics profile on实例
set statistics profile on实例 1.SQL语句实例 SQL 代码 复制 SET STATISTICS PROFILE ON GO SELECT COUNT(b.[Sal ...
- c#使用RSA进行注册码验证
公司的一个项目快完成了,最后要加上注册验证,翻了n多资料,终于做出来了.现在把体验说一下,以后要用的时候也好找.~~ .Net自带的类库里面有个算法. 这个算法的原理是不对称加密的原理.不对称加密原理 ...
- OpenCV:OpenCV目标检测Adaboost+haar源代码分析
使用OpenCV作图像检测, Adaboost+haar决策过程,其中一部分源代码如下: 函数调用堆栈的底层为: 1.使用有序决策桩进行预测 template<class FEval> i ...
- BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 网络流 + 二分 + Floyd
Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...
- 在 CentOS 7 上设置 grub2
在 CentOS 7 上设置 grub2 1. 开机选单是自动创建出来的 请勿尝试手动编辑开机选单,因为它是按照 /boot/ 目录内的文件自动创建出来的.然而你可以调整 /etc/default/g ...
- UNIX C 文件权限 Part2_day01
1.文件访问测试 测试调用进程对指定文件是否拥有足够的访问权限 #include <unistd.h> int access(const char* pathname,int mode); ...
- BZOJ 3744 Gty的妹子序列 (分块+树状数组+主席树)
题面传送门 题目大意:给你一个序列,多次询问,每次取出一段连续的子序列$[l,r]$,询问这段子序列的逆序对个数,强制在线 很熟悉的分块套路啊,和很多可持久化01Trie的题目类似,用分块预处理出贡献 ...