7-6 寻找大富翁 (25分)【堆排序,优先队列,less从大到小greater从小到大】

    科技2025-02-01  37

    优先队列的less可以把输入的数由大到小进行排列 优先队列的定义

    priority_queue<int,vector< int>,less< int>> q;

    优先队列的输入

    while(n–) { cin>>item; q.push(item); }

    取头删头

    item=Q.top(); Q.pop();

    注意循环是while(1)!!!在这卡了好久一开始用的是n&t, 但是n在输入的时候已经变成0了,低级错误,大意了,不过换了一个变量t也不对,我就很怀疑人生,,,之后朋友说改成1,就AC了…1也挺好的,反正肯定会return住,考虑好久while条件,不如直接无限循环嗳

    #include<iostream> #include<queue> using namespace std; priority_queue<int,vector<int>,less<int>>Q; int main(){ int n,m; cin>>n>>m; int t=n; int item; while(n--){ //输入 scanf("%d",&item); Q.push(item); } while(1) { item=Q.top(); Q.pop(); m--; t--; if(m==0||t==0){ cout<<item; return 0; } else cout<<item<<" "; } return 0; } /* 8 3 8 12 7 3 20 9 5 18 */
    Processed: 0.018, SQL: 8