Discrete summary

Three kinds of discrete methods

① unordered_map

Sorting, the heavy direct MP [Original Data Type] = Rank (O (1))

Do whatever they want.

Relative costs of space.

And I found no one else logN of discrete fast

About six times the speed difference, this is a big O (1) assignment problem ah

 

Summary: O (1) assignment, unrestricted, but slow

 

② For the data range is not particularly large, open directly int [data range] array

If the number of data is not much, it can be directed directly open more than 60,000 unsigned short data (output format% ud)

Go to sort heavy complexity of O (N * logN)

 

Summary: O (1) assignment, mapping array is too large can not afford to be open, but fast

 

int data[N];///辅助数组
void discrete(int *a,int n)
{
    for (int i=0;i<n;i++) data[i] = a[i];
    sort(data,data+n);
    int cnt = unique(data,data+n) - data;
    for (int i=0;i<n;i++)
        a[i] = lower_bound(data,data+cnt,a[i]) - data;
}

This method can be obtained with the corresponding discrete array of the original array, and find the original data from the data discretized

lowerbound here due to the weight corresponding to half directly find a [i] position

Also note that the second for loop is not to end cnt, cnt fact that no use

data [1] = 100, Data [2] = 1321, Data [3] = 20001

a[1] = 1,               a[2] = 3 ,              a[3] = 2

data[a[1]] = 100, data[a[2]] = 20001,data[a[3]] = 1321

Summary: O (logN) assignment, a little more than a waste of time and the actual ② ① point in time than the province, as well, do whatever they want.

 

Summarize the summary:

The amount of data is small, people are lazy to use the first

Data range is not enough space to use the second

Data can not take a large range map array, afraid to use a third timeout

mapping string int, not with a first

 

Please indicate there is misunderstanding, I just try holding the mentality to write

(Say you may not believe, I have come back five minutes changed twice a fatal mistake)

 

Guess you like

Origin blog.csdn.net/weixin_43768644/article/details/94465682