题目

链接:https://ac.nowcoder.com/acm/contest/28886/1002
来源:牛客网

时间限制:C/C++ 5秒,其他语言10秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

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)P(1 \leq P  \leq 1000)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)M (1  \leq M  \leq 9999)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.

示例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

题解

这道题目显然不能使用一直排序来求.

在这道题目里, 引起变化的是每时每刻插入一个新的数据,原先的处理结果要放着,而不是直接丢弃.

我认为如果是把中位数放到外面,就会引起一系列的不方便,还不如放到里面.

假想

代码

#include <iostream>
#include <queue>
#include <vector> using namespace std;
int print_cnt = 0;
priority_queue<int, vector<int>, greater<int> >q2;
priority_queue<int, vector<int>, less<int> >q1;
vector<int>v;
inline void print()
{
for (auto it = v.begin(); it != v.end(); it++)
{
if (print_cnt >= 10)
{
cout << endl;
print_cnt = 0;
}
cout << *it << ' ';
print_cnt++;
} }
inline void init()
{
priority_queue<int, vector<int>, greater<int> >q2_;
priority_queue<int, vector<int>, less<int> >q1_;
print_cnt = 0;
q1.swap(q1_);
q2.swap(q2_);
v.clear();
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
init();
int data_no;
int n;
cin >> data_no >> n;
for (int i = 0; i < n; i++)
{
int tmp;
cin >> tmp;
if (q1.empty() && q2.empty())
q1.push(tmp);
else if (!q1.empty())
{
if (tmp > q1.top())
q2.push(tmp);
else
q1.push(tmp);
}
while (q1.size() >= q2.size() + 2)
{
q2.push(q1.top());
q1.pop();
}
while (q1.size()+2 <= q2.size())
{
q1.push(q2.top());
q2.pop();
}
if (!(i % 2))
{
if (q2.size() > q1.size())
v.push_back(q2.top());
else
v.push_back(q1.top());
}
}
cout << data_no <<' ' << ((n + 1) / 2) << endl;
print();
cout << endl; }
return 0;
}

Running Median_via牛客网的更多相关文章

  1. 牛客网 --java问答题

    http://www.nowcoder.com/ 主要是自己什么都不怎么会.在这里可以学习很多的! 第一天看题自己回答,第二天看牛客网的答案! 1 什么是Java虚拟机?为什么Java被称作是“平台无 ...

  2. 牛客网《BAT面试算法精品课》学习笔记

    目录 牛客网<BAT面试算法精品课>学习笔记 牛客网<BAT面试算法精品课>笔记一:排序 牛客网<BAT面试算法精品课>笔记二:字符串 牛客网<BAT面试算法 ...

  3. C++版 - HDUoj 2010 3阶的水仙花数 - 牛客网

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - ...

  4. 牛客网第9场多校E(思维求期望)

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 题目描述 Niuniu likes to play OSU! We simplify the ...

  5. 牛客网暑期ACM多校训练营(第七场)Bit Compression

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...

  6. Beautiful Numbers(牛客网)

    链接:https://ac.nowcoder.com/acm/problem/17385来源:牛客网 题目描述 NIBGNAUK is an odd boy and his taste is stra ...

  7. 牛客网华为机试题之Python解法

    牛客网华为机试题之Python解法 第1题 字符串最后一个单词的长度 a = input().split(" ") print(len(a[-1])) 第2题 计算字符个数 a = ...

  8. 牛客网Wannafly挑战赛25A 因子(数论 素因子分解)

    链接:https://www.nowcoder.com/acm/contest/197/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

  9. 牛客网 2018年东北农业大学春季校赛 L题 wyh的天鹅

    链接:https://www.nowcoder.com/acm/contest/93/L来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒空间限制:C/C++ 262144K,其他语言524288 ...

随机推荐

  1. 探索ABP的EventHub解决方案

    在上一章中,我们构建了一个简单的全栈 Web 应用程序,我们已经看到了使用 ABP 框架开发应用的典型流程,在接下来,我们将使用 ABP 框架创建更高级的应用程序. 给出具有现实世界复杂性的例子并不容 ...

  2. AQS源码探究之竞争锁资源

    AQS源码探究---竞争锁资源 我们进入ReentrantLock源码中查看其内部类 Sync 对AQS进行扩展公共方法并定义抽象方法的抽象类 FaireSync 实现公平锁的AQS的实现类 UnFa ...

  3. 10┃音视频直播系统之 WebRTC 中的数据统计和绘制统计图形

    一.数据统计 在视频直播中,还有一项比较重要,那就是数据监控 比如开发人员需要知道收了多少包.发了多少包.丢了多少包,以及每路流的流量是多少,才能评估出目前用户使用的音视频产品的服务质量是好还是坏 如 ...

  4. 运维:DevSecOps

    什么是DevSecOps DevSecOps 是一场关于 DevOps 概念实践或艺术形式的变革.DevOps之父Patrick Debios 强调:"DevOps2.0时代应首先解决人的问 ...

  5. 好客租房12-JSX的注意点

    1.4注意点 1React元素的属性名使用驼峰式命名法 2特殊属性名 class-className for->htmlFor 3没有子节点可以用单标签表示 4使用小括号包裹jsx const ...

  6. Springmvc基础及应用

    SpringMVC简介和环境搭建 SpringMVC简介 Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一.在Spring3.0 后全面超越 S ...

  7. CF1682E Unordered Swaps

    鸽着,我不知道为什么对? 题意: 思路: code: #include<bits/stdc++.h> using namespace std; const int N=5e5+5; int ...

  8. 小样本利器1.半监督一致性正则 Temporal Ensemble & Mean Teacher代码实现

    这个系列我们用现实中经常碰到的小样本问题来串联半监督,文本对抗,文本增强等模型优化方案.小样本的核心在于如何在有限的标注样本上,最大化模型的泛化能力,让模型对unseen的样本拥有很好的预测效果.之前 ...

  9. Cabloy-CMS中区块的开发与效果

    关于区块 Cabloy-CMS引入了区块的概念,通过区块可以快速向文章添加各种类型的内容,比如:插入一个地图子页面.插入一首音乐,等等 Cabloy-CMS中的区块可以类比于Wordpress古腾堡编 ...

  10. Xmind演讲主题大纲

    参考:https://www.bilibili.com/video/BV1Rb411s7VG?p=9