2011.网研院.Problem B.虚数
Description
给你一个复数集合{Aj+i*Bj},保证Aj和Bj都是整数,初始为空集。
每次会给你如下两种操作中的一种:
1.“Insert x+iy”,其中x,y都是整数。表示在集合中加入一个复数 x+iy,同时输出此时集合的大小;
2.“Pop”。如果集合为空集直接返回“Empty!”,如果有元素则以”x+iy”的形式显示集合中模值最大的复数,然后将该元素从集合中删除,之后在第二行显示操作之后的集合大小,如果为空集则显示“Empty!”。
Input
第一行只有一个数T,代表case数。0<=T<=10
每一组case:
第一行有一个整数n,表示这组case中一共有n条命令 0<n<=100
接下来n行每行有一个命令,命令如上所述
保证不会输入两个模值同样的元素,并保证实部虚部都大于0,小于1000。
Output
依照上述原则输出每一个命令对应的输出
如果输入命令是Insert命令,则对应的输出占一行为集合大小;
如果输入命令是Pop命令,则对应的输出占一行或者两行,为模值最大的复数和集合大小。
请注意,输出集合大小的格式为”Size:空格x回车”,x为集合大小
Sample Input
1
5
Pop
Insert 1+i2
Insert 2+i3
Pop
Pop
Sample Output
Empty!
Size: 1
Size: 2
2+i3
Size: 1
1+i2
Empty!
#include<bits/stdc++.h>
using namespace std;
struct comp {
int shi;
int xu;
double res;
friend bool operator < (comp a, comp b) {
return a.res < b.res;
}
};
int main() {
int t,n;
string op;
int res;
cin>>t;
while(t--) {
priority_queue<comp> s;
comp tmp;
cin>>n;
while(n--) {
cin>>op;
if(op=="Insert") {
scanf("%d+i%d",&tmp.shi,&tmp.xu);
tmp.res=sqrt(pow(double(tmp.shi),2)+pow(double(tmp.xu),2));
s.push(tmp);
printf("Size: %d\n",s.size());
} else if(op=="Pop") {
if(s.empty()) {
printf("Empty!\n");
} else {
tmp=s.top();
s.pop();
printf("%d+i%d\n", tmp.shi, tmp.xu);
if(s.empty()) {
printf("Empty!\n");
} else {
printf("Size: %d\n",s.size());
}
}
}
}
}
return 0;
}
还没有评论,来说两句吧...