数据离散化

数据离散化

有一些题中,题目给的数据很大,但是数据的大小本身对我们的题目没有影响,这时候,我们可以将数据离散化

使用lower_bound()进行数据离散化

#include <cstdio>
#include <algorithm>
#include <iostream>
int const maxn = 100000;
using namespace std;

int main() {
	int n;
	cin >> n;
	int a[maxn], b[maxn];
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		b[i] = a[i];
	}

	int true_n = unique(b + 1, b + n + 1) - b - 1;

	for (int i = 1; i <= n; i++) {
		a[i] = lower_bound(b + 1, b + true_n +1, a[i]) - b;
	}

	for (int i = 1; i <= n; i++) {
		cout << a[i] << endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/woxiaosade/p/11421950.html