题目

链接: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. 真香!Windows 可直接运行 Linux 了

    点击关注上方"开源Linux", 后台回复"读书",有我为您特别筛选书籍资料~ 之前了解过一些适用于Linux的Windows子系统,最近又听人提起,于是在自己 ...

  2. SICP 2.2: 层次性数据和闭包性质(Python实现)

    绪论 序对可以为我们提供用于构造复合数据的基本"粘接剂",鉴于Python中tuple中元素不可变的性质,我们通过list来实现序对,如[1, 2].Python的PyListOb ...

  3. CesiumJS 2022^ 原理[5] - 着色器相关的封装设计

    目录 1. 对 WebGL 接口的封装 1.1. 缓冲对象封装 1.2. 纹理与采样参数封装 1.3. 着色器封装 1.4. 上下文对象与渲染通道 1.5. 统一值(uniform)封装 1.6. 渲 ...

  4. docker-compose 启动 rabbitmq

    说明 前提条件 ubuntu-20.04-server docker & docker-compose 安装参考 安装 准备 rabbitmq.conf 新建 rabbitmq.conf 文件 ...

  5. Java - happens-before

    Java - happens-before JSR-133对 happens-before 关系的定义如下: 如果一个操作 happens-before 另一个操作,那么第一个操作的执行结果将对第二个 ...

  6. 技术分享 | Appium环境安装与架构介绍

    原文链接 Appium架构 Appium 设计哲学 不需要为了自动化而重新编译或修改被测应用 不应该让移动端自动化测试限定在某种语言或者某个具体的框架 不要为了移动端的自动化测试而重新造轮子 移动端自 ...

  7. SQL Server 2008~2019版本序列号/密钥/激活码 汇总

    SQL Server 2019 Enterprise:HMWJ3-KY3J2-NMVD7-KG4JR-X2G8G Strandard:PMBDC-FXVM3-T777P-N4FY8-PKFF4 SQL ...

  8. Java基础-并发篇

    3.1. JAVA 并发知识库 3.2. JAVA 线程实现/创建方式 3.2.1. 继承 Thread 类 ​ Thread 类本质上是实现了 Runnable 接口的一个实例,代表一个线程的实例. ...

  9. RPA-UiPath视频教程1

    UiPath下载.安装.激活.第一个案例Helloworld!.参数类型.变量的介绍和使用 https://www.bilibili.com/video/av92816532 RPA直播公开课2020 ...

  10. 漫谈客户端存储技术之Cookie篇

    Cookie 说到Cookie,不管作为前端开发人员还是后端开发人员并不陌生,作为一种最古老.最稳定的客户端存储形式,即便是在当下各种新的客户端存储技术层出不穷的时代,它仍旧有其一席之位.Cookie ...