discretization [template]

First, let's take a look at what discretization is.
For example, you have some very large numbers, and now you need to do some processing on them (such as hashing or something), when only their relative size relationship is needed, you can perform discretization to save space complexity.
Original number:

45451212 3212313215321 548468354545122545 78461

After discretization:

2 3 4 1

The relative size is unchanged, and sorting is also possible.
So how to achieve discretization?
look at the code

#include<iostream>
#include<cstdio>
#include<algorithm>//要用到几个复杂的函数,大家可以查一下
using namespace std;
int a[1005],b[1005];//a为原数,b为离散化后的数 
int n;
int cnt;
int main() 
{  
    cin>>n; 
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        b[++cnt]=a[i];//先存进b去再处理
    }
    sort(b+1,b+1+cnt);//排序
    cnt=unique(b+1,b+1+cnt)-b-1;//去重(离散化是去重的)
    for(int i=1;i<=n;i++)
        cout<<lower_bound(b+1,b+1+cnt,a[i])-b<<endl;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325690162&siteId=291194637