题解 | 复数集合

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

struct Complex{
    int real;
    int imag;
    Complex(int a,int b):real(a),imag(b){}
    bool operator < (Complex c) const{
        return real*real+imag*imag <c.real*c.real +c.imag*c.imag;
    }
};

int main(){
    int n;
    while(cin>>n){
        priority_queue<Complex> p;
        for(int i=0;i<n;i++){
            string s;
            cin>>s;
            if(s=="Pop"){
                if(p.empty())cout<<"empty"<<endl;
                else {
                    cout<<p.top().real<<"+i"<<p.top().imag<<endl;
                    p.pop();
                    cout<<"SIZE = "<<p.size()<<endl;
                }
            }else {
                int a,b;
                scanf("%d+i%d",&a,&b);
                p.push(Complex(a,b));
                cout<<"SIZE = "<<p.size()<<endl;
            }
        }
    }
}

本题需要使用优先队列实现,且要对小于号进行重定义,因为这是个结构体

全部评论

相关推荐

09-22 09:42
门头沟学院 Java
牛客37185681...:马德,我感觉这是我面过最恶心的公司,一面是两个女hr,说什么实习前几个月属于试用期,试用期过了才能转成正式实习生,我***笑了,问待遇就是不说,问能不能接受全栈,沙币公司
如果可以选,你最想去哪家...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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