天梯赛2021 L2-039 清点代码库 (25 分)

主要问题
  1. 是否存在这个数组
  2. 排序
解决办法
  1. map<vector , int> 查看是否存在
  2. 自定义排序sort第三参数

易错:cin、cout多次使用时间长,选择scanf,printf

代码

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

const int N = 1e4 + 10;

int n , m;

struct Node{
    vector<int> arr;
    int cnt = 0;
}node[N];

bool cmp (const Node & a , const Node & b)
{
    if(a.cnt == b.cnt)
    {
        for(int i = 0 ; i < m ; i ++)
        {
            if(a.arr[i] == b.arr[i])continue;
            return a.arr[i] < b.arr[i];
        }
    }
    return a.cnt > b.cnt;
}

int main()
{
    int x;
    scanf("%d%d" , &n , &m);
    map < vector <int> , int > map1;
    for(int i = 0 ; i < n ; i ++)
    {
        vector <int> tmp;
        for(int j = 0 ; j < m ; j ++)
        {
            scanf("%d" , &x);
            tmp.push_back(x);
        }
        map1[tmp]++;
    }

    int i = 0;
    for(auto a : map1)
        node[i].arr = a.first , node[i ++].cnt = a.second;
    
    sort(node , node + i , cmp);

    printf("%d\n" , i);

    for(int j = 0 ; j < i ; j ++)
    {
        printf("%d " , node[j].cnt);
        for(int p = 0 ; p < m - 1 ; p ++)
            printf("%d " , node[j].arr[p]);
        printf("%d\n" , node[j].arr[m - 1]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值