离散化

板子

int a[maxn], b[maxn];//a[i]为原数组,b[i]为离散化后的数组
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d", &a[i]);
        b[i] = a[i];
    }
    sort(a + 1, a + 1 + n);
    int len = unique(a + 1, a + 1 + n) - (a + 1);//排序去重后的长度
    for(int i = 1; i <= n; ++i) b[i] = lower_bound(a + 1, a + 1 + n, b[i]) - a;//离散化结果从1开始,-a
    for(int i = 1; i <= n; ++i) printf("%d ", b[i]);
}

 

posted @ 2020-10-09 09:09  .Ivorelectra  阅读(105)  评论(0编辑  收藏  举报