HDOJ-3785 寻找大富翁(优先队列)
生活随笔
收集整理的這篇文章主要介紹了
HDOJ-3785 寻找大富翁(优先队列)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
尋找大富翁
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2248????Accepted Submission(s): 1081
?
Input 輸入包含多組測試用例.每個用例首先包含2個整數n(0<n<=100000)和m(0<m<=10),其中: n為鎮上的人數,m為需要找出的大富翁數, 接下來一行輸入鎮上n個人的財富值.
n和m同時為0時表示輸入結束.
?
Output 請輸出烏鎮前m個大富翁的財產數,財產多的排前面,如果大富翁不足m個,則全部輸出,每組輸出占一行.?
Sample Input 3 1 2 5 -1 5 3 1 2 3 4 5 0 0?
Sample Output 5 5 4 3?
Source 浙大計算機研究生復試上機考試-2009年 1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 7 using namespace std; 8 9 void search(int n, int m) 10 { 11 priority_queue <int> q; // 默認就是從大到小排序的 12 int t; 13 for(int i = 0; i < n; ++i) 14 { 15 scanf("%d", &t); 16 q.push(t); 17 } 18 m = m > n ? n : m; 19 for(int i = 0; i < m-1; ++i) 20 { 21 printf("%d ", q.top()); 22 q.pop(); 23 } 24 printf("%d\n", q.top()); 25 } 26 27 int main() 28 { 29 int n, m; 30 while(scanf("%d%d", &n, &m), m||n) 31 { 32 search(n, m); 33 } 34 return 0; 35 }?
轉載于:https://www.cnblogs.com/dongsheng/p/3162732.html
總結
以上是生活随笔為你收集整理的HDOJ-3785 寻找大富翁(优先队列)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle系统包—-dbms_outp
- 下一篇: TopFreeTheme精选免费模板【2