-
数据结构实验之排序四:寻找大富翁
- 学习一下
- 重载运算符形式
-
struct cmp { bool operator()(Node a, Node b) { if(a.x == b.x) return a.y>b.y; return a.x>b.x; } } priority_queue<Node, vector<Node>, cmp>p;
- 本题就是小顶堆维护:
-
#include<bits/stdc++.h> using namespace std; int n,m,x,cnt; vector<int>a; int main() { ios::sync_with_stdio(false); priority_queue<int,vector<int>,greater<int> >q; cin>>n>>m; while(n--) { cin>>x; if(cnt<m) { q.push(x); cnt++; } else { if(x>q.top()) { q.pop(); q.push(x); } } } while(m--) { a.push_back(q.top()); q.pop(); } for(int i=cnt-1; i>=0; i--) if(i==0) cout<<a[i]<<endl; else cout<<a[i]<<" "; return 0; }
熟练优先队列声明格式
最新推荐文章于 2024-03-20 18:49:01 发布