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. tomcat下配置https环境

    在网上搜了一下,内容不是非常完好. 现进行整理,做个学习笔记,以备以后使用. (1)进入到jdk下的bin文件夹 (2)输入例如以下指令"keytool -v -genkey -alias ...

  2. php缓存方案

    一.说说Memcached优化方案 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据 ...

  3. 利用JConsole工具监控java程序内存和JVM

    一.找到java应用程序对应的进程PI 性能测试应用程序访问地址:http://192.168.29.218:7070/training/ 部署的应用服务器为tomcat6.028 启动tomcat服 ...

  4. ARM体系结构_DAY2

    程序状态寄存器(CPSR) Mode位[4:0]:处理器模式为 USER模式不能直接切换到特权模式,在特权模式下可以直接修改mode位[4:0]为10000,切换到USER模式. T bit位[5]: ...

  5. C#在局域网中连接Liunx上的MySql数据库

    前期准备工作: 我所用的平台是VS2010和Ubuntu 14.04.3  LTS 一.由于MySql并没有集成在VS2010中所以要先安装MySQL Connector Net 6.9.8连接工具, ...

  6. linux下mysql环境支持中文配置步骤

    sql脚本执行前加上: CREATE DATABASE IF NOT EXISTS mydatabase DEFAULT CHARSET utf8 COLLATE UTF8_GENERAL_CI; u ...

  7. socket网络编程中的同步,异步,阻塞式,非阻塞式,有何联系与区别?

    一.举个打电话的例子: 阻塞   block   是指,你拨通某人的电话,但是此人不在,于是你拿着电话等他回来,其间不能再用电话.同步大概和阻塞差不多. 非阻塞   nonblock   是指,你拨通 ...

  8. VS2015试验随手记

    1.第一次安装时,未完整安装,没有安装MFC,导致可以创建MFC工程,但是不能编译 解决办法,修改安装,加入MFC 2.学习创建windows runtime component,第一次使用,可以得到 ...

  9. Mysql学习(慕课学习笔记7)修改数据表(下)

    添加主键约束 ALTER TABLE tb1_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,…….) ...

  10. R语言的数据结构

    首先声明,R语言对大小写敏感. 一.向量 vector类型可以存储数字.字符和逻辑类型.构建函数为C(): > a <- c(1,2,3,4) > b <- c('dau',' ...