题目描述
[2018 KAIST RUN Spring] Segmentation
前置知识
哈希、无序关联式容器(不必要)。
解题思路
大致思路想都不用想,按题意模拟即可。
但由于用户名是由字符串组成的,所以需要用哈希存储数据。
在 C++11 以后,我们可以使用 unordered_map。
unordered_map
基于哈希实现的无序关联式容器…采用哈希方式存储元素,内部元素不以任何特定顺序进行排序,所以访问无序关联式容器中的元素时,访问顺序也没有任何保证。
采用哈希存储的特点使得无序关联式容器在平均情况下大多数操作(包括查找,插入,删除)都能在常数时间复杂度内完成,相较于关联式容器与容器大小成对数的时间复杂度更加优秀。
—— Oi-wiki
基本用法
unordered_map<int, long long> mp;
//建一个键为 int、值为 long long 的哈希表
mp[3] = 10; // 插入或修改
long long x = mp[3]; // 取值
if (mp.count(3)) // 判断是否存在
mp.erase(3); // 删除键 3
mp.size(); // 元素个数 代码实现
先提前把 RF Channel 中的用户区分录好,方便后面查表:
string g[5][5]={
{"New Customer", "Potential Loyalist","Potential Loyalist","Loyal Customer","Champion"},
{"Promising", "Potential Loyalist","Potential Loyalist","Loyal Customer","Loyal Customer"},
{"About to Sleep","About to Sleep", "Need Attention", "Loyal Customer","Loyal Customer"},
{"Lost", "Hibernating", "About to Leave", "About to Leave","About to Leave"},
{"Lost", "Lost", "About to Leave", "About to Leave","Can't Lose Them"}
};当 的值为 时,更新用户 的访问时间和访问次数:
if(a==1)
{
mp[b].lv=t;
mp[b].fq++;
}当 的值为 时,计算用户 当前的近期度和频率,并查表输出:
if(a==2)
{
auto it=mp.find(b);
if(it==mp.end())
{
cout<<"None\n";
continue;
}
int r=t-it->second.lv;
int f=it->second.fq;
int ri=4;
for(int i=0;i<4;i++)
if(r<=rt[i])
{
ri=i;
break;
}
int fi=4;
for(int i=0;i<4;i++)
if(f<=ft[i])
{
fi=i;
break;
}
cout<<g[ri][fi]<<"\n";
}细节
- RF 模型不要填错。
- 记得特判 “None” 情况。
AC 代码
时间复杂度: 。(因为 ,所以哈希计算可视为常数时间复杂度)
#include<bits/stdc++.h>
using namespace std;
int rt[4],ft[4];
int n;
string g[5][5]={
{"New Customer", "Potential Loyalist","Potential Loyalist","Loyal Customer","Champion"},
{"Promising", "Potential Loyalist","Potential Loyalist","Loyal Customer","Loyal Customer"},
{"About to Sleep","About to Sleep", "Need Attention", "Loyal Customer","Loyal Customer"},
{"Lost", "Hibernating", "About to Leave", "About to Leave","About to Leave"},
{"Lost", "Lost", "About to Leave", "About to Leave","Can't Lose Them"}
};
struct node{
int lv=0,fq=0;
};
unordered_map<string,node> mp;
int main()
{
for(int i=0;i<4;i++)cin>>rt[i];
for(int i=0;i<4;i++)cin>>ft[i];
cin>>n;
for(int t=1;t<=n;t++)
{
int a;
string b;
cin>>a>>b;
if(a==1)
{
mp[b].lv=t;
mp[b].fq++;
}
else
{
auto it=mp.find(b);
if(it==mp.end())
{
cout<<"None\n";
continue;
}
int r=t-it->second.lv;
int f=it->second.fq;
int ri=4;
for(int i=0;i<4;i++)
if(r<=rt[i])
{
ri=i;
break;
}
int fi=4;
for(int i=0;i<4;i++)
if(f<=ft[i])
{
fi=i;
break;
}
cout<<g[ri][fi]<<"\n";
}
}
return 0;
}