【PAT】A1129 Recommendation System【排序】

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

题意

给出N个查询和最大推荐数K,每次查询一个商品,就显示当前查询次数最多的不多于K个商品(按照查询次数降序 > 序号升序 排序)。

思路

使用一个一维整型数组pq存储当前查询次数最多的K个商品的id,用inPQ来存储对应商品在pq中的位置(-1表示不在pq中),用数组cnt来存储每个商品的查询次数,每次查询的时候进行如下步骤:

  • 如果该商品已经在pq中,那么增大它的次数,对该商品所在pq中的元素进行冒泡(它有可能比前面的商品次数多,此时需要往前调整,直至pq重新变为有序)。
  • 如果该商品不在pq中,且pq中的元素少于K个,则向pq中增加一个元素,对其进行冒泡(前面的元素可能和它查询次数相同,但是id比它大,所以有可能需要调整元素顺序使pq重新有序)
  • 如果该商品不在pq中,但它增大查询次数后优先级比pq中最后一个元素高。则将其替换pq最后一个元素,进行冒泡。
    由于题目给出的K很小,所以冒泡步骤可以使用sort代替,这样写起来更方便些。(冒泡的复杂度是O(N),排序的复杂度是O(log2N))。

代码

#include <cstdio>
#include <algorithm>
#define MAX_N 50005
#define MAX_K 10
using namespace std;
int cnt[MAX_N], inPQ[MAX_N], pq[MAX_K], pqLen = 0;
// 从Pq总下标为i的元素开始冒泡
void swin(int i){
    for(; i > 0; i--){
        int u = pq[i], v = pq[i - 1];
        int cmp = cnt[u] - cnt[v];
        if(cmp > 0 || (cmp == 0 && u < v)){
            pq[i] = v;
            inPQ[v] = i;
            pq[i - 1] = u;
            inPQ[u] = i - 1;
        }
    }
}
int main(){
    fill(cnt, cnt + MAX_N, 0);
    fill(inPQ, inPQ + MAX_N , -1);
    
    int N, K, t;
    scanf("%d %d", &N, &K);
    
    for(int i = 0; i < N; i++){
        scanf("%d", &t);
        if(i > 0){
            printf("%d:", t);
            for(int j = 0; j < pqLen; j++){
                printf(" %d", pq[j]);
            }
            putchar('\n');
        }
        
        cnt[t]++;
        // 根据不同情况执行相应算法
        if(inPQ[t] >= 0){
            swin(inPQ[t]);
        }else if(pqLen < K){
            pq[pqLen] = t;
            inPQ[t] = pqLen;
            swin(pqLen++);
        }else{
            int temp = pq[pqLen - 1];
            int cmp = cnt[t] - cnt[temp];
            if(cmp > 0 || (cmp == 0 && t < temp)){
                inPQ[temp] = -1;
                inPQ[t] = pqLen - 1;
                pq[pqLen - 1] = t;
            }
            swin(pqLen - 1);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值