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 ...
随机推荐
- JAVA复制文件最快的算法
/** * 复制文件 * * @param srcFile * 源文件File * @param destDir * 目标目录File * @param newFileName * 新文件名 * @r ...
- version control system:git/hg/subversion/cvs/clearcase/vss。software configruation management。代码集成CI:Cruisecontrol/hudson/buildbot
version control system: git/hg/subversion/cvs/clearcase/vss software configruation management: daily ...
- MapReduce模式MapReduce patterns
After having modified and run a job in the last post, we can now examine which are the most frequent ...
- Protobuf 语法指南
英文: Proto Buffers Language Guide 本指南描述了怎样使用protocol buffer 语法来构造你的protocol buffer数据,包括.proto文件语法以及怎样 ...
- 【BestCoder】【Round#42】
模拟+链表+DP Orz AK爷faebdc A Growin要跟全部的n个人握手共2n杯香槟,再加上每对关系的两杯香槟,直接统计邻接矩阵中1的个数,再加2n就是answer //BestCoder ...
- 【BZOJ】【2730】【HNOI2012】矿场搭建
Tarjan求BCC/割点 然而似乎我一开始抄的白书的板子哪里抄错了?还是本身哪里不对……(可能是不适用于这道题?因为这题要求求出每个BCC的大小..? 膜拜了ydc的写法= = 其实两次dfs也并没 ...
- C++ string 字符串查找匹配
在写C++程序中,总会遇到要从一个字符串中查找一小段子字符串的情况,对于在C中,我们经常用到strstr()或者strchr()这两种方法.而对于C++的string,我们往往会用到find(). C ...
- UVA 156 (13.08.04)
Ananagrams Most crossword puzzle fans are used to anagrams--groupsof words with the same letters i ...
- 7 个 Bootstrap 在线编辑器用于快速开发响应式网站
Bootstrap 已经使响应式网站开发变得简单很多. 但是如果你不必手动写全部代码,事情会如何呢? 如果你可以自由地选择你想要使用的Bootstrap 组件.并可以把它们拖拽到画布中,事情会如何呢? ...
- 【Java VisualVM】使用 VisualVM 进行性能分析及调优
转载:https://blog.csdn.net/lmb55/article/details/79267277 一.概述 开发大型 Java 应用程序的过程中难免遇到内存泄露.性能瓶颈等问题,比如文件 ...