pat 1129. Recommendation System (25)(set的增删)

来源:互联网 发布:香港买mac口红多少钱 编辑:程序博客网 时间:2024/06/05 16:37

1129. Recommendation System (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<= 50000), 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 33 5 7 5 5 3 2 1 8 3 8 12
Sample Output:
5: 37: 3 55: 3 5 75: 5 3 73: 5 3 72: 5 3 71: 5 3 28: 5 3 13: 5 3 18: 3 5 112: 3 5 8

题意:给一列数,每次询问前m项出现次数最多的数字,出现次数相同按数字大小排序;

解:直接开个set在增加前做删除操作就可以了

#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<vector>#include<map>#include <set>//#include <bits/stdc++.h>using namespace std;const int N = 520;typedef long long LL;struct node{    int v, num;    bool operator <(const node &A)const    {        if(num!=A.num) return num>A.num;        else return v<A.v;    }};set<node>st;set<node>::iterator it;int vis[50010];int main(){    st.clear();    int n, m;    memset(vis,0,sizeof(vis));    scanf("%d %d", &n, &m);    int x;    scanf("%d", &x);    vis[x]++;    node tmp;    tmp.v=x,tmp.num=vis[x];    st.insert(tmp);    for(int i=2; i<=n; i++)    {        scanf("%d", &x);        printf("%d:",x);        int h=0, y;        for(it=st.begin(); it!=st.end(); it++)        {            h++;            printf(" %d",(*it).v);            if(h==m) break;        }        vis[x]++;        tmp.v=x,tmp.num=vis[x]-1;        if(st.find(tmp)!=st.end())st.erase(tmp);        tmp.num++;        st.insert(tmp);        printf("\n");    }    return 0;}







原创粉丝点击