The kth great number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 9407    Accepted Submission(s): 3752

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大数就好了;
代码:
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<set>
#include<vector>
#define INF 0x3f3f3f3f
using namespace std; struct Node{
int v;
friend bool operator < (Node a, Node b){
return a.v > b.v;
}
};
multiset<Node>st;
multiset<Node>::iterator iter; int main(){
int n, k;
char s[];
Node d;
while(~scanf("%d%d", &n, &k)){
st.clear();
int ans = 0x3f3f3f3f;
for(int i = ; i < n; i++){
scanf("%s", s);
if(s[] == 'I'){
scanf("%d", &d.v);
st.insert(d);
iter = st.end();
iter--;
if(st.size() > k)
st.erase(iter);
}
else{
iter = st.end();
iter--;
printf("%d\n", *iter);
}
}
}
return ;
}

还可以用vector;

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>vec;
vector<int>::iterator iter;
int main(){
int n, k;
char s[];
while(~scanf("%d%d", &n, &k)){
vec.clear();
for(int i = ; i < n; i++){
scanf("%s", s);
int v;
if(s[] == 'I'){
scanf("%d", &v);
iter = lower_bound(vec.begin(), vec.end(), v);
vec.insert(iter, v);
}
else{
printf("%d\n", vec[vec.size() - k]);
}
} }
return ;
}

The kth great number(set)的更多相关文章

  1. The kth great number(优先队列)

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

  2. HDU 4006 The kth great number (优先队列)

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

  3. 4.Single Number && Single Number (II)

    Single Number: 1. Given an array of integers, every element appears twice except for one. Find that ...

  4. PAT 甲级 1019 General Palindromic Number(20)(测试点分析)

    1019 General Palindromic Number(20 分) A number that will be the same when it is written forwards or ...

  5. Python3 数字Number(六)

    Python 数字数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变数字数据类型得值,将重新分配内存空间. 以下实例在变量赋值时 Number 对象将被创建: var1 = 1 var ...

  6. The Luckiest number(hdu2462)

    The Luckiest number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. BZOJ 3000: Big Number (数学)

    题目: https://www.lydsy.com/JudgeOnline/problem.php?id=3000 题解: 首先n很大,O(n)跑不过,那么就要用一些高端 而且没听过 的东西——sti ...

  8. 九度OJ 1040:Prime Number(质数) (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5278 解决:2180 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...

  9. 【CF1017C】The Phone Number(构造)

    题意:要求构造一个1-n的排列,使得它的LIS+LDS最小 n<=1e5 思路:一个百度之星时候从LYY处听来的结论:1-n随机排列的LIS期望是根号级别的 考虑将LIS与LDS都构造成根号级别 ...

随机推荐

  1. UVa 10701 - Pre, in and post

    题目:已知树的前根序,中根序遍历转化成后根序遍历. 分析:递归,DS.依据定义递归求解就可以. 前根序:根,左子树,右子树: 中根序:左子树,根,右子树: 每次,找到根.左子树.右子树,然后分别递归左 ...

  2. UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释

    转自:http://blog.csdn.net/meegomeego/article/details/39890385 layoutSubviews总结 ios layout机制相关方法 - (CGS ...

  3. redis主从 哨兵

    entinel是redis高可用的解决方案,sentinel系统(N个sentinel实例,N >= 1)可以监视一个或者多个redis master服务,以及这些master服务的所有从服务: ...

  4. 使用VTemplate模板引擎动态生成订单流程图

    1.VTemplate模板引擎的简介 VTemplate模板引擎也简称为VT,是基于.NET的模板引擎,它允许任何人使用简单的类似HTML语法的模板语言来引用.NET里定义的对象.当VTemplate ...

  5. 如何让网页打开就运行JS代码,不用onclick

    打开网页直接运行是要调用window.onload( )函数: <html>    <head>    </head>    <body>    < ...

  6. (转).net控件dropdownlist动态绑定数据

    DropDownList控件的使用(数据绑定)(.net学习笔记二)(2006-10-12 07:28:49) 转载   分类:.net学习笔记 一.在页面初始化时候将集合绑定到DropDownLis ...

  7. 华为oj 计算字符个数

    练手而已 #include <stdio.h> #include <string.h> int main(void) { char string[200]={'\0'}; in ...

  8. iOS的属性声明:retain和strong的区别

    声明属性时用strong或者retain效果是一样的(貌似更多开发者更倾向于用strong).不过在声明Block时,使用strong和retain会有截然不同的效果.strong会等于copy,而r ...

  9. 浅谈Block传值-匿名函数(代码块)

    block传值是从后往前传值---代理也是 // 使用block时, 不能访问self, 也不能直接访问属性, self.属性, 用self调用方法; 只要这样做了, block都会对其强引用一份, ...

  10. win8 Pro 64位在 UEFI模式下Ghost系统 备份 恢复

    一:在win8 安装U 盘中  1. 新建 “Ghost” 文件夹 2. 将下载的Ghost64.exe 文件拷贝到文件夹  二: 启动的时候 按下F12 选择 HDDUSB 1.Windows 安装 ...