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

思路:

每次只变一个,那么最多也只会从k个里排除一个,所以维护一个k大小的数组,当有符合的元素时,更新这个数组。

这里给两组数据:

5 3
5 4 3 2 1


5 3
3 3 3 2 1

AC代码:

#include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <string.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
//#define mp Debug(x) printf("%d\n", &x);
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1046513837;
const int maxn = 5e4 + 50;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
//typedef vector<ll> vec;
//typedef vector<vec> mat;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
bool cmp(pair<int, int> a, pair<int, int> b){
    if(a.second == b.second){
        return a.first < b.first;
    }
    return a.second > b.second;
}
int n, m;
int arr[maxn];
int times[maxn];
vector<pair<int, int> > ans;
int main() {
    cin >> n >> m;
    rep(i, 1, n){
        cin >> arr[i];
    }
    times[arr[1]]++;
    ans.push_back(make_pair(arr[1], 1));
    rep(i, 2, n){
        printf("%d: ", arr[i]);
        for(int j = 0; j < ans.size(); ++j){
            printf("%d%c", ans[j].first, (j==ans.size()-1)?'\n':' ');
        }
        times[arr[i]]++;
        auto p = make_pair(arr[i], times[arr[i]]);

        int flag = 0;
        for(int j = 0; j < ans.size(); ++j){
            ans[j].second = times[ans[j].first];
            if(p.first == ans[j].first){
                flag = 1;
                break;
            }
        }
        sort(ans.begin(), ans.end(), cmp);

        if(!flag && ans.size() < m){
            ans.push_back(p);
            sort(ans.begin(), ans.end(), cmp);
        }else if(!flag && cmp(p, ans.back())){
            ans.pop_back();
            ans.push_back(p);
            sort(ans.begin(), ans.end(), cmp);
        }
    }
	return 0;
}

/*

*/
发布了276 篇原创文章 · 获赞 21 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_40758751/article/details/103396392