The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest &&The kth great number


http://acm.hdu.edu.cn/showproblem.php?pid=4006


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
这道题有点下技巧,就是用了STL中multiset容器,可以自动从小到大排序,而且可以允许每次都插入相同的元素,还有就是每次要保证容器中有k个元素,

代码1:

#include<iostream>
#include<set>
using namespace std;
int n,m;
int main()
{   while(cin>>n>>m)
      { multiset<int> p;
          char c;
          int a;
           for(int i=0;i<n;i++)
           {  cin>>c;
            if(p.size()>m) p.erase(p.begin());
              if(c=='I') 
               {  cin>>a;
                 p.insert(a);
                }
                else  cout<<*p.begin()<<endl;
                }
                }return 0;
}

代码二:

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int n,m;
int main()
{   while(cin>>n>>m)
      {  priority_queue<int,vector<int>,greater<int> >p;
          for(int i=0;i!=n;i++)
         {   char c;
            cin>>c;
            if(c=='I')
            {  int t;
               cin>>t;
               p.push(t);
            if(p.size()>m) p.pop();
            }
            else cout<<p.top()<<endl;
            }
         }return 0;
}


你可能感兴趣的:(The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest &&The kth great number)