hdu4006 优先队列
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
Sample Output
HintXiao Ming won't ask Xiao Bao the kth great number when the number of the written number is smaller than k. (1=<k<=n<=1000000).
题目大意:
有n步操作,I代表往这一串数里面再加一个,Q则代表查询第k大的数是哪一个。
思路分析:首先要明白第k大的数实际上就是有k-1个数比他大,另外数据范围很大,直接暴力肯定TLE,
因此可以想到使用优先队列这种数据结构,构造最小堆,堆顶的元素刚好是第k大的数,因此当q.size()>k时
直接pop(),避免爆内存。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
int main()
{
int n,k,m;
char s[5];
while(scanf("%d%d",&n,&k)!=EOF)
{
priority_queue<int,vector<int>,less<int> >Q;
while(n--)
{
scanf("%s",s);
if(s[0]=='I')
{
scanf("%d",&m);
Q.push(m);
if(Q.size()>k) Q.pop();
}
else
{
printf("%d\n",Q.top());
}
}
}
return 0;
}
最新文章
- php根据地址的经纬度查询周围的城市例子
- C#实现自动单击
- 图片轮播 js代码
- UVaLive 6862 Triples (数学+分类讨论)
- 【转】一个安全测试的CheckList
- eclipse下切换svn用户
- 辛星解读mysql的用户管理
- 操作3 mongodb和mysql 开启慢查询日志 ,以及mongodb从配置文件启动
- hadoop端口配置指南
- Nuget 学习三
- Logstash使用grok过滤nginx日志(二)
- Graphviz--图形绘制工具
- 【Android Studio安装部署系列】三十、从Android studio2.2.2升级到Android studio3.0之路
- 2019OO第二单元总结
- Sql server 编写99乘法表
- spring ---->; aop测试需要的Maven依赖/测试时发生的一个exception
- UVa LA 3213 - Ancient Cipher 水题 难度: 0
- 请教Mysql如何删除 不包含 某些字符的记录
- GCD (Grand Central Dispatch) 笔记
- SaltStack 批量管理任务计划