[Tips] - discrete

Reference from Baidu Encyclopedia :

But when a large number of small data range data, the data we need to be discrete, so help us process the data.

There are three discrete steps:

  1. Sort

  2. deduplication

  3. Index

You will be able to achieve very good use stl:

As an example, fresh:

 

 1     n=read();
 2     for(int i=1;i<=n;i++){
 3         h_[i]=read();
 4         h[i]=h_[i];
 5     }
 6     sort(h_+1,h_+n+1);
 7 //    int size=unique(h_+1,h_+n+1)-h_; 如果有重复元素的话 
 8     for(int i=1;i<=n;i++){
 9         h[i]=lower_bound(h_+1,h_+n+1,h[i])-h_;
10     }
11     for(int i=1;i<=n;i++) printf("%d ",h[i]);

 

Input:

 

5
-1 23 -34 5 6

 

Output:

2 5 1 3 4

Guess you like

Origin www.cnblogs.com/Nelson992770019/p/11404949.html