1129 Recommendation System(25分)

#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
struct node
{
    int key, cnt;
    node( int a, int b ) : key(a), cnt(b) {} 	//使用node(key, cnt)即可插入;
    bool operator < ( const node &a ) const		//重载'<'运算符
    {   return cnt != a.cnt ? ( cnt > a.cnt ):( key < a.key );  }
};
int main()
{
    int N, K;
    set<node> A;
    vector<int> freq(50001);
    scanf("%d %d", &N, &K);
    for( int i = 0, temp; i < N; ++i )
    {
        scanf("%d", &temp);
        if( i )
        {
            printf("%d:", temp);
            int cnt = 0;
            for( auto it = A.begin(); it != A.end() && cnt < K; ++it, ++cnt )
                printf(" %d", it->key);
            printf("\n");
        }
        auto it = A.find( node( temp, freq[temp] ) );
        if( it != A.end() ) A.erase(it);
        A.insert( node( temp, ++freq[temp] ) );
    }
}

发布了152 篇原创文章 · 获赞 144 · 访问量 9241

猜你喜欢

转载自blog.csdn.net/qq_43749739/article/details/100177802