DuongNhi99

LIS - Dãy con tăng dài nhất (Sol 2)

Nov 23rd, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN = 3e4 + 5;
  5.  
  6. int n;
  7. int64_t a[maxN];
  8.  
  9. int64_t d[maxN];
  10.  
  11. int main() {
  12. #ifdef LOCAL
  13.     freopen("in1.txt", "r", stdin);
  14. #else
  15.     freopen("LIS.inp", "r", stdin);
  16.     freopen("LIS.out", "w", stdout);
  17. #endif
  18.     ios_base::sync_with_stdio(false);
  19.     cin.tie(nullptr);
  20.  
  21.     cin >> n;
  22.     for (int i = 1; i <= n; ++i)
  23.         cin >> a[i];
  24.  
  25.     int64_t ans = 0;
  26.     for (int i = 1; i <= n; ++i) {
  27.         int64_t left = 1, right = ans;
  28.         int64_t p = -1;
  29.  
  30.         while (left <= right) {
  31.             int64_t mid = (left + right) / 2;
  32.             if (d[mid] >= a[i])
  33.                 p = mid, right = mid - 1;
  34.             else left = mid + 1;
  35.         }
  36.  
  37.         if (p == -1) d[++ans] = a[i];
  38.         else d[p] = a[i];
  39.     }
  40.     cout << ans << '\n';
  41.  
  42.     return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment