美文网首页
1129 Recommendation System(25 分)

1129 Recommendation System(25 分)

作者: zjh3029 | 来源:发表于2018-08-25 16:07 被阅读0次

    运行超时,尚未解决

    #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;
    }
    

    相关文章

      网友评论

          本文标题:1129 Recommendation System(25 分)

          本文链接:https://www.haomeiwen.com/subject/dzjgiftx.html