跳到正文

题解

题解:P15698 [2018 KAIST RUN Spring] Segmentation

ycx54x1tx约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年10月9日
收录
2026年10月13日
更新
2026年10月13日
篇幅
约 6 分钟
获赞
1
题目
P15698

版本

  1. v1当前

题目描述

[2018 KAIST RUN Spring] Segmentation

前置知识

哈希、无序关联式容器(不必要)。

解题思路

大致思路想都不用想,按题意模拟即可。
但由于用户名是由字符串组成的,所以需要用哈希存储数据。
在 C++11 以后,我们可以使用 unordered_map。

unordered_map

基于哈希实现的无序关联式容器…采用哈希方式存储元素,内部元素不以任何特定顺序进行排序,所以访问无序关联式容器中的元素时,访问顺序也没有任何保证。

采用哈希存储的特点使得无序关联式容器在平均情况下大多数操作(包括查找,插入,删除)都能在常数时间复杂度内完成,相较于关联式容器与容器大小成对数的时间复杂度更加优秀。

—— Oi-wiki

基本用法

C++
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 中的用户区分录好,方便后面查表:

C++
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"}
};

当 AA 的值为 11 时,更新用户 BB 的访问时间和访问次数:

C++
if(a==1)
{
	mp[b].lv=t;
	mp[b].fq++;
}

当 AA 的值为 22 时,计算用户 BB 当前的近期度和频率,并查表输出:

C++
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";
}

细节

  1. RF 模型不要填错。
  2. 记得特判 “None” 情况。

AC 代码

时间复杂度: O(n)O(n) 。(因为 字符串 B 的长度≤10\text{字符串 } B \text{ 的长度} \le 10,所以哈希计算可视为常数时间复杂度)

C++
#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;
}
原文