离散化——unordered_map

学习一下unordered_map的用法,上海区域赛前才第一次见这个东西,看到和map用法一样自信觉得能用,然而场上卡住了,现在滚过来学一下orz【虽然事后发现G题根本不需要用这个东西。

学过哈希的都很容易理解离散化,无非就是数据过大开不了那么大的数组时,但其实中间有很多浪费掉的空间,那么映射到另一个数组中就可以了。

以前常用的c++离散化是

// a[i] 为初始数组,下标范围为 [1, n]
// len 为离散化后数组的有效长度
std::sort(a + 1, a + 1 + n);

len = std::unique(a + 1, a + n + 1) - a - 1;
// 离散化整个数组的同时求出离散化后本质不同数的个数。
std::lower_bound(a + 1, a + len + 1, x) - a;  // 查询 x 离散化后对应的编号

而unordered_map是一个内部实现了哈希表的数据结构。其查找效率是O(1)的。

那么先上一个基础应用例题:https://ac.nowcoder.com/acm/contest/5158/H

在这里插入图片描述

明显并查集,但是1e9,这么大肯定离散化,那就用一下这个unordered_map代替并查集中的pre数组就ok

#include<bits/stdc++.h>
using namespace std;
unordered_map<int ,int > pre;

int find(int a)
{
    if(pre[a] == 0)
        return a;
    else
        return pre[a] = find(pre[a]); 
}

void Union(int a,int b){
    int m = find(a);
    int n = find(b);
    pre[m] = n;
    return ;
}

int main()
{
    cin.tie(0),ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--) {
        pre.clear();
        int flag = 1;
        int n,a,b,c;
        cin >> n;
        for(int i = 0 ;i < n; i++) {
            cin>>a>>b>>c;
            int m = find(a);
            int n = find(b);
            if(c == 1 ){
                if( m!=n && a !=m &&b != n){
                    flag = 0;
                    break;
                }
                else 
                    Union(a,b);
            }
            else{
                if( m == n){
                    flag = 0;
                    break;
                }      
            }
        }
        if(flag == 0)
            cout<<"NO"<<endl;
        else
            cout<<"YES"<<endl;
    }
    system("pause");
    return 0;
}

未完持续考完试继续写

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unordered_map与unordered_set有什么区别? 回答: unordered_map和unordered_set都是关联式容器,类似于键值对 (key-value) 的模型。它们的底层实现方式不同,unordered_map使用哈希表作为底层数据结构,而unordered_set也是使用哈希表。unordered_map和unordered_set的区别在于它们存储的类型不同,即unordered_map存储键值对,而unordered_set存储单个元素。此外,unordered_map和unordered_set在功能上也有一些区别。unordered_map提供了以键为索引的查找功能,而unordered_set则提供了判断元素是否存在的功能。从效率上来看,unordered_map和unordered_set的增删查改操作的时间复杂度都是O(1),即常数时间。而map和set的时间复杂度为O(logN),其中N是容器中的元素数量。所以在对效率要求较高的情况下,选择unordered_map和unordered_set会更合适。但是,unordered_map和unordered_set相比于map和set会消耗更多的内存空间。因此,在对数据有排序或者对空间有要求的情况下,选择map和set;而对于对效率有要求的情况,选择unordered_map和unordered_set更合适。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [STL详解(十二)—— unordered_set、unordered_map的介绍及使用](https://blog.csdn.net/chenlong_cxy/article/details/122277348)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [模拟实现unordered_map和unordered_set超详解(C++)](https://blog.csdn.net/m0_67430750/article/details/124760725)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [unordered_set和unordered_map的使用【STL】](https://blog.csdn.net/m0_63312733/article/details/128000844)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值