Advertisement
El_GEMMY

lis (diff by 1)

Apr 23rd, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. // Those who cannot remember the past are
  2. // condemned to repeat it (use DP -_-)
  3. // - George Santayana
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8.  
  9. using namespace std;
  10. using namespace __gnu_pbds;
  11.  
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define ll long long
  15. #define ull unsigned long long
  16. #define MOD 1000000007
  17. #define PI 3.14159265
  18. #define ceil(a, b) (((a) / (b)) + ((a) % (b) ? 1 : 0))
  19. #define imin INT_MIN
  20. #define imax INT_MAX
  21. #define llmax LLONG_MAX
  22. #define llmin LLONG_MIN
  23. #define inf 2000000000
  24. #define nl '\n'
  25. #define ppcnt __builtin_popcount
  26. #define ppcntll __builtin_popcountll
  27. #define clz __builtin_clz
  28. #define clzll __builtin_clzll
  29. #define ctz __builtin_ctz
  30. #define ctzll __builtin_ctzll
  31. #define modulo(a, b, mod) ((((a) % (mod)) + ((b) % (mod))) % (mod))
  32. #define cnte(v, x) count(all(v), (x))
  33. #define mine(v) min_element(all(v))
  34. #define maxe(v) max_element(all(v))
  35. #define updmin(a, b) a = min(a, b)
  36. #define updmax(a, b) a = max(a, b)
  37. #define finmod(x, m) x = ((x) % (m) + (m)) % m
  38. #define debug(x) cout << "x: " << (x) << nl;
  39. #define debug2(x, y) cout << "x: " << (x) << " y: " << y << nl;
  40. #define ordered_set tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>
  41. #define ordered_map tree<int, int, less<>, rb_tree_tag, tree_order_statistics_node_update>
  42.  
  43. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  44. //vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
  45.  
  46. template<typename T = int> istream& operator>>(istream& in, vector<pair<int, int>>& v){
  47.     for (auto& [x, y] : v) in >> x >> y;
  48.     return in;
  49. }
  50.  
  51. template<typename T = int> istream& operator>>(istream& in, vector<T>& v){
  52.     for (T& i : v) in >> i;
  53.     return in;
  54. }
  55.  
  56. template<typename T = int> ostream& operator<<(ostream& out, const vector<T>& v){
  57.     for (const T& x : v)
  58.         out << x << ' ';
  59.     return out;
  60. }
  61.  
  62. template<typename T = pair<ll, ll>> ostream& operator << (ostream& out, const vector<pair<ll, ll>>& v){
  63.     for(auto& [x, y] : v){
  64.         out << x << ' ' << y << nl;
  65.     }
  66.     return out;
  67. }
  68.  
  69. void Start_Crushing() {
  70.     ios_base::sync_with_stdio(false);
  71.     cin.tie(nullptr);
  72.     cout.tie(nullptr);
  73. #ifndef ONLINE_JUDGE
  74.     freopen("input.txt", "r", stdin);
  75.     freopen("output.txt", "w", stdout);
  76. #endif
  77. }
  78. vector<int> v, memo;
  79. vector<vector<int>> mp;
  80.  
  81. int bs(int target, vector<int>& range){
  82.     int left = -1, right = (int)range.size() - 1;
  83.  
  84.     while(left < right - 1){
  85.         int mid = left - (left - right) / 2;
  86.  
  87.         if(range[mid] > target)
  88.             right = mid;
  89.         else
  90.             left = mid;
  91.     }
  92.  
  93.     return range[right] > target ? right : -1;
  94. }
  95.  
  96. int lis(int idx = 0){
  97.     if(mp[v[idx] + 1].empty())
  98.         return 1;
  99.  
  100.     int& ret = memo[idx];
  101.     if(~ret)
  102.         return ret;
  103.  
  104.     ret = 1;
  105.     int st = bs(idx, mp[v[idx] + 1]);
  106.  
  107.     if(~st){
  108.         while(st != mp[v[idx] + 1].size()){
  109.             updmax(ret, lis(mp[v[idx] + 1][st++]) + 1);
  110.         }
  111.     }
  112.  
  113.     return ret;
  114. }
  115.  
  116. void solve(){
  117.     int n; cin >> n;
  118.     v.resize(n);
  119.     mp.assign(20005, vector<int>());
  120.     memo.assign(n + 5, -1);
  121.  
  122.     for(int i = 0; i < n; i++){
  123.         cin >> v[i];
  124.         mp[v[i]].emplace_back(i);
  125.     }
  126.  
  127.     int res = imin;
  128.     for(int i = 0; i < n; i++){
  129.         updmax(res, lis(i));
  130.     }
  131.     cout << res;
  132. }
  133.  
  134. int main(){
  135.     Start_Crushing();
  136.  
  137.     int t = 1;
  138.     /*Multiple test cases?*/ cin >> t;
  139.     while (t--) {
  140.         solve();
  141.         if(!t)
  142.             break;
  143.         cout << nl;
  144.     }
  145.  
  146. //    for(int tc = 1; tc <= t; tc++){
  147. //        cout << "Case #" << tc << ": ";
  148. //        solve();
  149. //        if(tc != t)
  150. //            cout << nl;
  151. //    }
  152.  
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement