题解 | 结构体优先队列

结构体优先队列

https://www.nowcoder.com/practice/e3d68ce7184e4658b42740edd4308d51

#include<bits/stdc++.h>
using namespace std;

struct node{
    int chinese, math, english, sum;
};

bool operator<(node a, node b){
   if(a.sum != b.sum)
   {
    return a.sum < b.sum ;
   }
   else if(a.chinese != b.chinese)
   {
    return a.chinese < b.chinese ; 
   }
   else if(a.math != b.math)
   {
    return a.math < b.math ; 
   }
   else 
   {
    return a.english < b.english ; 
   }
}

priority_queue<node> s;
void insertValue(int chinese, int math, int english){
    s.push({chinese , math , english , chinese + math + english}) ;
}

void deleteValue(){
    s.pop() ;
}

node getTop(){
    return s.top() ; 
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q,op;
    int x, y, z;
    cin>>q;
    while(q--){
        cin>>op;
        if(op==1){
            cin>>x>>y>>z;
            insertValue(x, y, z);
        }
        if(op==2){
            node tmp = getTop();
            cout<<tmp.chinese<<" "<<tmp.math<<" "<<tmp.english<<endl;
        }
        if(op==3){
           deleteValue();
        }
    }
    return 0;
}

全部评论

相关推荐

2025-11-26 14:42
郑州轻工业大学 Java
在写周报的打工人很独...:这个笔试昨天晚上做了一下,真难啊,前后端,ai全有
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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