离散化

把长度为 nn 的 aa 数组映射成只有 1∼tot1\sim tot 的整数的 bb 数组,其中返回值 tottot 是 aa 中元素个数。

int discrete(int n,int *a){
    int b[N];
    for(int i=1;i<=n;i++)
        b[i]=a[i];
    sort(b+1,b+n+1);
    int tot=unique(b+1,b+n+1)-(b+1);
    for(int i=1;i<=n;i++)
        a[i]=lower_bound(b+1,b+tot+1,a[i])-b;
    return tot;
}

空间复杂度 O(n)O(n),时间复杂度 O(nlog⁡n)O(n\log n)。

back

posted @ 2021-08-28 23:07  luckydrawbox  阅读(28)  评论(0)    收藏  举报  来源