Advertisement
dorin98

LIS

Feb 16th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. #define dim 100001
  5.  
  6. using namespace std;
  7.  
  8. ifstream fin("scmax.in");
  9. ofstream fout("scmax.out");
  10.  
  11. int n,v[dim];
  12. int sc[dim],urm[dim],len;
  13.  
  14. void readData()
  15. {
  16.     fin>>n;
  17.     for (int i=1; i<=n; ++i)
  18.         fin>>v[i];
  19. }
  20.  
  21. int binarySearch(int x)
  22. {
  23.     int i,step;
  24.     for (step=1; step<len; step<<=1);
  25.     for (i=0; step; step>>=1)
  26.         if (i+step<=len && v[sc[i+step]]<x) i+=step;
  27.     return i;
  28. }
  29.  
  30. void printRec(int x)
  31. {
  32.     if (urm[x]) printRec(urm[x]);
  33.     fout<<v[x]<<' ';
  34. }
  35.  
  36. void dp()
  37. {
  38.     sc[1]=1;
  39.     len=1;
  40.     for (int i=2; i<=n; ++i)
  41.     {
  42.         int x=v[i];
  43.         int poz=binarySearch(x);
  44.         if (poz==len) sc[++len]=i;
  45.         else if (v[sc[poz+1]]>x) sc[poz+1]=i;
  46.         urm[i]=sc[poz];
  47.     }
  48.     fout<<len<<'\n';
  49.     printRec(sc[len]);
  50. }
  51.  
  52. int main()
  53. {
  54.     readData();
  55.     dp();
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement