discretization idea

Given a list of numbers, in some cases the absolute magnitude of the values ​​of these numbers is not important, but the relative magnitude is. For example, the class scores are 99 98 97 95 96, and the ranking is 1 2 3 5 4.

Discretization is a data processing technique that converts widely distributed and sparsely distributed data into a dense distribution, so that the algorithm can be processed faster and more space-saving.

Discretization steps: sorting, discretization, homing.

Such as: 4000 210 11 45 800

  The array subscript is 1 2 3 4 5

Store them in a struct and sort them.

---------------------

Sort: 11 45 210 800 4000

                        3          4       2     5     1

------------------------

Discretization: 1 2 3 4 5

The subscript of the array: 3 4 2 5 1

----------------------

Homing: 5 3 1 2 4     

Array index 1 2 3 4 5

The following is the code analysis:

#define _CRT_SECURE_NO_WARNINGS 1
//离散化
#include<bits/stdc++.h>
using namespace std;
const int N = 500010;
struct data1 {
	int val;
	int id; // 用坐标代替
}olda[N];
int newa[N];
bool cmp(data1 x, data1 y) { return x.val < y.val; }
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &olda[i].val);
		olda[i].id = i;
	}
	sort(olda + 1, olda + 1 + n, cmp); //排序

	//离散化 + 归位 
	for (int i = 1; i <= n; i++) {
		newa[olda[i].id] = i;
		if (olda[i].val == olda[i - 1].val)
			newa[olda[i].id] = newa[olda[i - 1].id];
	}
	for (int i = 1; i <= n; i++)
		printf("%d", newa[i]);
	return 0;
}

Discretization is achieved with STL functions:

The lower_bound() and unique() functions implement discretization.

Find the first element equal to or greater than x: lower_bound() and equal to x. Position such as pos = lower_bound(a,a+n,x) - a.

unique() function:

unique is one of the very useful functions in the c++ standard template library STL, using this function requires #include <algorithm> header file

The function of this function is to "remove" repeated elements of adjacent elements in the container or array
(1) The removal here is not a real erase, but to put the repeated elements at the end of the container, and the return value is after deduplication The tail address of . 

Note this: it only works on adjacent elements.
(2) Unique is for adjacent elements, so for array members or container members that are out of order, they need to be sorted first, and the std::sort() function can be called.

#define _CRT_SECURE_NO_WARNINGS 1
//离散化
#include<bits/stdc++.h>
using namespace std;
const int N = 5000010;
int olda[N];
int newa[N];
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &olda[i]);
		newa[i] = olda[i]; //这一步很关键
	}
	sort(olda + 1, olda + n + 1);//默认是从小到大 less<int>()
	int cnt = n; // 记录个数 调用unique函数时,是去重的数组
	//cnt = unique(olda + 1, olda + 1 + n) - (olda + 1);//返回不重复的尾元素 如  1 1 2 3 调用函数后 1 2 3 1 返回 3 
	for (int i = 1; i <= cnt; i++) {
		newa[i] = lower_bound(olda + 1, olda + 1 + n, newa[i]) - olda;
	}
	for (int i = 1; i <= cnt; i++) {
		printf("%d  ", newa[i]);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/zhi6fui/article/details/128533920