PAT甲级——A1129 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 //所以购买第一件商品3是没有推荐的,因为没有购买历史记录
 3 //然后根据前面购买的历史次数,进行次数从多到少进行推荐,最多推荐三个,相同次数的推荐标号最小的的//
 4 //使用set来得到按要求的排序规格,vector来辅助查询set中是否已经存在元素
 5 #include <iostream>
 6 #include <set>
 7 #include <vector>
 8 using namespace std;
 9 int n, k;
10 struct Node
11 {
12     int value, cnt;
13     bool operator < (const Node& a)const//set的排序规则
14     {
15         return (cnt != a.cnt) ? cnt > a.cnt:value < a.value;
16     }
17 };
18 int main()
19 {
20     int n, k, num;
21     scanf("%d %d", &n, &k);
22     set<Node>s;
23     vector<int>v(n + 1, 0);
24     for (int i = 0; i < n; ++i)
25     {
26         cin >> num;
27         if (i != 0)//第一件商品没有推荐
28         {
29             cout << num << ":";
30             int i = 1;
31             for (auto ptr = s.begin(); ptr != s.end() && i <= k; ++ptr, i++)
32                 cout << " " << ptr->value;
33             cout << endl;
34         }
35         auto ptr = s.find(Node{ num,v[num] });
36         if (ptr != s.end())//记住set不能修改内容,只能擦除后重新写入
37             s.erase(ptr);
38         v[num]++;
39         s.insert(Node{ num,v[num] });//使用了初始化列表原则
40     }
41     return 0;
42 }

原文地址:https://www.cnblogs.com/zzw1024/p/11483142.html

时间: 2024-10-08 08:00:24

PAT甲级——A1129 Recommendation System【25】的相关文章

PAT 甲级 1129 Recommendation System

https://pintia.cn/problem-sets/994805342720868352/problems/994805348471259136 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 pref

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names

PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connec

PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic n

PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, wh

PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if

PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be /, where N?c?? is the number of distinct common numbers shared by the two sets, and N?t?? is the total number of distinct numbers in the two sets. Yo

PAT甲级——A1145 HashingAverageSearchTime【25】

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find w

PAT甲级——A1146 TopologicalOrder【25】

This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options. Input Specification: Each in