日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 3785 寻找大富翁(sort排序或优先队列)

發(fā)布時(shí)間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 3785 寻找大富翁(sort排序或优先队列) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

?

浙江桐鄉(xiāng)烏鎮(zhèn)共有n個(gè)人,請(qǐng)找出該鎮(zhèn)上的前m個(gè)大富翁.

Input輸入包含多組測試用例.?
每個(gè)用例首先包含2個(gè)整數(shù)n(0<n<=100000)和m(0<m<=10),其中: n為鎮(zhèn)上的人數(shù),m為需要找出的大富翁數(shù), 接下來一行輸入鎮(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

?

解題報(bào)告:水題,排序即可,優(yōu)先隊(duì)列亦可解。

?

?

排序ac:

#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std;int a[100000+5]; int main() {int n,m;while(~scanf("%d%d",&n,&m)) {if(n==0&&m==0) break;memset(a,0,sizeof(a));for(int i = 1; i<=n; i++) {scanf("%d",&a[i]);}sort(a+1,a+n+1, greater<int>() );if(n<=m) {for(int i = 1; i<=n; i++) {printf("%d%c",a[i],i==n?'\n':' '); }continue;}if(n>m) {for(int i = 1; i<= m; i++) {printf("%d%c",a[i],i==m?'\n':' '); }}}return 0 ; }

事實(shí)證明不加if(n<=m)的情況 也能ac,看來是樣例中沒出這樣的樣例?不然依照題干應(yīng)該是有這一種情況發(fā)生啊。

?

優(yōu)先隊(duì)列ac:

#include<cstdio> #include<iostream> #include<queue> using namespace std; priority_queue<int,vector<int>,less<int> >q;//等價(jià)于priority_queue<int> q; ?從大到小排序? int main() {int n,t,a;while(scanf("%d %d",&n,&t)&&(n!=0||t!=0)){for(int i=0;i<n;i++){scanf("%d",&a);q.push(a);}for(int i=0;i<t-1;i++){printf("%d ",q.top());q.pop();}printf("%d",q.top());//最有一個(gè)數(shù)字沒有空格。printf("\n");//每次別忘清空隊(duì)列while(!q.empty()) q.pop();}return 0; } int n,t,a;while(scanf("%d %d",&n,&t)&&(n!=0||t!=0)){for(int i=0;i<n;i++){scanf("%d",&a);q.push(a);}for(int i=0;i<t-1;i++){printf("%d ",q.top());q.pop();}printf("%d",q.top());//最有一個(gè)數(shù)字沒有空格。printf("\n");//每次別忘清空隊(duì)列while(!q.empty()) q.pop();}return 0; }

優(yōu)先隊(duì)列ac2:(優(yōu)化了n<=m的情況)源自網(wǎng)絡(luò)

/* 當(dāng)輸入小于m個(gè)的時(shí)候,一直輸入,當(dāng)輸入大于m個(gè)數(shù)之后 ,這m個(gè)數(shù)會(huì)按照從小到大的順序排序, 再往里面輸入的時(shí)候要進(jìn)行判斷,如果那個(gè)數(shù)比對(duì)頂元素大的話,這個(gè)數(shù)進(jìn)隊(duì),對(duì)頂元素出對(duì), 進(jìn)隊(duì)后就又變成從小到大排序了,輸入完畢之后,將數(shù)據(jù)按從大到小輸出,因?yàn)閷?duì)頂元素是最小值, 所以,應(yīng)該將元素 都重新賦值到數(shù)組中,然后從后往前輸出!(就這個(gè)轉(zhuǎn)換方法必須想到!!!) */ #include <stdio.h> #include <queue> #include <algorithm> using namespace std; priority_queue<int,vector<int>,greater<int> >q; int a,b[100005]; int main() {int n,m,i,j,k;while(scanf("%d%d",&n,&m)&&(n||m)) { //n,m分別代表村中的總?cè)藬?shù),和最富有的前m個(gè)人for(i=0; i<n; i++) { //將財(cái)產(chǎn)值放到隊(duì)列中scanf("%d",&a);if(q.size()<m)q.push(a);else if(a>q.top()) {q.push(a);q.pop();}}if(n>m) { //當(dāng)總?cè)藬?shù)比富翁人數(shù)多的話執(zhí)行下面語句for(i=0; i<m; i++) {b[i]=q.top();q.pop();}for(i=m-1; i>0; i--)printf("%d ",b[i]);printf("%d\n",b[0]);} else { //總?cè)藬?shù)沒有富翁人數(shù)多的話,執(zhí)行這個(gè)下面的語句!for(i=0; i<n; i++) {b[i]=q.top();q.pop();}for(i=n-1; i>0; i--)printf("%d ",b[i]);printf("%d\n",b[0]);}}return 0; }

今天又用set實(shí)現(xiàn)了一下,,第一發(fā)錯(cuò)了,后來發(fā)現(xiàn)需要multiset

AC代碼:

#include<bits/stdc++.h>using namespace std; multiset<int,greater<int> > ss; int main() {int n,m;while(~scanf("%d%d",&n,&m) ) {if(n+m == 0) break; ss.clear();int tmp;for(int i =1; i<=n; i++) {scanf("%d",&tmp);ss.insert(tmp);}int cnt = 0;set<int, greater<int> > :: iterator it; for(it = ss.begin(); it!=ss.end(); it++) {cnt++;printf("%d%c",*it,cnt==m?'\n':' ');if(cnt == m) break;}}return 0 ; }

?

?

?

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的HDU 3785 寻找大富翁(sort排序或优先队列)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。