当前位置:网站首页>用户喜好
用户喜好
2022-07-25 09:26:00 【scwMason】
为了不断优化推荐效果,今日头条每天要存储和处理海量数据。假设有这样一种场景:我们对用户按照它们的注册时间先后来标号,对于一类文章,每个用户都有不同的喜好值,我们会想知道某一段时间内注册的用户(标号相连的一批用户)中,有多少用户对这类文章喜好值为k。因为一些特殊的原因,不会出现一个查询的用户区间完全覆盖另一个查询的用户区间(不存在L1<=L2<=R2<=R1)。
输入描述:
输入: 第1行为n代表用户的个数 第2行为n个整数,第i个代表用户标号为i的用户对某类文章的喜好度 第3行为一个正整数q代表查询的组数 第4行到第(3+q)行,每行包含3个整数l,r,k代表一组查询,即标号为l<=i<=r的用户中对这类文章喜好值为k的用户的个数。 数据范围n <= 300000,q<=300000 k是整型
输出描述:
输出:一共q行,每行一个整数代表喜好值为k的用户的个数
示例1
输入
5 1 2 3 3 5 3 1 2 1 2 4 5 3 5 3
输出
1 0 2
说明
样例解释: 有5个用户,喜好值为分别为1、2、3、3、5, 第一组询问对于标号[1,2]的用户喜好值为1的用户的个数是1 第二组询问对于标号[2,4]的用户喜好值为5的用户的个数是0 第三组询问对于标号[3,5]的用户喜好值为3的用户的个数是2
思路
建立一个map,不同喜好值作为key,下标作为val,这个key对应的val组成一个array。然后去寻找k对应的array里面符合在l~r区间的,这一步用二分法做
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_map<int, vector<int>> key_m;
int n;cin>>n;
for(int i=1;i<=n;i++){
int tem;cin>>tem;
key_m[tem].push_back(i);
}
int q;cin>>q;
for(int i=0;i<q;i++){
int l,r,k;cin>>l>>r>>k;
unordered_map<int, vector<int>>::iterator itera=key_m.find(k);
if(itera==key_m.end()) cout<<0<<endl;
else{
auto ie1=lower_bound(itera->second.begin(),itera->second.end(),l);
auto ie2=upper_bound(itera->second.begin(), itera->second.end(),r);
cout<<ie2-ie1<<endl;
}
}
return 0;
}
边栏推荐
猜你喜欢

修改mysql的分组报错Expression #1 of SELECT list is not in GROUP

Mlx90640 infrared thermal imaging sensor temperature measurement module development notes (II)

Redis使用场景

链表相关(设计链表及环链表问题)

Introduction to low power consumption and UPF

UE4 外部打开exe文件

【近万字干货】别让你的简历配不上你的才华——手把手教你制作最适合你的简历

Mlx90640 infrared thermal imager temperature measurement module development notes (4)

JS uses requestanimationframe to detect the FPS frame rate of the current animation in real time

ROS分布式操作--launch文件启动多个机器上的节点
随机推荐
Advanced introduction to digital IC Design SOC
链表相关(设计链表及环链表问题)
mysql历史数据补充新数据
Probability theory and mathematical statistics 3 discrete random variables and probability distributions (Part 2)
C函数不加括号的教训
Configuring ROS development environment with vscode: Causes and solutions to the problem of ineffective code modification
Download and installation of QT 6.2
LOAM 融合 IMU 细节之 TransformToEnd 函数
Nodejs initial experience
数据库MySQL详解
线程池的设计和原理
ADC introduction
ROS分布式操作--launch文件启动多个机器上的节点
The way of code neatness -- hit the pain point directly
CentOs安装redis
【成长必备】我为什么推荐你写博客?愿你多年以后成为你想成为的样子。
Probability theory and mathematical statistics 4 continuous random variables and probability distributions (Part 1)
Eco introduction
VScode配置ROS开发环境:修改代码不生效问题原因及解决方法
Wechat applet jumps to other applets