HDUOJ----4006The kth great number(最小堆...)
The kth great number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 6020 Accepted Submission(s): 2436
Xiao 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).

- #include<iostream>
- #include<set>
- using namespace std;
- int main()
- {
- int n,k,i,temp;
- char ss[];
- multiset<int> sta;
- while(scanf("%d%d",&n,&k)!=EOF)
- {
- sta.clear();
- for(i=;i<n;i++)
- {
- scanf("%s",ss);
- if(*ss=='I')
- {
- scanf("%d",&temp);
- if(i<k) sta.insert(temp);
- else
- {
- int head=*sta.begin();
- if(temp>head)
- {
- sta.erase(sta.begin());
- sta.insert(temp);
- }
- }
- }
- else cout<<*(sta.begin())<<endl;
- }
- }
- return ;
- }
方法二:
采取传统的最小堆,最大堆求解..
- /*最小堆hdu 4006*/
- /*@code Gxjun*/
- #include<stdio.h>
- #include<string.h>
- #define maxn 1000002
- int heap[maxn],n,k;
- void change(int *a ,int *b){
- *a^=*b , *b^=*a, *a^=*b;
- }
- void updata_heap(int tol)
- {
- if(!(tol&)) //是偶数数表示完全二叉树
- {
- if(heap[tol]<heap[tol>>])
- change(&heap[tol],&heap[tol>>]);
- tol--;
- }
- for(int i=tol ; i> ;i-=)
- {
- if(heap[i]>heap[i-])
- {
- if(heap[i-]<heap[i>>])
- change(&heap[i-],&heap[i>>]);
- }
- else
- if(heap[i]<heap[i>>])
- change(&heap[i],&heap[i>>]);
- }
- }
- //数据更新
- void input_heap()
- {
- char ss[];
- int i,temp;
- for(i= ; i<=k ;i++)
- scanf("%s %d",ss,&heap[i]);
- updata_heap(k);
- for(i=k+ ;i<=n ;i++)
- {
- scanf("%s",ss);
- if(*ss=='I')
- {
- scanf("%d",&temp);
- if(temp>heap[])
- {
- heap[]=temp;
- updata_heap(k);
- }
- }
- else
- if(*ss=='Q')
- printf("%d\n",heap[]);
- }
- }
- int main()
- {
- /*freopen("test.out","w",stdout);*/
- while(scanf("%d%d",&n,&k)!=EOF)
- {
- /*memset(heap,0,sizeof(int)*(k+2));*/
- input_heap();
- }
- return ;
- }
HDUOJ----4006The kth great number(最小堆...)的更多相关文章
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- Lintcode: Kth Smallest Number in Sorted Matrix
Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...
- Lintcode401 Kth Smallest Number in Sorted Matrix solution 题解
[题目描述] Find the kth smallest number in at row and column sorted matrix. 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的 ...
- HDOJ4006 The kth great number 【串的更改和维护】
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- HDU 4006 The kth great number 优先队列、平衡树模板题(SBT)
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- [LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...
- Lintcode: Kth Prime Number (Original Name: Ugly Number)
Ugly number is a number that only have factors 3, 5 and 7. Design an algorithm to find the kth numbe ...
- 排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix
[抄题]: 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. [思维问题]: 不知道应该怎么加,因为不是一维单调的. [一句话思路]: 周围两个数给x或y挪一 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...
随机推荐
- s:select 标签中list存放map对象的使用
1.XXXAction.java private List<Map<String, String>> maptest = null; public List<Map< ...
- RecyclerView源码分析(一)--整体设计
RecyclerView这个控件出来已经有一段时间了,如果看这篇文章的你,还没有使用过这个控件.那请先去学习怎样使用.不然看也白看.这里奉上一些关于介绍RecyclerView使用方法的优秀博客: 鸿 ...
- is-subsequence
public class Solution { public boolean isSubsequence(String s, String t) { int idx = 0; for (int i=0 ...
- LoadTestAgentResultsLateException in VS2010
遇到报错, 首先得先仔细读读人家给的出错信息. 而不是先怀疑是自己什么地方弄错了, 胡乱修改. 比如说, 遇到下面的报错: LoadTestAgentResultsLateException R ...
- ItemsControl
<ItemsControl Grid.Row=" ItemsSource="{Binding Content.patientInfoList}" Width=&qu ...
- Jmeter-Maven-Plugin高级应用:Modifying Properties
Modifying Properties Pages 12 Home Adding additional libraries to the classpath Advanced Configurati ...
- Linux内核设计基础(四)之虚拟文件系统
先来看一下写文件函数write的运行过程: ret = write(fd, buf, len); write适用于各种文件系统.它首先运行sys_write(),而正是这个sys_write()进行实 ...
- Silverlight 之 创建
Silverlight 项目文件是您可以使用不同工具来创建和编辑的文本文件.例如,可以使用 Visual Studio 2010 以及 Expression Blend 来创建 Silve ...
- C#中相关结构的用法及用途
C#中Dictionary的用法及用途 http://www.cnblogs.com/linzheng/archive/2010/12/13/1904709.html C#中的Dictionary字典 ...
- Linux下串口操作之数据拼接
串口操作中,特别以非阻塞的方式读取和发送数据,做好进程之间的同步很重要.有时我们会发现这样一个问题,在进行read操作时,一次read不能获得一个完整的数据帧,这就好比你买了一个电脑,送货的先把显示器 ...