ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NC50940 Running Median

2022-07-09 00:03:31  阅读:167  来源: 互联网

标签:tmp NC50940 Median pq2 number pq1 Running output line


题目链接

题目

题目描述

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 \leq P \leq 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 \leq M \leq 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.

示例1

输入

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

输出

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

题解

知识点:优先队列。

用两个优先队列维护中位数左边和右边的序列,因为中位数一定在序列的中间位置,左边比他大,右边比他小且数量几乎一样(相差不超过 \(1\) )。因此左边维护大顶堆,右边维护小顶堆,每次存入数据后比较两序列的大小,那边比那边大超过 \(1\) 就把队头转移直至平衡,就可以维护一个动态中位数了。

时间复杂度 \(O(n \log n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long

using namespace std;

bool solve() {
    int p, m;
    cin >> p >> m;
    cout << p << ' ' << (m + 1) / 2 << '\n';
    priority_queue<int> pq1;
    priority_queue<int, vector<int>, greater<int>> pq2;
    int tmp;
    cin >> tmp;
    pq1.push(tmp);
    cout << pq1.top() << ' ';
    for (int i = 2;i <= m;i++) {
        cin >> tmp;
        if (tmp <= pq1.top()) pq1.push(tmp);
        else pq2.push(tmp);
        if (pq1.size() > 1 + pq2.size()) {
            pq2.push(pq1.top());
            pq1.pop();
        }
        else if (pq1.size() + 1 < pq2.size()) {///注意,不要size相减,会ULL溢出,-1变最大值
            pq1.push(pq2.top());
            pq2.pop();
        }
        if (i & 1) cout << (pq1.size() > pq2.size() ? pq1.top() : pq2.top()) << ' ';
        if (!(i % 20)) cout << '\n';
    }
    cout << '\n';
    return true;
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        if (!solve()) cout << -1 << '\n';
    }
    return 0;
}

标签:tmp,NC50940,Median,pq2,number,pq1,Running,output,line
来源: https://www.cnblogs.com/BlankYang/p/16459938.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有