Advertisement
El_GEMMY

wavio sequence (fenwick tree)

Jun 4th, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 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 findmod(x, m) x = ((x) % (m) + (m)) % m
  38. #define getmod(x, m) ((x) % (m) + (m)) % (m)
  39. #define debug(x) cout << "x: " << (x) << nl;
  40. #define debug2(x, y) cout << "x: " << (x) << " y: " << y << nl;
  41. #define ordered_set tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>
  42. #define ordered_map tree<int, int, less<>, rb_tree_tag, tree_order_statistics_node_update>
  43.  
  44. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  45. //vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
  46.  
  47. template<typename T = int> istream& operator>>(istream& in, vector<pair<int, int>>& v){
  48.     for (auto& [x, y] : v) in >> x >> y;
  49.     return in;
  50. }
  51.  
  52. template<typename T = int> istream& operator>>(istream& in, vector<T>& v){
  53.     for (T& i : v) in >> i;
  54.     return in;
  55. }
  56.  
  57. template<typename T = int> ostream& operator<<(ostream& out, const vector<T>& v){
  58.     for (const T& x : v)
  59.         out << x << ' ';
  60.     return out;
  61. }
  62.  
  63. template<typename T = pair<int, int>> ostream& operator << (ostream& out, const vector<pair<int, int>>& v){
  64.     for(auto& [x, y] : v){
  65.         out << x << ' ' << y << nl;
  66.     }
  67.     return out;
  68. }
  69.  
  70. void Start_Crushing() {
  71.     ios_base::sync_with_stdio(false);
  72.     cin.tie(nullptr);
  73.     cout.tie(nullptr);
  74. #ifndef ONLINE_JUDGE
  75.     freopen("input.txt", "r", stdin);
  76.     freopen("output.txt", "w", stdout);
  77. #endif
  78. }
  79.  
  80. template<typename T>
  81. struct fenwick_tree{
  82.     int n{};
  83.     vector<T> bit;
  84.  
  85.     explicit fenwick_tree(int n){
  86.         this -> n = n;
  87.         bit.assign(10005, 0);
  88.     }
  89.  
  90.     void add(int idx, int value){
  91.         while(idx <= 10000){
  92.             updmax(bit[idx], value);
  93.             idx += idx & -idx;
  94.         }
  95.     }
  96.  
  97.     T query(int idx){
  98.         T ret = 0;
  99.         while(idx){
  100.             updmax(ret, bit[idx]);
  101.             idx ^= idx & -idx;
  102.         }
  103.         return ret;
  104.     }
  105.  
  106.     void assign(int size, int value){
  107.         this -> n = size;
  108.         bit.assign(size, value);
  109.     }
  110. };
  111.  
  112. vector<int> co;
  113.  
  114. int idx(int x){
  115.     return int(upper_bound(all(co), x) - co.begin()) + 1;
  116. }
  117.  
  118. void solve(){
  119.     int n;
  120.     while(cin >> n){
  121.         vector<int> v(n); cin >> v;
  122.  
  123.         co = v;
  124.         sort(all(co));
  125.         co.erase(unique(all(co)), co.end());
  126.  
  127.         int ans = 1;
  128.         fenwick_tree<int> ft(n);
  129.  
  130.         vector<int> lis(n), lds(n);
  131.  
  132.         for (int i = 0; i < n; i++) {
  133.             ft.add(idx(v[i]), ft.query(idx(v[i]) - 1) + 1);
  134.             lis[i] = ft.bit[idx(v[i])];
  135.         }
  136.         ft.assign(10005, 0);
  137.         reverse(all(v));
  138.  
  139.         for (int i = 0; i < n; i++) {
  140.             ft.add(idx(v[i]), ft.query(idx(v[i]) - 1) + 1);
  141.             lds[n - i - 1] = ft.bit[idx(v[i])];
  142.         }
  143.  
  144.         for (int i = 0; i < n; i++) {
  145.             int len = min(lis[i], lds[i]);
  146.             updmax(ans, len * 2 - 1);
  147.         }
  148.         cout << ans << nl;
  149.     }
  150. }
  151.  
  152. int main(){
  153.     Start_Crushing();
  154. //    freopen("perm.in", "r", stdin);
  155.  
  156.     int t = 1;
  157. //    /*Multiple test cases?*/ cin >> t;
  158.     while (t--) {
  159.         solve();
  160. //        if(!t)
  161. //            break;
  162. //        cout << nl;
  163.     }
  164.  
  165. //    for(int tc = 1; tc <= t; tc++){
  166. //        cout << "Case #" << tc << ": ";
  167. //        solve();
  168. //        if(tc != t)
  169. //            cout << nl;
  170. //    }
  171.  
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement