Advertisement
i_love_rao_khushboo

Untitled

Sep 15th, 2022 (edited)
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.76 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define ld long double
  6. #define ull unsigned long long
  7. #define pb push_back
  8. #define ppb pop_back
  9. #define mp make_pair
  10. #define F first
  11. #define S second
  12. #define PI 3.1415926535897932384626
  13. #define sz(x) ((int)(x).size())
  14.  
  15. typedef pair<int, int> pii;
  16. typedef pair<ll, ll> pll;
  17. typedef vector<int> vi;
  18. typedef vector<ll> vll;
  19. typedef vector<ull> vull;
  20. typedef vector<bool> vb;
  21. typedef vector<char> vc;
  22. typedef vector<pii> vpii;
  23. typedef vector<pll> vpll;
  24. typedef vector<vi> vvi;
  25. typedef vector<vll> vvll;
  26. typedef vector<vull> vvull;
  27. typedef vector<vb> vvb;
  28. typedef vector<vc> vvc;
  29.  
  30. /************************************************** DEBUGGER *******************************************************************************************************/
  31.  
  32. #ifndef ONLINE_JUDGE
  33. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  34. #else
  35. #define debug(x)
  36. #endif
  37.  
  38. void _print(ll t) { cerr << t; }
  39. void _print(int t) { cerr << t; }
  40. void _print(string t) { cerr << t; }
  41. void _print(char t) { cerr << t; }
  42. void _print(ld t) { cerr << t; }
  43. void _print(double t) { cerr << t; }
  44. void _print(ull t) { cerr << t; }
  45.  
  46. template <class T, class V> void _print(pair <T, V> p);
  47. template <class T> void _print(vector <T> v);
  48. template <class T> void _print(vector <vector<T>> v);
  49. template <class T> void _print(set <T> v);
  50. template <class T, class V> void _print(map <T, V> v);
  51. template <class T> void _print(multiset <T> v);
  52. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  53. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  54. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  55. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  56. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  57. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  58.  
  59. /*******************************************************************************************************************************************************************/
  60.  
  61. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  62. int rng(int lim) {
  63.     uniform_int_distribution<int> uid(0,lim-1);
  64.     return uid(rang);
  65. }
  66.  
  67. const int INF = 0x3f3f3f3f;
  68. const int mod = 1e9+7;
  69.  
  70. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  71.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  72.                          
  73. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  74. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  75.  
  76. /******************************************************************************************************************************/
  77.  
  78. int len_LIS(vi &v, int n, int prev) {
  79.     // base case
  80.     if(n == 0) return 0;
  81.    
  82.     // choice diagram code
  83.     // for each element of v[], we have 2 choices ==>
  84.     // Choice 1: exclude the current element and process the remaining elements
  85.     // Choice 2: include the current element if it is smaller than prev element in LIS
  86.    
  87.     int exclude = len_LIS(v, n - 1, prev);
  88.    
  89.     int include = 0;
  90.     if(v[n-1] < prev) include = 1 + len_LIS(v, n - 1, v[n-1]);
  91.    
  92.     // return maximum of above two choices
  93.     return max(exclude, include);
  94. }
  95.  
  96. void solve()
  97. {
  98.     int n; cin >> n;
  99.     vi v(n);
  100.     for(int i = 0; i < n; i++) cin >> v[i];
  101.    
  102.     cout << len_LIS(v, n, INT_MAX) << "\n";
  103. }
  104.  
  105. int main()
  106. {
  107.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  108.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  109.  
  110.     // #ifndef ONLINE_JUDGE
  111.     //     freopen("input.txt", "r", stdin);
  112.     //     freopen("output.txt", "w", stdout);
  113.     // #endif
  114.    
  115.     // #ifndef ONLINE_JUDGE
  116.     //      freopen("error.txt", "w", stderr);
  117.     // #endif
  118.    
  119.     int t = 1;
  120.     // int test = 1;
  121.     // cin >> t;
  122.     while(t--) {
  123.         // cout << "Case #" << test++ << ": ";
  124.         solve();
  125.     }
  126.  
  127.     return 0;
  128. }
  129.  
  130. /* # prev is initialised with INT_MAX in the main() fⁿ
  131.  
  132.    # Time Complexity: The time complexity of this recursive approach is exponential (O(2ⁿ)) as there
  133.                       is a case of overlapping subproblems.
  134.                      
  135.    # Auxiliary Space: O(1). No external space used for storing values apart from the internal
  136.                             stack space (which use O(n) space).
  137. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement