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. Android Studio新手全然指引

    Android Studio新手全然指引 @author ASCE1885的 Github 简书 微博 CSDN Android Studio的下载及安装 假设你的电脑能够FQ,那么请直接到Andro ...

  2. Spring redirect直接返回项目根文件夹

    return "redirect:/";

  3. jquery获取当前元素坐标

    1. jquery获取当前元素坐标 A) 获取对象

  4. Windows Server 2008搭建域控制器《转载51CTO.com》

    Windows Server 2008搭建域控制器 引入 在小型网络中,管理员通常独立管理每一台计算机,如最为常用的用户管理.但当网络规模扩大到一定程度后,如超过 10 台计算机,而每台计算机上有 1 ...

  5. Android调试系列—使用android studio调试smali代码

    1.工具介绍 使用工具 android killer:用于反编译apk包,得到smali代码 android studio:调试smali代码工具,或者使用idea,android studio就是在 ...

  6. post请求和get请求的区别

    1:如果表单是以post方式发送,那么表单中的数据会放在请求报文体中,发送到服务端.但是如果是以get方式提交表单,那么表单中用户输入的数据都是以URL地址的方式发送到服务端. 2:在服务端接收数据时 ...

  7. sqlserver插入datetime

    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")

  8. 找不到类型“IBatisService.boxManageService”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHostingEnvironment/serviceActivations 中提供。

    找不到类型“IBatisService.boxManageService”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/s ...

  9. 2 读取solr下的索引文件(lucene文件)

    import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnaly ...

  10. Cut the sticks

    def main(): n = int(raw_input()) arr = map(int, raw_input().strip().split()) for i in range(n): cutN ...