其他分享
首页 > 其他分享> > 离散化

离散化

作者:互联网

形式一(无重复元素):

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 ;
}

 

形式二(有重复元素):

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 ;
}

 

形式三:map容器

标签:Node,sort,const,int,cin,离散,1e5
来源: https://www.cnblogs.com/xqk0225/p/16078501.html