The kth great number(set)

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[2];
    Node d;
    while(~scanf("%d%d", &n, &k)){
        st.clear();
        int ans = 0x3f3f3f3f;
        for(int i = 0; i < n; i++){
            scanf("%s", s);
            if(s[0] == ‘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 0;
}

还可以用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[2];
    while(~scanf("%d%d", &n, &k)){
        vec.clear();
        for(int i = 0; i < n; i++){
            scanf("%s", s);
            int v;
            if(s[0] == ‘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 0;
}
时间: 2024-08-29 06:15:58

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

hdu 4006 The kth great number (优先队列)

优先队列头文件 #include <queue> 默认优先为从大到小优先. 自定义优先级 1 struct cmpmin{ //按从小到大 2 3 // 因为标准库默认使用元素类型的<操作符来确定它们之间的优先级关系. 4 bool operator ()(int &a, int &b){ 5 return a>b; //所以规定小的元素的优先级大于大的元素. 6 } 7 }; 8 9 struct cmpmax{ //按从大到小 10 bool operator

HDU 4006 The kth great number (基本算法-水题)

The kth great number 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 mu

hdu 4006 The kth great number (优先队列+STB+最小堆)

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 6637    Accepted Submission(s): 2671 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a roun

HDU 4006 The kth great number(multiset(或者)优先队列)

题目 询问第K大的数 //这是我最初的想法,用multiset,AC了——好吧,也许是数据弱也有可能 //multiset运用——不去重,边插入边排序 //iterator的运用,插入的时候,如果是相同的数没直接放在相同的现有的数据后面的 #include<cstdio> #include<cstring> #include<algorithm> #include<set> using namespace std; //#define IN freopen(

hdu 5623 KK&#39;s Number(dp)

问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗10?4??)个数,每次KK都会先拿数.每次可以拿任意多个数,直到NN个数被拿完.每次获得的得分为取的数中的最小值,KK和对手的策略都是尽可能使得自己的得分减去对手的得分更大.在这样的情况下,最终KK的得分减去对手的得分会是多少? 输入描述 第一行一个数T\left( 1\leq T\leq 10\right)T(1≤T≤10),表示数据组

LeetCode——Single Number(II)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm shou

HDU 1018 Big Number (数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 解题报告:输入一个n,求n!有多少位. 首先任意一个数 x 的位数 = (int)log10(x) + 1; 所以n!的位数 = (int)log10(1*2*3*.......n) + 1; = (int)(log10(1) + log10(2) + log10(3) + ........ log10(n)) + 1; 1 #include<cstdio> 2 #include<cs

HDU 3943 K-th Nya Number(数位dp+二分)

Problem Description Arcueid likes nya number very much. A nya number is the number which has exactly X fours and Y sevens(If X=2 and Y=3 , 172441277 and 47770142 are nya numbers.But 14777 is not a nya number ,because it has only 1 four). Now, Arcueid

JS中六种数据类型(四)——Number (转)

Number类型应该是ECMAScript中最令人关注的数据类型了,这种类型使用IEEE754格式来表示整数和浮点数值(浮点数值在某些语言中也被称为双精度数值).为支持各种数值类型,ECMA-262定义了不同的数值字面量. 最基本的数值字面量格式是十进制整数,十进制整数可以像下面这样直接在代码中输入: var  item =55;   //整数 除了以十进制表示外,整数还可以通过八进制(以8为基数)或十六进制(以16为基数)的字面值来表示.其中,八进制字面值的第一位必须是零(0),然后是八进制数