离散化

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[500005],b[500005],n;
int main() {
    scanf("%d",&n);
    for(int i = 1;i <= n;++ i) scanf("%d",&a[i]),b[i] = a[i];
    sort(b + 1,b + n + 1);
    int c = unique(b + 1,b + n + 1) - b - 1;//去重
    for(int i = 1;i <= n;++ i) a[i] = lower_bound(b + 1,b + c + 1,a[i]) - b;
    return 0;
}

unique 函数去重。
lower_bound 返回第一个大于等于 \(val\) 的位置。
upper_bound 返回第一个大于 \(val\) 的位置。

posted @ 2022-11-13 21:09  eegg  阅读(42)  评论(0)    收藏  举报