HDOJ-3785 寻找大富翁(优先队列)
生活随笔
收集整理的這篇文章主要介紹了
HDOJ-3785 寻找大富翁(优先队列)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
尋找大富翁
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2248????Accepted Submission(s): 1081
?
Input 輸入包含多組測(cè)試用例.每個(gè)用例首先包含2個(gè)整數(shù)n(0<n<=100000)和m(0<m<=10),其中: n為鎮(zhèn)上的人數(shù),m為需要找出的大富翁數(shù), 接下來(lái)一行輸入鎮(zhèn)上n個(gè)人的財(cái)富值.
n和m同時(shí)為0時(shí)表示輸入結(jié)束.
?
Output 請(qǐng)輸出烏鎮(zhèn)前m個(gè)大富翁的財(cái)產(chǎn)數(shù),財(cái)產(chǎn)多的排前面,如果大富翁不足m個(gè),則全部輸出,每組輸出占一行.?
Sample Input 3 1 2 5 -1 5 3 1 2 3 4 5 0 0?
Sample Output 5 5 4 3?
Source 浙大計(jì)算機(jī)研究生復(fù)試上機(jī)考試-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; // 默認(rèn)就是從大到小排序的 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 }?
轉(zhuǎn)載于:https://www.cnblogs.com/dongsheng/p/3162732.html
總結(jié)
以上是生活随笔為你收集整理的HDOJ-3785 寻找大富翁(优先队列)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: oracle系统包—-dbms_outp
- 下一篇: Linux Mysql 1130错误解决