其他分享
首页 > 其他分享> > PAT 1129 Recommendation System (25 分)

PAT 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

分析:
1、题意难理解,可能英语太差,我是直接通过给的测试点案例推出实现是什么功能的

2、用单纯的vector排序的话,测试点3,测试点4会超时

3、
方法1:
通过map在外计数,然后同时把新数据推入vector里,然后再对vector排序,但是这样做测试点3,4会超时;所以我们必须要剪枝,我们可以只维护前k个里的顺序,记录第k个的计数,如果当输入的数大于等于第k个的计数时且不在vector里,则加入vector里排序输出,最后再把第k+1个数据删除,一定要删除!否则越堆越多,也会超时的

方法2:
使用set+结构体来排序,因为会出现很多重复元素,用set可以减少很多的重复元素进入;set里的元素只能访问不能更新,只能先删除旧的元素,再添加值为新的元素;
set对结构体自定义排序两种方法:
1、在结构体内友元函数重载小于符号(set)

struct Node
{
    int data,cnt;
    friend bool operator < (Node n1,Node n2)
    {
        if (n1.cnt != n2.cnt)
            return n1.cnt > n2.cnt;
        else
            return n1.data < n2.data;
    }
};

2、在结构体外写成cmp形式(set<Node,cmp>)

struct Node
{
	int data,cnt;
};
struct cmp
{
	bool operator()(Node n1,Node n2)
	{
        if (n1.cnt != n2.cnt)
            return n1.cnt > n2.cnt;
        else
            return n1.data < n2.data;	
	}
}

我的代码:

vector+map+剪枝:

#include<cstdio>
#include<vector>
#include<unordered_map>
#include<algorithm>
using namespace std;
vector<int> v;
unordered_map<int,int> mp,flag;
bool cmp(int v1,int v2)
{
    if (mp[v1] != mp[v2])
        return mp[v1] > mp[v2];
    else
        return v1 < v2;
}
int main()
{
    int n,k,temp,num,maxnum = 0;
    scanf("%d%d",&n,&k);
    scanf("%d",&num);
    temp = num; 
    for (int i = 1 ;i < n ;i++)
    {
        scanf("%d",&num);
        mp[temp]++;
        if ((v.size() < k || mp[temp] >= maxnum) && flag[temp] == 0)
        {
            v.push_back(temp);
            flag[temp] = 1;
        }
        sort(v.begin(),v.end(),cmp);
        int cnt = (v.size() < k)?v.size():k;
        printf("%d:",num);
        for (int j = 0 ;j < cnt ;j++)
            printf(" %d",v[j]);
        printf("\n");
        if (v.size() > k)
        {   
            flag[v[v.size()-1]] = 0;
            v.erase(v.end()-1);            
        }         
        maxnum = mp[v[cnt-1]];
        temp = num;
    }
    return 0;
}

set+结构体:

#include<cstdio>
#include<set>
#include<unordered_map>
#include<algorithm>
using namespace std;
const int maxn = 50010;
struct Node
{
    int data,cnt;
    friend bool operator < (Node n1,Node n2)
    {
        if (n1.cnt != n2.cnt)
            return n1.cnt > n2.cnt;
        else
            return n1.data < n2.data;
    }
};
unordered_map<int,int> mp;
int main()
{
    int n,k,num;
    set<Node> s;
    scanf("%d%d",&n,&k);
    scanf("%d",&num);
    mp[num]++;
    s.insert(Node{num,mp[num]}); 
    for (int i = 1 ;i < n ;i++)
    {
        int temp = 0;
        scanf("%d",&num);  
        int cnt = (s.size() < k)?s.size():k;
        printf("%d:",num);
        for (auto it = s.begin() ;temp < cnt && it != s.end() ;it++)
        {
            printf(" %d",it->data);
            temp++;
        }    
        auto it = s.find(Node{num,mp[num]});        
        if (it != s.end()) s.erase(it);
        mp[num]++;
        s.insert(Node{num,mp[num]});
        printf("\n");
    }
    return 0;
}

标签:Node,1129,cnt,PAT,int,System,num,mp,n1
来源: https://blog.csdn.net/qq_44627816/article/details/114319818