Maruf_Hasan

LIS nlogn soln

Jun 3rd, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1.  
  2. //In the Name of Allah Most Gracious, Most Merciful//
  3. /*If you want something you've never had, you have to do something you never did.*/
  4.  
  5.  
  6.  
  7. //LIS nlogn implementation
  8.  
  9. #include<bits/stdc++.h>
  10. using namespace std;
  11.  
  12.  
  13. #define pb push_back
  14. #define ll long long
  15. #define pii pair<int,int>
  16. #define pll pair<ll,ll>
  17. #define M 100007
  18. #define INF 1e9
  19. #define INFL 1e18
  20. #define PI acos(-1)
  21. #define mp make_pair
  22. #define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  23. //const int fx[]= {+1,-1,+0,+0};
  24. //const int fy[]= {+0,+0,+1,-1};
  25. //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
  26. //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
  27. //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
  28. //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
  29.  
  30. vector<int>v;
  31. vector<int>tail(M,0);
  32.  
  33. int Ceilindex(int l,int r,int key)
  34. {
  35. while(r-l>1)
  36. {
  37. int m=l+(r-l)/2;
  38. if(v[m]>=key)
  39. {
  40. r=m;
  41. }
  42. else
  43. {
  44. l=m;
  45. }
  46. }
  47. return r;
  48. }
  49. int longestIncreasingSubsequenceLength()
  50. {
  51. if(v.size()==0)
  52. return 0;
  53. int length=1;
  54. tail[0]=v[0];
  55. for(int i=0;i<v.size();i++)
  56. {
  57. if(v[i]<tail[0])
  58. {
  59. tail[0]=v[i];
  60. }
  61. else if(v[i]>tail[length-1])
  62. {
  63. tail[length++]=v[i];
  64. }
  65. else
  66. {
  67. tail[Ceilindex(-1,length-1,v[i])]=v[i];
  68. }
  69. }
  70. return length;
  71. }
  72. int main()
  73. {
  74. fast_in_out;
  75. //freopen("input.txt","r",stdin);
  76. //freopen("output.txt","w",stdout);
  77. int n;
  78. cin>>n;
  79. for(int i=0;i<n;i++)
  80. {
  81. int x;
  82. cin>>x;
  83. v.pb(x);
  84. }
  85. int ans=longestIncreasingSubsequenceLength();
  86. cout<<ans<<endl;
  87.  
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment