• 1129 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

    题意:

      这道题有点像操作系统中的最近最久未使用算法,就会将最经常使用的输出,如果频率相等则输出序号较小的。

    思路:

      将下标和访问的次数构造成一个结构体,然后用set来存储这个结构体,重载<号(划重点)使set集合中的元素按照访问的次数递减,if there is a tie,再按照下标递增的方法排序。最后输出set集合中前k个元素。

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 struct Node {
     6     int num;
     7     int index;
     8     bool operator<(const Node& a) const {
     9         return a.num == num ? index < a.index : num > a.num;
    10     }
    11 };
    12 
    13 int main() {
    14     int n, k, t, cnt;
    15     cin >> n >> k;
    16     vector<int> v(n + 1, 0);
    17     set<Node> s;
    18     for (int i = 1; i <= n; ++i) {
    19         cin >> t;
    20         cnt = 0;
    21         if (i != 1) {
    22             cout << t << ":";
    23             for (auto it : s)
    24                 if (cnt++ < k)
    25                     cout << " " << it.index;
    26                 else
    27                     break;
    28             cout << endl;
    29         }
    30         auto it = s.find(Node{v[t], t});
    31         if (it != s.end()) s.erase(it);
    32         v[t]++;
    33         s.insert(Node{v[t], t});
    34     }
    35 
    36     return 0;
    37 }

      刚开始做的时候感觉应该比较简单,但是当自己动手实现起来发现真的不太好维护,应该用什么样的数据结构来存储这样的数据呢?怎样做才能不超时呢?带着这些疑问去看了大佬的代码,看过之后又学会了一种操作。


      今天又把这题给做了一下,因为忘记了上次是怎么做的了,所以这次写起来比较费力,最后提交的时候还有两组测试点没有通过。我也不想再Debug了,如果选择的数据结构不合适的话,做起来真的很费力。而且还容易出错。

    2020-07-21 17:08:31

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    DFS
    离散化
    前缀和&差分
    数组运用_1-15 选择题
    数组运用_1-13 选择题
    数组运用_1-11 选择题
    数组运用_1-9 选择题
    数组运用_1-4 编程练习
    数组初始_2-22编程练习
    poj 3669 bfs(这道题隐藏着一个大坑)
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12748454.html
Copyright © 2020-2023  润新知