The output consists of one integer representing the largest number of islands that all lie on one line.
用set或者priority_queue维护长度为k从小到大有序的序列,每次查询的时候首元素即为所求。
#include <cstdio>
#include <set>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
multiset<int> ms;
int main()
{
int n, k, x;
char ch[10];
while(~scanf("%d%d", &n, &k)) {
ms.clear();
while(n--) {
scanf("%s", ch);
if(ch[0]=='I') {
scanf("%d",&x);
ms.insert(x);
if(ms.size()>k) {
ms.erase(ms.begin());
}
} else {
printf("%d\n", *ms.begin());
}
}
}
return 0;
}
#include <cstdio>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
priority_queue<int,vector<int>, greater<int> > pq;
int main()
{
int n, k, x;
char ch[10];
while(~scanf("%d%d", &n, &k)) {
while(!pq.empty()) pq.pop();
while(n--) {
scanf("%s", ch);
if(ch[0]=='I') {
scanf("%d",&x);
pq.push(x);
if(pq.size()>k) {
pq.pop();
}
} else {
printf("%d\n", pq.top());
}
}
}
return 0;
}
hdu 4006 The kth great number,set,priority_queue
原文:http://blog.csdn.net/yew1eb/article/details/39479243