[题解]Running Median

    科技2022-08-05  125

    题目描述 For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of the elements received so far. 输入 The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by an odd decimal integer M, (1 ≤ M ≤ 9999), giving the total number of signed integers to be processed. The remaining line(s) in the dataset consists of the values, 10 per line, separated by a single space. The last line in the dataset may contain less than 10 values. 输出 For each data set the first line of output contains the data set number, a single space and the number of medians output (which should be one-half the number of input values plus one). The output medians will be on the following lines, 10 per line separated by a single space. The last line may have less than 10 elements, but at least 1 element. There should be no blank lines in the output. 样例输入 Copy 3 1 9 1 2 3 4 5 6 7 8 9 2 9 9 8 7 6 5 4 3 2 1 3 23 23 41 13 22 -3 24 -31 -11 -8 -7 3 5 103 211 -311 -45 -67 -73 -81 -99 -33 24 56 样例输出 Copy 1 5 1 2 3 4 5 2 5 9 8 7 6 5 3 12 23 23 22 22 13 3 5 5 3 -3 -7 -3 提示 来源:Greater New York Regional 2009

    问题传送门

    题目的理解

    这道题与洛谷P1168类似但输出有点***,对于题目描述,可知这道题让我们做一个维护区间序列 a a a的中位数,注意需要排序(对于有限的数集,可以通过把所有观察值高低排序后找出正中间的一个作为中位数。如果观察值有偶数个,通常取最中间的两个数值的平均数作为中位数。 --选自百度百科).

    分析这道题

    首先,注意序列个数达到奇数个时,要输出询问答案。

    对于最暴力的方法

    对于每次询问,做一遍查询,排序,输出 a n s k / 2 + 1 ans_{k / 2 + 1} ansk/2+1这个数即可。

    分析时间复杂度为 O ( n 2 log ⁡ n ) O(n^2 \log n) O(n2logn),此时过不了这道题,但可以拿到 40 % 40 \% 40%的好成绩.

    代码如下
    #include<bits/stdc++.h> using namespace std; int n,a[100001]; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ cin>>a[i]; if(i==1)printf("%d\n",a[i]); if(i!=1&&i&1){ sort(a+1,a+i+1); printf("%d\n",a[(1+i)/2]); } }return 0; }

    对于正解的方法1

    用堆来维护,然后二分, 分析时间复杂度为 O ( n log ⁡ n ) . O(n \log n). O(nlogn).可以过,但这不是重点。

    对此代码
    #include <iostream> #include <cstdio> #include <queue> using namespace std; priority_queue<int> big; priority_queue<int, vector<int>, greater<int> > small; int main() { int T; scanf("%d", &T); int id, m, cnt, a, mid; while(T--) { while(!big.empty()) big.pop(); while(!small.empty()) small.pop(); scanf("%d%d", &id, &m); cnt = 1, mid = -0x3fffff; cout<<id<<" "<<(m + 1 >> 1)<<endl; for(int i = 1; i <= m; ++i) { scanf("%d", &a); if(big.empty()) { big.push(a); mid = a; cout<<a<<" "; continue; } if(a < mid) big.push(a); else small.push(a); if(((int)big.size() - (int)small.size()) > 1) { int t = big.top(); big.pop(); small.push(t); } else if(((int)small.size() - (int)big.size()) > 1) { int t = small.top(); small.pop(); big.push(t); } if(i & 1) { ++cnt; if(big.size() > small.size()) mid = big.top(); else mid = small.top(); if(cnt % 10 == 0) cout<<mid<<endl; else cout<<mid<<" "; } else mid = (small.top() + big.top()) / 2; } if(cnt % 10 != 0) cout<<endl; } return 0; }

    对于正解的方法2

    这道题用链表维护也可以。

    对于正解的方法3(重点)

    我们考虑用平衡树来维护这个序列,对于每次询问,只需要询问 [ 1 , i ] [1,i] [1,i]区间即可,查找区间这段排名 ( k t h ) (kth) (kth) ( i / 2 + 1 ) (i / 2 + 1) (i/2+1)的数字。其他情况只需要插入 ( i n s e r t ) (insert) (insert),然后一个平衡树板子就好了。

    分析时间复杂度:对于平衡树查询有 O ( log ⁡ n ) O(\log n) O(logn)的复杂度, 然后询问存在大概 n / 2 + 1 n / 2 + 1 n/2+1次, 所以时间复杂度约为 O ( n ) O(n) O(n) 综合一下,这道题用平衡树来维护的时间复杂度为 O ( n log ⁡ n ) O(n \log n) O(nlogn). 可以过了。

    Splay代码

    Splay \text{Splay} Splay 为例(这里无需排序,因为 Splay \text{Splay} Splay 本质已经排过序了,该代码复杂度为 O ( n log ⁡ n ) O(n \log n) O(nlogn)):

    #include<cstdio> #include<cstring> #define re register using namespace std; const int maxn = 1e5 + 7; int root,ncnt; struct node { int ch[2],val,cnt,fa,size; } tr[maxn]; inline void pushup(re int x) { tr[x].size=tr[tr[x].ch[0]].size+tr[tr[x].ch[1]].size+tr[x].cnt; } inline void rotate(re int x) { re int y=tr[x].fa,z=tr[y].fa,k=tr[y].ch[1]==x,w=tr[x].ch[k^1]; tr[y].ch[k]=w; tr[w].fa=y; tr[z].ch[tr[z].ch[1]==y]=x; tr[x].fa=z; tr[x].ch[k^1]=y; tr[y].fa=x; pushup(y),pushup(x); } inline void splay(re int x,re int rt) { while(tr[x].fa != rt) { re int y=tr[x].fa,z=tr[y].fa; if(z!=rt)(tr[z].ch[0]==y)^(tr[y].ch[0]==x)?rotate(x):rotate(y); rotate(x); } if(!rt)root=x; } inline void insert(re int x) { re int cur=root,p=0; while(cur&&tr[cur].val!=x)p=cur,cur=tr[cur].ch[x>tr[cur].val]; if(cur)tr[cur].cnt++; else { cur=++ncnt; if(p)tr[p].ch[x>tr[p].val]=cur; tr[cur].ch[0]=tr[cur].ch[1]=0; tr[cur].fa=p; tr[cur].val=x; tr[cur].cnt=tr[cur].size=1; } splay(cur,0); } inline void find(re int x) { if(!root)return; int cur=root; while(tr[cur].ch[x>tr[cur].val]&&x!=tr[cur].val)cur=tr[cur].ch[x>tr[cur].val]; splay(cur,0); } inline int kth(re int k) { re int cur=root; if(tr[cur].size<k)return 0; while(1) if(tr[cur].ch[0]&&k<=tr[tr[cur].ch[0]].size)cur=tr[cur].ch[0]; else if(k>tr[tr[cur].ch[0]].size+tr[cur].cnt) { k -= tr[tr[cur].ch[0]].size+tr[cur].cnt,cur=tr[cur].ch[1]; } else { splay(cur,0); return cur; } } int t,cnt,n,m,x; int main() { // freopen("1.txt","w",stdout); scanf("%d",&t); while(t--) { scanf("%d%d",&cnt,&n); printf("%d %d\n",cnt,(n+1) >> 1); memset(tr,0,sizeof(tr)); for(re int i=1; i<=n; i++) { scanf("%d",&x); insert(x); if(i & 1){ printf("%d ",tr[kth((i / 2 + 1))].val); if(((i+1)/2)%10 == 0)puts(""); else if((n%2==1&&i==n)||(n%2==0&&i==n-1))puts(""); } } } return 0; }

    又用平衡树水过一道题。

    Processed: 0.040, SQL: 8