sicily 1022---可用于大数据排序查找中位数(非常快)--优先队列

#include <iostream>
#include <cstdio>
#include <vector> 
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;


struct Student
{
char name_[15];
int solve;
} stu;


struct Greater
{
bool operator() (Student a, Student b)
{
return a.solve < b.solve;
}
};


struct Less
{
bool operator() (Student a, Student b)
{
return a.solve > b.solve;
}
};


int main()
{
int T;
scanf("%d", &T);
while (T--)
{
priority_queue<Student, vector<Student>, Greater> MAX;
priority_queue<Student, vector<Student>, Less> MIN;
char str[20];
while (1)
{
scanf("%s", str);
if (strcmp(str, "Add") == 0)
{
scanf("%s%d", stu.name_, &stu.solve);
if(MAX.size() == MIN.size())
{
if (MAX.empty() || stu.solve < MIN.top().solve)
MAX.push(stu);
else if (stu.solve > MIN.top().solve)
{
MAX.push(MIN.top());
MIN.pop();
MIN.push(stu);
}
}
else if (MAX.size() > MIN.size())
{
if (stu.solve > MAX.top().solve)
MIN.push(stu);
else if (stu.solve < MAX.top().solve)
{
MIN.push(MAX.top());
MAX.pop();
MAX.push(stu);
}
}
}
else if (strcmp(str, "Query") == 0)
{
if (MIN.size() == MAX.size())
printf("No one!\n");
else
printf("%s\n", MAX.top().name_);
}
else if (strcmp(str, "End") == 0)
{
if (MIN.size() == MAX.size())
printf("Happy BG meeting!!\n");
else
printf("%s is so poor.\n",MAX.top().name_);
break;
}
}
if (T != 0)
printf("\n");
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值