PAT_A1129#Recommendation System

该博客围绕简单推荐系统的编程实现展开,介绍了系统通过用户访问物品的次数来评估偏好。给出了输入输出规格,包括查询总数、最大推荐数等信息。还提及了实现要点,如推荐列表的设置、排序优化等,并给出代码来源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Source:

PAT A1129 Recommendation System (25 分)

Description:

Recommendation system predicts the preference that a user would give to an item. Now you are asked to program a very simple recommendation system that rates the user's preference by the number of times that an item has been accessed by this user.

Input Specification:

Each input file contains one test case. For each test case, the first line contains two positive integers: N (≤ 50,000), the total number of queries, and K (≤ 10), the maximum number of recommendations the system must show to the user. Then given in the second line are the indices of items that the user is accessing -- for the sake of simplicity, all the items are indexed from 1 to N. All the numbers in a line are separated by a space.

Output Specification:

For each case, process the queries one by one. Output the recommendations for each query in a line in the format:

query: rec[1] rec[2] ... rec[K]

where query is the item that the user is accessing, and rec[i] (i=1, ... K) is the i-th item that the system recommends to the user. The first K items that have been accessed most frequently are supposed to be recommended in non-increasing order of their frequencies. If there is a tie, the items will be ordered by their indices in increasing order.

Note: there is no output for the first item since it is impossible to give any recommendation at the time. It is guaranteed to have the output for at least one query.

Sample Input:

12 3
3 5 7 5 5 3 2 1 8 3 8 12

Sample Output:

5: 3
7: 3 5
5: 3 5 7
5: 5 3 7
3: 5 3 7
2: 5 3 7
1: 5 3 2
8: 5 3 1
3: 5 3 1
8: 3 5 1
12: 3 5 8

Keys:

  • 快乐模拟

Attention:

  • 有点在线处理的意思,不太好想
  • 推荐列表要多开两个单位,k号必须是除了列表外,次数最多且序号最小的,因此还需要存储k+1号来保证k号的序号是最小的
  • cmp引用传参会快一点,没改之前有个测试点过不去,为此还打算自己重写排序算法,没想到试了下引用传参就好了0,0
  • 其实运算符重载也是可以的,最开始怕超时,没敢用,而且思路上更简单一些;

Code:

 1 /*
 2 Data: 2019-05-31 19:33:26
 3 Problem: PAT_A1129#Recommendation System
 4 AC: 01:04:23
 5 
 6 
 7 题目大意:
 8 推荐系统会预测用户对于商品的喜好程度,现在通过用户获取商品的次数来评估用户的喜好程度
 9 输入:
10 第一行给出,查询次数N<=5e4,系统给出推荐最大数量K<=10
11 第二行给出,用户依次查询的商品编号(1~N)
12 输出:
13 当前搜索编号:推荐商品编号 <=k
14 */
15 #include<cstdio>
16 #include<algorithm>
17 using namespace std;
18 const int M=5e5+10,N=10;
19 int rec[N],w[M]={0},in[M]={0},pt=0;
20 
21 bool cmp(const int &a, const int &b)
22 {
23     if(w[a]!=w[b])
24         return w[a]>w[b];
25     else
26         return a < b;
27 }
28 
29 int main()
30 {
31 #ifdef    ONLINE_JUDGE
32 #else
33     freopen("Test.txt", "r", stdin);
34 #endif
35 
36     int n,k,index;
37     scanf("%d%d", &n,&k);
38     for(int i=0; i<n; i++)
39     {
40         scanf("%d", &index);
41         if(i!=0)
42         {
43             printf("%d:", index);
44             for(int j=0; j<min(pt,k); j++)
45                 printf(" %d", rec[j]);
46             printf("\n");
47         }
48         w[index]++;
49         if(in[index]==0){
50             rec[pt]=index;
51             in[index]=1;
52             if(pt<k+1)  pt++;
53         }
54         sort(rec,rec+pt+1,cmp);
55         if(pt==k+1)
56             in[rec[pt]]=0;
57     }
58 
59     return 0;
60 }

 

转载于:https://www.cnblogs.com/blue-lin/p/10957549.html

WITH product_aggregate AS ( SELECT T21.system_item_code AS system_item_code , T22.item_name AS item_name , T22.recommendation_sales_price AS recommendation_sales_price , T21.service_charge_type AS service_charge_type FROM( SELECT DISTINCT T11.system_item_code AS system_item_code , T11.service_charge_type AS service_charge_type FROM( SELECT T00.system_item_code AS system_item_code, T00.service_charge_type AS service_charge_type FROM m_ia_reserve_item T00 INNER JOIN m_ia_item_by_store_all T01 ON T01.version = #{itemByStoreAllVersion} AND T01.original_store_code = #{originalStoreCode} AND T01.system_item_code = T00.system_item_code AND T01.pattern_type = T00.pattern_type AND T01.pattern_code = T00.pattern_code WHERE T00.version = #{reserveItemVersion} AND T00.reserve_analysis_group_code = #{reserveAnalysisGroupCode} AND T00.apply_start_date <![CDATA[ <= ]]> CAST(#{reserveAnalysisAggregateDate} AS DATE) UNION ALL SELECT T10.system_item_code AS system_item_code, T10.service_charge_type AS service_charge_type FROM m_ia_reserve_item_netgift T10 WHERE T10.version = #{netgiftVersion} AND T10.original_store_code = #{originalStoreCode} AND T10.reserve_analysis_group_code = #{reserveAnalysisGroupCode} ) T11 ) T21 INNER JOIN m_ia_item_by_store_all T22 ON T22.version = #{itemByStoreAllVersion} AND T22.original_store_code = #{originalStoreCode} AND T22.system_item_code = T21.system_item_code ) SELECT T21.item_code AS item_code, T21.system_item_code AS system_item_code, T20.item_name AS item_name, T20.recommendation_sales_price AS recommendation_sales_price, T20.service_charge_type AS service_charge_type, TO_CHAR(T21.analysis_date, 'YYYYMMDD') AS analysis_date, CASE WHEN T20.service_charge_type IN ('1','2') THEN 0 ELSE T21.delivery_quantity END AS delivery_quantity, CASE WHEN T20.service_charge_type IN ('1','2') THEN 0 ELSE T21.sales_quantity END AS sales_quantity, T21.delivery_result_flag AS delivery_result_flag, T21.sales_amount AS sales_amount FROM product_aggregate T20 INNER JOIN t_ia_reserve_item_aggregate T21 ON T21.original_store_code = #{originalStoreCode} AND T21.reserve_analysis_group_code = #{reserveAnalysisGroupCode} AND T21.system_item_code = T20.system_item_code AND T21.analysis_date <![CDATA[ <= ]]> CAST(#{targetPeriodEndDate} AS DATE) <if test="targetPeriodStartDate <= salesStartDate"> AND T21.analysis_date <![CDATA[ >= ]]> CAST(#{targetPeriodStartDate} AS DATE) </if> <if test="salesStartDate < targetPeriodStartDate"> AND T21.analysis_date <![CDATA[ >= ]]> CAST(#{salesStartDate} AS DATE) </if> ORDER BY T21.item_code, T21.system_item_code, T21.analysis_date           能优化一下这个sql吗
最新发布
06-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值