很多很多数,怎么求最小的100个
我想到的办法是给他们排序,这是不是计算量太大呀,更好的方法是什么 谢谢
程序代码:#include <stdio.h>
#include <queue>
using namespace std;
struct Node
{
int a;
bool operator<(const Node &node1)const
{return node1.a<a;}
};
int main()
{
int n,k;
Node t;
char c;
priority_queue<Node> pq;
while(EOF != scanf("%d%d",&n,&k))
{
while(!pq.empty())pq.pop();
getchar();
while(n--)
{
scanf("%c",&c);
getchar();
if('I' == c)
{
scanf("%d",&t.a);
getchar();
if(pq.size() < k)
pq.push(t);
else if(t.a>(pq.top()).a)
{
pq.pop();
pq.push(t);
}
}
else if('Q' == c)
{
printf("%d\n",(pq.top()).a);
}
}
}
return 0;
}