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; }
相关文章推荐
- android volley 预研
- Android View的onTouchEvent和OnTouch区别
- Robotium之Android控件定位实践和建议(Appium/UIAutomator姊妹篇)
- 计蒜客 京东的物流路径
- spring3+quartz2
- win8.1 实现安装net frament3.5成功
- Python用Bottle轻量级框架进行Web开发
- easyui 获取指定列的值,格式化该列的值的实例
- Xcode7 不能使用http网络请求
- Spring事务 UnexpectedRollbackException
- 怎么学习一个开源框架
- 云平台的项目管理工作该如何走?
- BZOJ 1257: [CQOI2007]余数之和sum( 数论 )
- 用java做了个控制台版的2048
- MySql 索引
- C# 导入EXCEL 报错外部表不是预期的格式错误
- Painter(模拟)
- bzoj2705
- Spiral Matrix II
- Android-给照片底部添加波浪效果