Problem Description
Problem Analysis
Actually, sorting would solve this easily
But since this is in the queue topic
let’s write it with a Priority Queue~
A Priority Queue gives higher priority to larger values by default
so this can be written very easily
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| #include<stdio.h>
#include<queue>
using namespace std;
int main() {
int n,m;
while(scanf("%d %d",&n,&m),n||m) {
priority_queue<int>q;
for(int i=0; i<n; i++) {
int t;
scanf("%d",&t);
q.push(t);
}
for(int i=0; i<m; i++) {
printf("%d",q.top());
q.pop();
if(i!=m-1)
printf(" ");
else
printf("\n");
}
}
return 0;
}
|
Problem link: 【HDU】[3785]Finding the Millionaire