优先队列 题解 | #复数集合#

复数集合

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

#include <iostream>
#include <queue>
using namespace std;

struct Cx{
    int a, b;
    bool operator< (const Cx& C) const{
        int s1 = a * a + b * b, s2 = C.a * C.a + C.b * C.b;
        if(s1 != s2) return s1 < s2;
        return b > C.b;
    }
};
priority_queue<Cx> q;

int main(){
    int n;
    string op;
    while(cin>>n){
        while(n--){
            cin>>op;
            if(op == "Pop"){
                if(!q.size()) puts("empty");
                else{
                    auto t = q.top();
                    q.pop();
                    printf("%d+i%d\n", t.a, t.b);
                    printf("SIZE = %d\n", q.size());
                }
            }else{
                int x, y;
                scanf("%d+i%d", &x, &y);
                q.push({x, y});
                printf("SIZE = %d\n", q.size());
            }
        }
    }
    
    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务