discretization

  It should have been a very simple thing, but I didn't listen to it when the seniors talked before, and now I am a bit at a loss when I encounter a topic that needs to be discretized. After reading the blogs of the big guys on the Internet, I basically understand it and make a record.

  The following content ideas and some code samples come from:

  https://blog.csdn.net/xiangaccepted/article/details/73276826


  

  Discretization, mapping finite individuals in infinite space to finite space, so as to improve the space-time efficiency of the algorithm. This is the definition on Baidu Encyclopedia. So, for example, a certain topic tells you that there are 1e5 numbers, each of which is not more than 1e9 in size, and requires you to operate on these numbers (such as union search and the like). Then it is definitely not possible to directly open an array of size 1e9, but the range of 1e5 is completely fine. For example, if {4,7,6,9} is now discretized, the result is {1,3,2,4}, that is, when we do not need the specific data , you just need to know their relative sizes.

 

 


 

  

  There are two ways to discretize:

  First, look at a piece of code:  

const int N=1e5+7;
int t[N],a[N];
int main()
{
  cin>>n;
  for(int i=1;i<=n;i++)
    cin>>a[i],t[i]=a[i];
  sort(t+1,t+n+1);
  m=unique(t+1,t+n+1)-t-1;
  for(int i=1;i<=n;i++)
    a[i]=lower_bound(t+1,t+m+1,a[i])-t;
}

 

  In this code, a[] is discretized, and the range becomes m. By the way, explain, unique is a function that comes with C++, which means to deduplicate a sequence, and then return the number of elements that are not repeated, of course, subtract the first address later. Then this discretization can also be applied to sequences with repeated elements, but the complexity will be higher than the second method to be discussed later.

  take a chestnut

  {6,8,4,9,5,6,7,4}, first sort to get {4,4,5,6,6,7,8,9}, deduplicate {4,5,6,7 ,8,9}, then the original sequence becomes {3,5,1,6,2,3,4,1}.

  

  Second, the complexity is better than the above one, but it cannot handle repeated elements.

  Look at the code first:

const int N=1e5+7;
struct Node{
  int v,id;
  bool operator < (const Node a)const{
    return v<a.v;}//排序用
}a[N];
int n,rank[N];
int main()
{
  cin>>n;
  for(int i=1;i<=n;i++){
    cin>>a[i].v;
    a[i].id=i;}
  sort(a+1,a+n+1);
  for(int i=1;i<=n;i++)
    rank[a[i].id]=i;
}

  This method directly uses the structure to store the position of the elements of the original array, and then reassigns them after sorting. Then rank[] is the result of discretization of the structure a[].

  Take a chestnut:

  v: 3 6 5 10 8

  id:1 2 3 4 5

  After sorting:

  v: 3 5 6 8 10

  id:1 3 2 5 4

  So after discretization:

  v: 3 5 6 8 10

  id:1 3 2 5 4

  rk:1 2 3 4 5

  In the original order:

  v: 3 6 5 10 8

  rk:1 3 2 5 4

  The above is what Konjac has roughly mastered today. Welcome everyone to step on it and give more advice and advice to Konjac.

 

Guess you like

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