#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct Tnode { int real;//实部 int vir;//虚部 int abs;//复试的模值 } Tnode; void adjust(Tnode* x, int y, int len) { //调整子树为大根堆 ,y是父亲位置 int dad = y; int son = dad * 2 + 1; while (son < len) { if (son + 1 < len && x[son].abs < x[son + 1].abs) { son++; } else if (son + 1 < len && x[son].abs == x[son + 1].abs) { if (x[son].real < x[son + 1].real) { son++; } } if (x[son].abs > x[dad].abs) { Tnode temp; temp = x[dad]; x[dad] = x[son]; x[son] = temp; dad = son; son = 2 * dad + 1; } else if (x[son].abs == x[dad].abs) { if (x[son].real > x[dad].real) { //儿子大于父亲 Tnode temp; temp = x[dad]; x[dad] = x[son]; x[son] = temp; dad = son; son = 2 * dad + 1; } } else { break; } } } void makebigheap(Tnode* x, int len) { //构造大根堆 for (int i = len / 2 - 1; i >= 0; i--) { adjust(x, i, len); } } int main() { int n; // memset(&a, 0,sizeof(Tnode)); while (scanf("%d\n", &n) != EOF) { Tnode a[1000];//大根堆数组 int num = 0; //堆内有几个元素 char tmp[20]; for (int i = 0; i < n; i++) { scanf("%s", &tmp); // gets(tmp); if (tmp[0] == 'P') { //pop指令操作 if (num == 0) { printf("empty\n"); continue; } else { num--; printf("%d+i%d\n", a[0].real, a[0].vir); printf("SIZE = %d\n", num); a[0] = a[num]; adjust(a, 0, num); continue; } } else { //insert指令操作 scanf("%d+i%d", &a[num].real, &a[num].vir); a[num].abs = a[num].real * a[num].real + a[num].vir * a[num].vir; num++; printf("SIZE = %d\n", num); makebigheap(a, num); continue; } } } }