zoukankan      html  css  js  c++  java
  • 1129 Recommendation System (25 分)

    1129 Recommendation System (25 分)
     

    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来解决,不过我尽量写自己的想法出来。
    数组模拟

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int n,m;
     4 struct Node
     5 {
     6     int va, times;
     7     friend bool operator < (const Node &a, const Node &b){
     8         return a.times == b.times ? a.va < b.va:a.times > b.times;
     9     }
    10 };
    11 map<int,int> val, vis;
    12 vector<Node> v;
    13 int main(){
    14     cin >> n >> m;
    15     int x;
    16     for(int i = 0; i < n; i++){
    17         cin >> x;
    18         val[x] ++;
    19         if(i){
    20             cout << x<<":";
    21             for(int j = 0; j < v.size(); j++)
    22                 cout <<" "<<v[j].va;
    23             cout<<endl;
    24         }
    25         if(v.size() == 0 && m >= 1){
    26             v.push_back({x,val[x]});
    27             vis[x] = 1;
    28             continue;
    29         }else if(v.size()){
    30             if(v.size() < m){
    31                 if(vis[x] == 0){
    32                     vis[x] = 1;
    33                     v.push_back({x,val[x]});
    34                 }else{
    35                     for(int j = 0; j < v.size(); j++)
    36                         if(v[j].va == x)
    37                             v[j].times ++;
    38                 }
    39             }else if(v.size()){
    40                 if(vis[x] == 0){
    41                     if(val[x] == v[v.size()-1].times && v[v.size()-1].va > x){
    42                         vis[v[v.size()-1].va] = 0;
    43                         v[v.size()-1].va = x;
    44                         vis[x] = 1;
    45                     }else if(val[x] > v[v.size()-1].times){
    46                         vis[x] = 1;
    47                         vis[v[v.size()-1].va] = 0;
    48                         v[v.size()-1].va = x;
    49                         v[v.size()-1].times = val[x];
    50                     }
    51                 }else{
    52                     for(int j = 0; j < v.size(); j++)
    53                         if(v[j].va == x)
    54                             v[j].times ++;
    55                 }
    56             }
    57         }
    58         sort(v.begin(), v.end());
    59     }
    60     return 0;
    61 }






  • 相关阅读:
    浏览器 显示flash问题
    类型参数的约束
    C# FUNC 应用
    c#抽奖系统
    3D基础数学小结
    google应用之字体引用
    MYSQL启动参数
    chrome中你不知道的快捷方式
    SQL Server 2008在添加用户时弹出15195错误
    Hibernate Maven Missing artifact javax.transaction:jta:jar:1.0.1B
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11312337.html
Copyright © 2011-2022 走看看