Running Median_via牛客网
题目
链接: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牛客网的更多相关文章
- 牛客网 --java问答题
http://www.nowcoder.com/ 主要是自己什么都不怎么会.在这里可以学习很多的! 第一天看题自己回答,第二天看牛客网的答案! 1 什么是Java虚拟机?为什么Java被称作是“平台无 ...
- 牛客网《BAT面试算法精品课》学习笔记
目录 牛客网<BAT面试算法精品课>学习笔记 牛客网<BAT面试算法精品课>笔记一:排序 牛客网<BAT面试算法精品课>笔记二:字符串 牛客网<BAT面试算法 ...
- C++版 - HDUoj 2010 3阶的水仙花数 - 牛客网
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - ...
- 牛客网第9场多校E(思维求期望)
链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 题目描述 Niuniu likes to play OSU! We simplify the ...
- 牛客网暑期ACM多校训练营(第七场)Bit Compression
链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...
- Beautiful Numbers(牛客网)
链接:https://ac.nowcoder.com/acm/problem/17385来源:牛客网 题目描述 NIBGNAUK is an odd boy and his taste is stra ...
- 牛客网华为机试题之Python解法
牛客网华为机试题之Python解法 第1题 字符串最后一个单词的长度 a = input().split(" ") print(len(a[-1])) 第2题 计算字符个数 a = ...
- 牛客网Wannafly挑战赛25A 因子(数论 素因子分解)
链接:https://www.nowcoder.com/acm/contest/197/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- 牛客网 2018年东北农业大学春季校赛 L题 wyh的天鹅
链接:https://www.nowcoder.com/acm/contest/93/L来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒空间限制:C/C++ 262144K,其他语言524288 ...
随机推荐
- js实时查询,为空提示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MVC 与 Vue
MVC 与 Vue 本文写于 2020 年 7 月 27 日 首先有个问题:Vue 是 MVC 还是 MVVM 框架? 维基百科告诉我们:MVVM 是 PM 的变种,而 PM 又是 MVC 的变种. ...
- 运维:OAAS
IT和互联网运维的新篇章正要掀开,云计算和运维即服务(OaaS)正在为各类企业提供系统构建和扩展,帮助他们取得在线业务的成功. 互联网信息科技和运维一直在不断变化,包括了IT所覆盖的一切新工具.云.基 ...
- 一文详解 WebSocket 网络协议
WebSocket 协议运行在TCP协议之上,与Http协议同属于应用层网络数据传输协议.WebSocket相比于Http协议最大的特点是:允许服务端主动向客户端推送数据(从而解决Http 1.1协议 ...
- nacos 快速入门
每日一句 外表可是具有欺骗性的. 每日一句 No victory comes without a price. 凡是成功就要付出代价. 概述 这个快速开始手册是帮忙您快速在您的电脑上,下载.安装并使用 ...
- Stream.toList()和Collectors.toList()的性能比较
昨天给大家介绍了Java 16中的Stream增强,可以直接通过toList()来转换成List. 主要涉及下面这几种转换方式: list.stream().toList(); list.stream ...
- 关于spring整合mybatis
第一步导入依赖 <dependencies> <dependency> <groupId>org.mybatis</groupId> <artif ...
- bare Git 仓库是什么?
背景 今天,坐我旁边的同事问我一些关于服务器上命令的问题.其中有一个用了特殊参数的 git init 的命令,我也不认识,遂去 Google... bare Git 仓库 定义 A bare Git ...
- Django从数据库导model迁移乱码
python manage.py inspectdb > [your app name]\models.py 执行后 model.py乱码,将models.py以 UTF-16LE打开.
- 合宙AIR105(二): 时钟设置和延迟函数
目录 合宙AIR105(一): Keil MDK开发环境, DAP-Link 烧录和调试 合宙AIR105(二): 时钟设置和延迟函数 Air105 的时钟 高频振荡源 芯片支持使用内部振荡源, 或使 ...