(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() {
int m,n

该博客讨论了CCF 201909-4推荐系统的算法设计,作者通过分析题目需求,指出在频繁增删和查询操作中,初选vector并排序的方法会导致超时。文章建议使用红黑树结构的set来优化,因为set能方便地对商品信息进行排序。为了满足排序条件,文章强调需要重载比较运算符"<",优先考虑得分、类别和序号。代码实现部分展示了这一优化思路。
&spm=1001.2101.3001.5002&articleId=107826200&d=1&t=3&u=69dc8bf7a0e340f3b5cfc6b81b2a9398)
533

被折叠的 条评论
为什么被折叠?



