离散化
板子
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]); }