Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int inf = 0x3f3f3f3f;
  6.  
  7. vector<int>sequence,LIS_sequence,L;
  8.  
  9. int n;
  10.  
  11. int LisNlogN()
  12. {
  13.     int i;
  14.  
  15.     int I[n+1];
  16.  
  17.     I[0] = -inf;
  18.  
  19.     for(i=1; i<=n; i++)
  20.     {
  21.         I[i] = inf;
  22.     }
  23.  
  24.     int Lislength = 0;
  25.  
  26.     for(i=0; i<n; i++)
  27.     {
  28.         int low,high,mid;
  29.  
  30.         low = 0;
  31.  
  32.         high = Lislength;
  33.  
  34.         while(low<=high)
  35.         {
  36.             mid = (low+high)/2;
  37.  
  38.             if(I[mid]<sequence[i])
  39.             {
  40.                 low = mid + 1;
  41.             }
  42.             else
  43.             {
  44.                 high = mid - 1;
  45.             }
  46.         }
  47.  
  48.         I[low] = sequence[i];
  49.  
  50.         L.push_back(low);
  51.  
  52.         if(Lislength<low)
  53.         {
  54.             Lislength = low;
  55.         }
  56.     }
  57.  
  58.     return Lislength;
  59. }
  60.  
  61. void findSequence(int maxLength)
  62. {
  63.     int i,j;
  64.  
  65.     i = 0;
  66.  
  67.     for(j=1; j<n; j++)
  68.     {
  69.         if(L[j]>=L[i])
  70.         {
  71.             i = j;
  72.         }
  73.     }
  74.  
  75.     int top = L[i] - 1;
  76.  
  77.     LIS_sequence.push_back(sequence[i]);
  78.  
  79.     for(j = i-1 ; j>=0 ; j--)
  80.     {
  81.         if( sequence[j]<sequence[i] && (L[j] == L[i]-1) )
  82.         {
  83.             i = j;
  84.  
  85.             LIS_sequence.push_back(sequence[i]);
  86.  
  87.         }
  88.     }
  89.  
  90.     for(i=maxLength-1; i>=0; i--)
  91.     {
  92.         printf("%d\n",LIS_sequence[i]);
  93.     }
  94.  
  95.     return ;
  96. }
  97.  
  98. int main()
  99. {
  100.  
  101.     int i,a;
  102.  
  103.     while(scanf("%d",&a)!=EOF)
  104.     {
  105.         sequence.push_back(a);
  106.     }
  107.  
  108.     n = sequence.size();
  109.  
  110.     int res = LisNlogN();
  111.  
  112.     printf("%d\n-\n",res);
  113.  
  114.     findSequence(res);
  115.  
  116.     return 0;
  117. }