1129 Recommendation System(25 分)

2018-08-25  本文已影响0人  zjh3029

运行超时,尚未解决

#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
#include <set>
using namespace std;

multiset<int>m;
map<int, int>ma;

bool CMP(const pair<int, int>& x, const pair<int, int>& y)
{
    return x.second > y.second;
}
int main()
{
    int M,N,a;
    cin >> M >> N;

    cin >> a;
    m.insert(a);
    for (int i = 1; i < M; i++)
    {
        cin >> a;
        cout <<a<<":";
        for (multiset<int>::iterator it = m.begin(); it !=m.end(); it++)
        {
            //cout << *it <<" "<<m.count(*it)<<"    ";
            ma[*it] = m.count(*it);
        }
        vector<pair<int, int> > v(ma.begin(), ma.end());
        sort(v.begin(), v.end(), CMP);
        /*for (map<int, int>::iterator i = ma.begin(); it!=ma.end(); it++)
        {
            cout << it->first << " ";
        }*/
        for (vector<pair<int, int> >::iterator it = v.begin(); it!=v.end()&&it<(v.begin()+N); it++)
        {
            cout << " "<<it->first;
        }
        cout << endl;
        m.insert(a);
    }
    system("pause");
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读