程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu1025-Constructing Roads In JGShinings Kingdom

hdu1025-Constructing Roads In JGShinings Kingdom

編輯:C++入門知識

最長上升子序列(長度)


[cpp] 
#include<iostream>  
#include<cstdio>  
#include<cstring>  
 
using namespace std;  
int n ; 
const int maxn = 500005 ; 
int dp[ maxn ] , a[ maxn ]; 
 
int LIS(int n ) 

    int low , len , high , mid ; 
    len = 1 ; 
    //  mid = ( low + high ) / 2 ;  
    dp[ 1 ] = a[ 1 ] ; 
    for( int i = 2 ; i <= n ; i++ ) 
    { 
        low = 1 ; 
        high = len ; 
        while( low <= high ) 
        { 
            mid = ( low + high ) / 2 ; 
            if( a[ i ] > dp[ mid ] ) 
                 low = mid + 1 ; 
            else 
                high = mid - 1 ; 
        } 
        dp[ low ] = a[ i ] ; 
        if( low > len ) 
            len = low ; 
    } 
    return len ; 

  
 
int main() 

    int i , j , n ;  
    int flag = 1 ; 
    while( ~scanf( "%d" , &n )  ) 
    { 
        memset( dp , 0 ,sizeof( dp ) ) ; 
        memset( a , 0 ,sizeof( a ) ) ; 
        for( i = 0 ; i < n ; ++i ) 
        { 
            int x , y ; 
            scanf( "%d%d" , &x , &y ); 
            a[ x ] = y ; 
        } 
        int ans = LIS( n ) ; 
        printf( "Case %d:\n" , flag++ ) ; 
        if( ans == 1 )   
            printf( "My king, at most 1 road can be built.\n" ) ; 
        else 
            printf( "My king, at most %d roads can be built.\n" , ans ) ; 
        printf( "\n" ) ; 
    } 
    return 0  ; 

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
int n ;
const int maxn = 500005 ;
int dp[ maxn ] , a[ maxn ];

int LIS(int n )
{
 int low , len , high , mid ;
 len = 1 ;
 // mid = ( low + high ) / 2 ;
 dp[ 1 ] = a[ 1 ] ;
 for( int i = 2 ; i <= n ; i++ )
 {
  low = 1 ;
  high = len ;
  while( low <= high )
  {
   mid = ( low + high ) / 2 ;
   if( a[ i ] > dp[ mid ] )
     low = mid + 1 ;
   else
    high = mid - 1 ;
  }
  dp[ low ] = a[ i ] ;
  if( low > len )
   len = low ;
 }
 return len ;
}
 

int main()
{
 int i , j , n ; 
 int flag = 1 ;
 while( ~scanf( "%d" , &n )  )
 {
  memset( dp , 0 ,sizeof( dp ) ) ;
  memset( a , 0 ,sizeof( a ) ) ;
  for( i = 0 ; i < n ; ++i )
  {
   int x , y ;
   scanf( "%d%d" , &x , &y );
   a[ x ] = y ;
  }
  int ans = LIS( n ) ;
  printf( "Case %d:\n" , flag++ ) ;
  if( ans == 1 ) 
   printf( "My king, at most 1 road can be built.\n" ) ;
  else
   printf( "My king, at most %d roads can be built.\n" , ans ) ;
  printf( "\n" ) ;
 }
 return 0  ;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved