寻找大富翁

题目链接
优先队列

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int arr[maxn];
int main(){
	int n,m,x;
	priority_queue<int, vector<int>, greater<int> > pq;
	while(cin>>n>>m){
		while(!pq.empty()){
			pq.pop();
		}
		for(int i=0;i<n;i++){
			cin>>x;
			if(pq.size() < m) pq.push(x);
			else if(pq.top() < x){
				pq.pop();
				pq.push(x);
			}
		}
		vector<int> ans;
		while(!pq.empty()){
			ans.push_back(pq.top());
			pq.pop();
		}
		reverse(ans.begin(), ans.end());
		for(int i=0;i<ans.size();i++){
			cout<<ans[i];
			if(i!=ans.size()-1) cout<<" ";
			else cout<<endl;
		}
	}
	
	
	return 0;
} 
全部评论

相关推荐

01-15 22:54
武汉大学 Java
点赞 评论 收藏
分享
优秀的大熊猫在okr...:多益:此贼,必有同谋,按律,该当连坐!
你不能接受的企业文化有哪...
点赞 评论 收藏
分享
2025-12-09 16:27
门头沟学院 Java
迷茫的大四🐶:我追的看不上我,追我的我看不上
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务