PAT-A-Level 1129 推荐系统 (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

给出n个商品编号,k个最大推荐数量,对每次点击之前的点击次数降序输出编号,如果点击次数相等,就从小到大输出编号。

如当点击到第三个5时,前面出现了2次5,1次3,1次7,则按上面的排序规则,输出5 3 7。

用set中运算符重载结构体排序,或者set中的自选排序函数(这要慢一些),可以很容易的解出该题。其实map应该也可以。先说第一种方法。

set运算符重载排序

对set进行"<(小于号)"运算符重载后(大于号我没试过),调用迭代器就会默认使用重载的规则对set进行遍历,能够大大减小时间复杂度。重载的写法如下。

注意:由于每次对同编号商品的插入只有更新set中的值才行,而直接更改set中对应元素的频数并不能更改其在红黑树中的位置,所以此时需要用公共函数find找到对应元素则set中的位置,之后将这个迭代器用set.erase删除。我最开始放弃使用set,正是因为忘记了迭代器+erase可以更新set等STL数据结构的值了。

  • set.erase(iterator)可以用来删除set中的元素。
  • STL.find(elementType)返回该数据结构下指向这个文件类型的迭代器。
struct node{
    int v,cnt;	//v是商品编号,cnt是出现频数
    bool operator < (node a) const {	//const不能少
        if (cnt==a.cnt) return v<a.v;	//和sort函数的排序方法一样
        else    return cnt>a.cnt;
    }
};
#include <bits/stdc++.h>
#define N 50005
using namespace std;
int a[N],frq[N];
struct node{
    int v,cnt;
    bool operator < (node a) const {
        if (cnt==a.cnt) return v<a.v;
        else    return cnt>a.cnt;
    }
};
set<node> ss;
int main()  {
    int n,k;
    cin>>n>>k;
    for (int i=0;i<n;i++)   cin>>a[i];
    for (int i=1;i<n;i++)   {
        if (i==0)   continue;
        set<node>::iterator it1=ss.find(node{a[i-1],frq[a[i-1]]});
        if (it1!=ss.end())    ss.erase(it1);
        frq[a[i-1]]++;
        ss.insert(node{a[i-1],frq[a[i-1]]});    //此种插入之后,不会再随着其他插入改变位置,其实是没有意义的,只有找到原本的元素,删除之,次数+1后重新插入才有意义
        cout<<a[i]<<':';
        set<node>::iterator it0=ss.begin();
        int j=0;
        for (;it0!=ss.end()&&j<k;j++,it0++)
            cout<<' '<<(*it0).v;
        cout<<endl;
    }
    return 0;
}

set自选排序函数

如下定义。

int a[N],frq[N];		//frq存储每个元素的频数
struct cmp{
    bool operator ()(int a,int b) {
        if (frq[a]==frq[b])  return a<b;
        else return     frq[a]>frq[b];
    }
};
set<int,cmp>    sic;
#include <bits/stdc++.h>
#define N 50005
using namespace std;
int a[N],frq[N];
struct cmp{
    bool operator ()(int a,int b) {
        if (frq[a]==frq[b])  return a<b;
        else return     frq[a]>frq[b];
    }
};
set<int,cmp>    sic;
int main()  {
    int n,k;
    cin>>n>>k;
    for (int i=0;i<n;i++)   cin>>a[i];
    for (int i=1;i<n;i++)   {
        if (i==0)   continue;
        set<int,cmp>::iterator it1=sic.find(a[i-1]);
        if (it1!=sic.end())  {sic.erase(it1);}
        frq[a[i-1]]++;
        sic.insert(a[i-1]);
        cout<<a[i]<<':';
        set<int,cmp>::iterator it0=sic.begin();
        int j=0;
        for (;it0!=sic.end()&&j<k;j++,it0++)    cout<<' '<<(*it0);
        cout<<endl;
    }
    return 0;
}

我的非STL的错解,17分

#include <bits/stdc++.h>
#define N 50005
using namespace std;
int a[N],frq[N],usd[N];
vector<int> vr;
bool cmp(int a,int b)   {
    if (frq[a]==frq[b]) return a<b;
    return frq[a]>frq[b];
}
int main()  {
    int n,k,sml=-1;
    cin>>n>>k;
    for (int i=0;i<n;i++)   {
        cin>>a[i];
        if (i==0)   {continue;}
        frq[a[i-1]]++;
        if ((int)vr.size()<k)    {   //如果序列未满
            if (usd[a[i-1]]==0)   {   //如果序列中无该值
                vr.push_back(a[i-1]);
                usd[a[i-1]]=1;
            }
            else {
                sort(vr.begin(),vr.end(),cmp);
                if (!vr.empty())    sml=frq[vr.back()];
            }

        }
        else {  //序列已满
            if (usd[a[i-1]]==0)   {   //如果序列中无该值
                if (frq[a[i-1]]>=sml)  {   //并且这个值出现频数大于等于序列中的最小值
                    vr.push_back(a[i-1]);
                    sort(vr.begin(),vr.end(),cmp);
                    if (vr.back()!=a[i-1])    {usd[a[i-1]]=1;usd[vr.back()]=0;}
                    vr.pop_back();
                    if (!vr.empty())    sml=frq[vr.back()];
                }
            }
            else {  //如果有该值
                sort(vr.begin(),vr.end(),cmp);
                if (!vr.empty()) sml=frq[vr.back()];
            }
        }
        cout<<a[i]<<':';
        for (int j=0;j<(int)vr.size();j++)
            cout<<' '<<vr[j];
        cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值