HDU-3785 题解
寻找大富翁
题目大意
浙江桐乡乌镇共有n个人,找出该镇上的前m个大富翁。
Time: 1000 ms
Memory: 32768 kB
解题思路及分析
堆 STL优先队列
将数据放入堆中,再弹出m个最大值
AC代码
#include <bits/stdc++.h>
using namespace std;
int n, m;
int main()
{
while (~scanf("%d%d", &n, &m) && n && m)
{
priority_queue<int> q;
for (int i = 0; i < n; i++)
{
int x;
scanf("%d", &x);
q.push(x);
}
bool first = 1;
while (m-- && !q.empty())
{
if (first) first = 0;
else printf(" ");
printf("%d", q.top());
q.pop();
}
puts("");
}
return 0;
}