题意:
XX和YY玩一个游戏,有N轮。
每一轮执行操作:
XX在盒子里面放入ai个球(编号num,重量wgt)
YY从盒子里面取出bi个最重的球
问最后盒子里面最重的球的编号和重量
已经确定球的编号两两各不相同,球的重量两两各不相同
思路:
优先队列。
注意的地方:
自己定义的结构体用优先队列的时候需要重载大于号和小于号(指的是优先级的大小关系),注意两个const 必须加。
代码:
#include <cstdio>
#include <queue>
using namespace std;
struct Ball
{
int n, w;
Ball() {}
Ball(int _n, int _w): n(_n), w(_w) {}
bool operator > (const Ball &b) const { return w > b.w; }
bool operator < (const Ball &b) const { return w < b.w; }
};
int t, n, ai, bi, num, wgt;
Ball ans;
int main()
{
scanf("%d", &t);
while (t --)
{
priority_queue <Ball> Q;
scanf("%d", &n);
while (n --)
{
scanf("%d %d", &ai, &bi);
while (ai --)
{
scanf("%d %d", &num, &wgt);
Q.push(Ball(num, wgt));
}
while (bi --) Q.pop();
}
ans = Q.top();
printf("%d %d\n", ans.n, ans.w);
}
}