不需要全部有序的情况下使用堆的数据结构会更高效
#include <iostream>
#include <queue>
#include "queue"
using namespace std;
struct Complex {
int shibu;
int xubu;
Complex(int a, int b) {
shibu = a;
xubu = b;
}
bool operator< (Complex B)const {
if ((shibu * shibu + xubu * xubu) < (B.shibu * B.shibu + B.xubu * B.xubu))
return true;
else if ((shibu * shibu + xubu * xubu) == (B.shibu * B.shibu + B.xubu*
B.xubu)) {
return xubu > B.xubu;
} else return false;
}
};
int main() {
int n;
while (cin >> n) { // 注意 while 处理多个 case
// cout << a + b << endl;
priority_queue<Complex> myQueue;
while (n--) {
string order;
cin >> order;
if (order == "Pop") {
if (myQueue.empty()) cout << "empty" << endl;
else {
Complex biggest = myQueue.top();
myQueue.pop();
cout << biggest.shibu << "+i" << biggest.xubu << endl << "SIZE = " <<
myQueue.size() << endl;
}
} else {//命令为insert时
int sb, xb;
scanf("%d+i%d", &sb, &xb);
myQueue.push(Complex(sb, xb));
cout << "SIZE = " << myQueue.size() << endl;
}
}
}
}
// 64 位输出请用 printf("%lld")

京公网安备 11010502036488号