题目

链接: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. S2-045远程命令执行漏洞的利用

    Apache Struts2 远程命令执行 (S2-045) 漏洞介绍: 漏洞编号:S2-045CVE编号:CVE-2017-5638漏洞类型:远程代码执行漏洞级别:高危漏洞风险:黑客通过利用漏洞可以 ...

  2. axios源码解析 - 请求方法的别名实现

    axios中的创建请求方式很多,比如axios(url),axios.get(url),axios.post(url),axios.delete(url),方便快捷的api设计让axios火得一塌糊涂 ...

  3. html5 tts(文字朗读)

    在 chrome 下使用比较好的中文语音包. 注意 speechSynthesis.getVoices() 有时候可能会返回空数组,需要做验证 var zhCnLangs = speechSynthe ...

  4. L2M-GAN: Learning to Manipulate Latent Space Semantics for Facial Attribute Editing阅读笔记

    L2M-GAN: Learning to Manipulate Latent Space Semantics for Facial Attribute Editing 2021 CVPR L2M-GA ...

  5. 【SpringCloud原理】万字剖析OpenFeign之FeignClient动态代理生成源码

    年前的时候我发布两篇关于nacos源码的文章,一篇是聊一聊nacos是如何进行服务注册的,另一篇是一文带你看懂nacos是如何整合springcloud -- 注册中心篇.今天就继续接着剖析Sprin ...

  6. CenterNet训练时黑白图片不能画框的问题

    解决CenterNet在detect.py中不能画框的问题 在第centernet.py的第198行的中加上这一行 image = image.convert('RGB')

  7. PostgreSQL 的窗口函数 OVER, WINDOW, PARTITION BY, RANGE

    最近在数据处理中用到了窗函数, 把使用方法记录一下, 暂时只有分组排序和滑动时间窗口的例子, 以后再逐步添加 场景 在SQL查询时, 会遇到有两类需要分组统计的场景, 在之前的SQL语法中是不方便实现 ...

  8. Aged-cat 的 WebServer 配置详细流程

    前言 最近看到一个不错的webserver项目,但是配置过程给的不详细,这里给出详细配置过程 项目地址:https://github.com/Aged-cat/WebServer (感谢老猫大神的项目 ...

  9. Groovy基础语法

    Groovy 基础语法 变量定义 1.支持动态类型,使用def关键字定义变量 // Java中定义变量的方式 int age = 18; String name = "张三"; / ...

  10. 深入理解 happens-before 原则

    在前面的文章中,我们深入了解了 Java 内存模型,知道了 Java 内存模型诞生的意义,以及其要解决的问题.最终我们知道:Java 内存模型就是定义了 8 个基本操作以及 8 个规则,只要遵守这些规 ...