Iamtui1010

sequence_decomposing_fenwick

Dec 10th, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<cstdlib>
  5. #include<set>
  6.  
  7. #define long long long
  8. #define nln '\n'
  9.  
  10. using namespace std;
  11.  
  12. vector<long> dp;
  13.  
  14. void update(long ind, long val)
  15. {
  16. while (ind < (long)dp.size())
  17. {
  18. dp[ind] = max(dp[ind], val);
  19. ind += ind&-ind;
  20. }
  21. }
  22.  
  23. long get(long ind)
  24. {
  25. long mav = 0;
  26. while (ind > 0)
  27. {
  28. mav = max(mav, dp[ind]);
  29. ind -= ind&-ind;
  30. }
  31. return mav;
  32. }
  33.  
  34. int main()
  35. {
  36. // Input
  37. //freopen("lis.inp", "r", stdin);
  38. long n;
  39. cin >> n;
  40. vector<long> a(n+1, 0);
  41. for (long i = 1; i <= n; ++i)
  42. cin >> a[i];
  43. reverse(a.begin()+1, a.end());
  44. // Compress number
  45. set<long> num(a.begin()+1, a.end());
  46. vector<long> tea(num.begin(), num.end());
  47. reverse(tea.begin(), tea.end());
  48. tea.push_back(0);
  49. reverse(tea.begin(), tea.end());
  50.  
  51. for (long i = 1; i <= n; ++i)
  52. a[i] = lower_bound(tea.begin()+1, tea.end(), a[i]) - tea.begin();
  53.  
  54. // process by Fenwick
  55. long ans = 0;
  56. dp.resize(n+1, 0);
  57. for (long i = 1; i <= n; ++i)
  58. {
  59. long res = get(a[i]);
  60. update(a[i], res+1);
  61. ans = max(ans, res+1);
  62. }
  63. cout << ans << nln;
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment