您的位置:首页 > 其它

HDU 4006 The kth great number 【队列】

2015-07-29 13:49 447 查看

The kth great number

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 8367 Accepted Submission(s): 3303



[align=left]Problem Description[/align]
Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is
too much, Xiao Bao is feeling giddy. Now, try to help Xiao Bao.

[align=left]Input[/align]
There are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed
by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number.

[align=left]Output[/align]
The output consists of one integer representing the largest number of islands that all lie on one line.

[align=left]Sample Input[/align]

8 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q


[align=left]Sample Output[/align]

1
2
3

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).


[align=left]Source[/align]
The 36th
ACM/ICPC Asia Regional Dalian Site —— Online Contest

思路:

这道题是应用优先队列的思想,从小到大排序,先往已将定义好的队列中输入m个数,并按照从小到大的顺序排好,最顶元素就是第m大的元素,如果就m个元素就将最顶不的元素输出就行了,如果有大于m个元素,就需要与对顶元素比较,如果比对顶元素大就把这个元素扔进队里面,把对顶元素扔出,如果比对顶元素小就不需要进队!
这道题刚开始需要你输入n,m,他们两个分别代表案例的个数和第m 大数,接下来输入n行,如果输入IN就将IN后面的数在符合要求的条件下进队,如果输入OUT则将按题意输出!

代码:

#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;
int main()
{
int n,m,i,j,k,b;
char a[5];
while(scanf("%d%d",&n,&m)!=EOF)
{
priority_queue<int,vector<int>,greater<int> >q;
getchar();
for(i=0;i<n;i++)
{
scanf("%s",a);
if(a[0]=='I')
{
scanf("%d",&b);
if(q.size()<m)//这一点为什么不能是<=? 因为当等于m的时候就不能再往里面进数了!
q.push(b);//当等于m-1个数的时候,再进一个就满足题上说的m个数了
else if(b>q.top())
{
q.push(b);
q.pop();
}
}
else
{
printf("%d\n",q.top());
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: