hdu 4006 The kth great number(优先队列)
来源:互联网 发布:刘义军 知乎 编辑:程序博客网 时间:2024/05/01 13:31
简单的,赤裸裸的优先队列题目!
小朋友都惊呆了,我错了好几次!!!
题目要求输出第k大的数,数值一直在更新,故普通方法会超时的,
这题可以用一个值记录输入数的个数,若个数小于k则输入的数坚定入列,
否则输入的数与入列的第k个数比较,若大于第k个数,删除第k个数,将该数入列!
#include<stdio.h>
#include<string.h>#include<queue>
using namespace std;
struct node
{
int x;
friend bool operator<(node a,node b)
{
return a.x>b.x;
}
};
int main()
{
int n,k,i,m,count;
node cur;
char str[10];
while(scanf("%d%d",&n,&k)!=EOF)
{
priority_queue<node>q;
count=0;
for(i=0;i<n;i++)
{
scanf("%s",str);
if(str[0]=='I')
{
scanf("%d",&m);
cur.x=m;
count++;
if(count<=k)
{
q.push(cur);
}
else
{ //刚开始在这写的是cur=q.top();if(m>cur.x)好吧,就错在这里,cur在上面已经赋值了,在这又赋值,果断的把原来的数据丢失了,故Wrong,晕!
//低级的错误!!
if(m>q.top().x){
q.pop();
q.push(cur);
}
}
}
else
{
cur=q.top();
printf("%d\n",cur.x);
}
}
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006
- HDU 4006 The kth great number(优先队列)
- HDU 4006 The kth great number(优先队列)
- hdu 4006 The kth great number(优先队列)
- HDU-#4006 The kth great number(优先队列)
- HDU 4006 The kth great number (优先队列)
- 【HDU 4006】The kth great number(优先队列)
- HDU 4006 The kth great number (优先队列)
- 【HDU】-4006-The kth great number(优先队列,好)
- HDU:4006 The kth great number(优先队列)
- HDU 4006 The kth great number(优先队列)
- hdu 4006 The kth great number(优先队列)
- hdu 4006 The kth great number(优先队列)
- hdu 4006 The kth great number(使用优先队列)
- HDU 4006 The kth great number(优先队列、堆实现)
- HDU 4006 The kth great number (堆实现优先队列)
- hdu 4006 The kth great number 优先队列
- HDU 4006 The kth great number【优先队列】
- hdu 4006 The kth great number(优先队列)
- 京东商城发现了一枚Bug
- (原创)3.2 AddOwner和OverrideMetadata的区别
- HBase安装
- Android应用开发学习笔记之菜单
- Ubuntu11.04更新源(转)
- hdu 4006 The kth great number(优先队列)
- LeetCode-Path Sum
- SpringMVC+mybatis+DWR3注解
- 【xinfanqie】笔者分享:教你如何快速关闭磁盘索引
- java先序中序建立二叉树的递归算法
- android ndk 下载地址
- Loaders初步学习
- 13年 8 月16
- velocity与struts2