avatar
fireworks99
keep hungry keep foolish

模拟 The kth great number HDU4006

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).

如果每次Q就sort排序一次会TLE,那么就先用vector存下一个数,每次存数时按大小插入

vector 的 insert

v.insert(v.begin()+2,1);//在迭代器中第二个元素插入新元素

lower_bound 与 upperbound

ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置。

ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中第一个大于val的位置。

vector版

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int n, k;
    while(~scanf("%d%d", &n, &k))
    {
        getchar();
        vector<int> vec;
        for(int i = 0; i < n; ++i)
        {
            char c;
            scanf("%c", &c);
            switch(c)
            {
            case 'I':
                int tem;
                scanf("%d", &tem);
                vec.insert(lower_bound(vec.begin(), vec.end(), tem), tem);
                getchar();
                break;
            case 'Q':
                getchar();
                cout << *(vec.end() - k) << '\n';
            }
        }
    }
    return 0;
}

multiset版

#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
    int n, k;
    while(~scanf("%d%d", &n, &k))
    {
        getchar();
        multiset<int> m;
        for(int i = 0; i < n; ++i)
        {
            char ch;
            scanf("%c", &ch);
            switch(ch)
            {
            case 'I':
                int tem;
                scanf("%d", &tem);
                m.insert(tem);
                getchar();
                if(m.size() > k)
                    m.erase(m.begin());
                break;
            case 'Q':
                getchar();
                cout << *m.begin() << '\n';
            }
        }
    }
    return 0;
}

priority_queue版

#include <functional>
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

int main()
{
    int n, k;
    while(~scanf("%d%d", &n, &k))
    {
        getchar();
        priority_queue<int, vector<int>, greater<int> > q;
        for(int i = 0; i < n; ++i)
        {
            char c;
            scanf("%c", &c);
            switch(c)
            {
            case 'I':
                int tem;
                scanf("%d", &tem);
                q.push(tem);
                getchar();
                if(q.size() > k)
                    q.pop();
                break;
            case 'Q':
                getchar();
                cout << q.top() << '\n';
            }
        }
    }
}
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy