【每日一题】4月9日题目精讲 Running Median
文章目录
- 题目:
- 题意:
- 题解一:
- 题解二:
题目:
–>链接<—
时间限制: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 integerP(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 (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
题意:
给你m个数,一次输入,每当输入的个数为奇数时,输出按大小排列最中间的数
比如1 5 6 7 8
一开始输入1,输出1
然后输入1 5,不输出
输入1 5 6,输出5
输入1 5 6 7,不输出
输入1 5 6 7 8,输出6
题解一:
可以用堆来做
w1为大堆,w1用于存放小值
w2为小堆,w2存放大值
比如上面那个例子1 5 6 7 8
奇数位存在w1,偶数存在w2
如果w1.top()>w2.top(),就是w1的最大比w2的最小值大,就将这两个值互换,始终保证,w1的值比w2的任意一个都小,这样无论数据怎么读入,w1的最大值始终都是最中间的数
看下面模拟:
第一轮:
w1:1
w2:0
二:
w1:1
w2:5
三:
w1:1 6
w2:5
6>5
w1:1 5
w2:6
四
w1:1 5
w2: 7 6
五
w1:1 5 8
w2: 7 6
8>6
w1:1 5 6
w2; 7 8
这样每奇数轮,w1的top位就是答案
#include <iostream>
#include<queue>
#include<cstdio>
using namespace std;priority_queue<int>w1;//默认为大堆,从大到小priority_queue<int,vector<int>,greater<int> >w2;//默认为小堆
int main()
{// freopen("in.txt","r",stdin);//ios::sync_with_stdio(0);int n;cin>>n;int case1=0;int p,m,x;int minn,maxx;while(n--){cin>>p>>m;case1=0;printf("%d %d\n",p,m/2+1);for(int i=1;i<=m;i++){cin>>x;if(i%2==1)w1.push(x);else w2.push(x);if(i!=1){minn=w1.top();maxx=w2.top();if(minn>maxx){w1.pop();w2.pop();w1.push(maxx);w2.push(minn);}}if(case1!=0&&case1%10==0){cout<<endl;case1=0;}if(i%2==1){if(i==m)cout<<w1.top();elseprintf("%d ",w1.top());case1++;}}cout<<endl;while(!w1.empty())w1.pop();while(!w2.empty())w2.pop();}return 0;
}
(这个题的格式我一开始一直没注意。。。)
题解二:
我看很多人都是这么做的,但是只能过50%的数据。。包括我自己
我看了清楚姐姐的代码稍微改一下:
我们在读入一个数后,直接与w1.top比较,小于就存进去,大了就存w2里
当w1的数量多了,就把堆顶拿出给w2(小根堆)
w2多了就给大根堆
这样维护出来其实和上面的方法差不多
因为总数是奇数,两个堆数量一定不一样,多的那方,堆顶就是答案
代码:
清楚阿姨(狗头 )代码:
#include<bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>, greater<int> > q2; //小根堆 ,存较大的一半的数
priority_queue<int> q1; //大根堆 ,存较小的一半的数
int main()
{int t;scanf("%d", &t);while(t--){while(!q1.empty()) q1.pop();while(!q2.empty()) q2.pop(); //优先队列没有clear函数,要一个一个弹出int m, n;scanf("%d %d", &m ,&n);printf("%d %d\n", m, (n+1)/ 2);int x;scanf("%d", &x);q1.push(x);printf("%d ", x);for(int i = 2; i <= n; i++){int x;scanf("%d", &x);if (x <= q1.top()) q1.push(x); //如果当前值比大根堆堆顶小,说明在小的这二分之一,塞进大根堆else q2.push(x);int num1= q1.size();int num2= q2.size();if (num1 - num2 > 1) //大根堆里面元素多了,把堆顶拿出来塞近小根堆{q2.push(q1.top());q1.pop();}else if(num2 - num1 >1) //小根堆里面元素多了,把堆顶拿出来塞近大根堆{q1.push(q2.top());q2.pop();}if (i % 2 == 1) //目前的元素个数是奇数{num1 = q1.size();num2 = q2.size();if(num1 > num2) printf("%d ", q1.top()); //中位数在大根堆else printf("%d ", q2.top()); //中位数在小根堆if (i % 20 == 19 && i!=n) printf("\n");}}printf("\n");}
return 0;
}
【每日一题】4月9日题目精讲 Running Median相关推荐
- 牛客网 每日一题 7月23日题目精讲—wpy的请求
来源:牛客网: 文章目录 wpy的请求 题解: 代码: wpy的请求 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K Special Judge ...
- 【每日一题】8月28日题目精讲 编号
[每日一题]8月28日题目精讲 编号 链接:https://ac.nowcoder.com/acm/problem/19925 来源:牛客网 题目描述 你需要给一批商品编号,其中每个编号都是一个7位1 ...
- 【每日一题】7月17日题目精讲—BOWL 碗的叠放
[每日一题]7月17日题目精讲-BOWL 碗的叠放 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld ...
- 【每日一题】7月15日题目精讲—生日快乐
[每日一题]7月15日题目精讲-生日快乐 [SCOI2009]生日快乐 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO For ...
- 【每日一题】7月13日题目精讲—Kingdom
[每日一题]7月13日题目精讲-Kingdom 文章目录 题目描述 题解: 代码: 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 1048576K,其他语言2097152K 64bi ...
- 【牛客每日一题】tokitsukaze and Soldier 题目精讲 贪心、优先队列、堆
链接:https://ac.nowcoder.com/acm/problem/50439 来源:牛客网 ACM在线模板 今天才发现牛客推出了一个每日一题的版块,3月25号就开始了,今天才发现,赶紧补救 ...
- 【每日一题】5月7日题目精讲 「火」皇家烈焰
链接: 「火」皇家烈焰 文章目录 题目描述 题解: 代码: 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: % ...
- 牛客网【每日一题】4月14日题目精讲 Xorto
文章目录 题目描述 题解: 代码: 扩展 传送 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format:%lld 题目描述 ...
- 【每日一题】7月1日题目精讲 借教室
来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K 64bit IO Format: %lld 文章目录 题目描述 题解: 差分: 二 ...
最新文章
- OpenCV持久化(二)
- Clean-Code: 注释
- 图解未来50年前沿科技趋势与22个预测
- cocos2d笔记——解析HelloWorldScene
- [Dynamic Language] Python 静态方法、类方法、属性
- keyStore vs trustStore--转载
- linux script $,linux – 这在shell脚本中是什么意思SCRIPTNAME =“${0 ## * /}”?
- C++ 数组在内存中的分配
- python之socket
- html怎样在一张图片里写字,用HTML代码在图片上写字
- 当代计算机技术在建筑设备中的应用,计算机技术在建筑设计中应用浅谈.doc
- ubuntu 18.04 卸载 mysql 过程记录
- html不建议翻译,[译] 可用但最不常见的 HTML5 标签
- QEMU文档之bypass iommu
- 使用 docker 来安装 oracle 11c
- 真心实用!Python 办公自动化之 PDF 的详细操作(全)
- 扭矩扭力测量——扭矩传感器
- 《网络游戏核心技术与实战》学习笔记——专业术语
- 一物一码(6): 一物一码之【开展一物一码营销活动基本流程2,设计开发部署,生产包装,活动上线】
- 没去现场?一文 Get 云栖大会「移动专场」精华内容~含PPT下载
热门文章
- 史上最牛空姐,从飞机上掉下愣是没摔死
- php正则获取标签,PHP通过正则匹配中文字符 过滤html标签 获取文本内容
- python退出帮助系统help应该使用exit_python--help - tesion
- solr 配置mysql数据源_solr data-config.xml配置文件的见解mysql数据源
- html设置样式不继承_web前端入门到实战:css的核心原理分为优先级原则与继承原则两大部分...
- 帆软获取上月的第一天与最后一天_《原神》岩港打工第一天怎么玩 岩港打工第一天玩法攻略...
- 纯c语言实现的改进暗通道去雾算法测试程序(附赠大量测试图像),基于改进暗通道先验算法的图像去雾...
- as本地仓库更改_将gitee仓库连接GitHub Desktop。新建更改仓库并上传至gitee。
- centos 重启网卡_CentOS7网络配置和修改网卡名称及常用服务管理命令
- xilinx7中管脚mrcc和srcc_Xilinx 7系列FPGA架构之SelectIO结构(一)