【Algorithm】GPLT L2-039 清点代码库

【Algorithm】GPLT L2-039 清点代码库,第1张

L2-039 清点代码库

将每一个功能模块用vector存储,并将其储存在map容器内。最后将数量与模块输出储存在可以有多个相同key值的multimap容器内。因为按照升序输出,所以添加一个容器参数greater。这样一来multimap容器内就是排好序的答案啦,然后直接输出答案即可。

set, map, multiset, multimap, 基于平衡二叉树(红黑树),动态维护有序序列
    size()
    empty()
    clear()
    begin()/end()
    ++, -- 返回前驱和后继,时间复杂度 O(logn)

    set/multiset
        insert()  插入一个数
        find()  查找一个数
        count()  返回某一个数的个数
        erase()
            (1) 输入是一个数x,删除所有x   O(k + logn)
            (2) 输入一个迭代器,删除这个迭代器
        lower_bound()/upper_bound()
            lower_bound(x)  返回大于等于x的最小的数的迭代器
            upper_bound(x)  返回大于x的最小的数的迭代器
    map/multimap
        insert()  插入的数是一个pair
        erase()  输入的参数是pair或者迭代器
        find()
        []  注意multimap不支持此 *** 作。 时间复杂度是 O(logn)
        lower_bound()/upper_bound()

unordered_set, unordered_map, unordered_multiset, unordered_multimap, 哈希表
    和上面类似,增删改查的时间复杂度是 O(1)
    不支持 lower_bound()/upper_bound(), 迭代器的++,--

#include 
#include 
#include 

using namespace std;

const int N = 1e4 + 10;

int n, m;
vector temp[N];
map, int> mp;
multimap, greater > res;

int main()
{
    scanf("%d%d", &n, &m);
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int x;
            scanf("%d", &x);
            temp[i].push_back(x);
        }
        mp[temp[i]]++;
    }
    
    for (auto it : mp) res.insert({it.second, it.first});
    
    printf("%d\n", mp.size());
    
    for (auto it : res) {
        printf("%d", it.first);
        for (auto p : it.second) printf(" %d", p);
        puts("");
    }
    
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/717744.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存