(1)题目描述
(2)算法思想
本题涉及到频繁的增删以及查询操作,必须先确定合适的数据结构,我一开始选用了vector并在每次增删后进行排序,不出所料超时只得了10分。
涉及到频繁增删应优先考虑红黑树结构的set与map,此处需要对商品信息进行排序,方便起见应选用set容器(map表示映射关系难以对类别、序号以及分数进行统一排序)
使用set存储商品信息需要重载"<",由题意可知优先级:得分>类别>序号,即得分高者靠前,若得分相等则类别低者靠前,若类别也相等,则序号小者优先。
(3)代码实现
#include<set>
#include<map>
#include<vector>
#include<iostream>
using namespace std;
struct node {
int id;
int type;
int score;
bool operator<(const node &n) const {
if(score==n.score) {
if(type==n.type)
return id<n.id;
return type<n.type;
}
return score>n.score;
}
};
int main