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

Problem Description
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.
 
Input
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.
 
Output
The output consists of one integer representing the largest number of islands that all lie on one line.
 
Sample Input
8 3 I 1 I 2 I 3 Q I 5 Q I 4 Q
 
Sample Output
1 2 3

Hint

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

 题意: 大致是输入一些数,这个过程中不断询问第k大的数是多少?
因而普通的排序是不行的,所以很好定义为最小堆,最大堆的求解...
但是对堆的求解也有两种,第一种是构造一个k堆,然后再输入数据,不断更新维护这个k堆,我们暂时叫他第k堆吧...
这样的话,一此题给的sample  data  为列,
 
所以  红黑树来表示的话就是下面这个了...
为了这,STL  multiset....
代码:

 #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(最小堆...)的更多相关文章

  1. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  2. 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: [ ...

  3. Lintcode401 Kth Smallest Number in Sorted Matrix solution 题解

    [题目描述] Find the kth smallest number in at row and column sorted matrix. 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的 ...

  4. HDOJ4006 The kth great number 【串的更改和维护】

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

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

  6. [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 ...

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

  8. 排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix

    [抄题]: 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. [思维问题]: 不知道应该怎么加,因为不是一维单调的. [一句话思路]: 周围两个数给x或y挪一 ...

  9. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. C++ Primer 学习笔记_72_面向对象编程 --句柄类与继承[续]

    面向对象编程 --句柄类与继承[续] 三.句柄的使用 使用Sales_item对象能够更easy地编写书店应用程序.代码将不必管理Item_base对象的指针,但仍然能够获得通过Sales_item对 ...

  2. Guava Files 源码分析(二)

    createTempDir()之后就没有什么有意思的函数了,基本上都是对Java IO函数操作的聚合,只看一个simplifyPath() /** * Returns the lexically cl ...

  3. Android安卓手机游戏开发

    成都传智播客Java培训,免费学Android安卓手机游戏开发,安卓android开发课程包括Android安卓应用开发和Android安卓游戏开发两个方向,可是偏向游戏开发. 依据"199 ...

  4. 数据库实例: STOREBOOK > 表空间 > 编辑 表空间: USERS

    ylbtech-Oracle:数据库实例: STOREBOOK  >  表空间  >  编辑 表空间: USERS 表空间  >  编辑 表空间: USERS 1. 一般信息返回顶部 ...

  5. OpenCV学习(21) Grabcut算法详解

    grab cut算法是graph cut算法的改进.在理解grab cut算之前,应该学习一下graph cut算法的概念及实现方式. 我搜集了一些graph cut资料:http://yunpan. ...

  6. Mybatis映射实体改造和异常问题

    现在WEB开发经常使用 Mybatis 作为持久化框架,在开发过程中,会在Java代码中构建实体类与数据库表字段相互映射, 下面提出一个关于映射实体优化的方案:通过链式编程实现给实例对象赋值. 参考代 ...

  7. C语言文件打开方式及说明

    ANSI C规定文件打开用函数fopen,关闭为fclose. 1.调用方式通常为: FILE *fp; fp=fopen(文件名, 打开方式);   2.参数说明: 文件名: 形如"myf ...

  8. CSS 过滤器 兼容ie,火狐和谷歌

    这篇汇总主要是提供一些CSS不透明的详细介绍,代码示例和解释,以实现这项有用的CSS技术在您的项目中兼容所有浏览器. 关于CSS 透明度,有一点需要注意的是,它虽然使用了很多年,但它一直以来都不是一个 ...

  9. 用C#代码编写的SN快速输入工具

    一般软件都要输入序列号(SN),而大家平时用的最多的恐怕是盗版软件,通常盗版软件的序列号(SN)都保存成:XXXXX-XXXXX-XXXX-XXXX的形式. 而软件输入序列号的地方通常都是几个文本框( ...

  10. NYOJ-267 郁闷的C小加(二)

    郁闷的C小加(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考“郁闷的C小加(一)”),C小加很 ...