Advertisement
El_GEMMY

Hussin and Arrays 2 (partial sum + bs)

Mar 31st, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 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 inf 2000000000
  22. #define nl '\n'
  23. #define modulo(a, b, mod) ((((a) % mod) * ((b) % mod)) % mod)
  24. #define debug(x) cout << "x: " << x << nl;
  25. #define debug2(x, y) cout << "x: " << x << " y: " << y << nl;
  26. #define ordered_set tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>
  27. #define ordered_map tree<int, int, less<>, rb_tree_tag, tree_order_statistics_node_update>
  28.  
  29. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  30. //vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
  31.  
  32. template<typename T = int> istream& operator>>(istream& in, vector<pair<int, int>>& v){
  33.     for (auto& [x, y] : v) in >> x >> y;
  34.     return in;
  35. }
  36.  
  37. template<typename T = int> istream& operator>>(istream& in, vector<T>& v){
  38.     for (T& i : v) in >> i;
  39.     return in;
  40. }
  41.  
  42. template<typename T = int> ostream& operator<<(ostream& out, const vector<T>& v){
  43.     for (const T& x : v)
  44.         out << x << ' ';
  45.     return out;
  46. }
  47.  
  48. template<typename T = pair<int, int>> ostream& operator << (ostream& out, const vector<pair<int, int>>& v){
  49.     for(auto& [x, y] : v){
  50.         out << x << ' ' << y << nl;
  51.     }
  52.     return out;
  53. }
  54.  
  55. void Start_Crushing() {
  56.     ios_base::sync_with_stdio(false);
  57.     cin.tie(nullptr);
  58.     cout.tie(nullptr);
  59. #ifndef ONLINE_JUDGE
  60.     freopen("input.txt", "r", stdin);
  61.     freopen("output.txt", "w", stdout);
  62. #endif
  63. }
  64.  
  65. void solve(){
  66.     int n; cin >> n;
  67.     vector<int> v(n), partial(n); cin >> v;
  68.  
  69.     partial.back() = v.back();
  70.     for(int i = 1; i < n; i++){
  71.         partial[n - i - 1] = max(partial[n - i], v[n - i - 1]);
  72.     }
  73.  
  74.  
  75.     int ans = 0;
  76.     for(int i = 0; i < n; i++){
  77.         int left = i, right = n;
  78.  
  79.         while(left < right - 1){
  80.             int mid = left - (left - right) / 2;
  81.  
  82.             if(partial[mid] >= v[i])
  83.                 left = mid;
  84.             else
  85.                 right = mid;
  86.         }
  87.         ans = max(ans, left - i);
  88.     }
  89.     cout << ans;
  90. }
  91.  
  92. int main(){
  93.     Start_Crushing();
  94.  
  95.     int t = 1;
  96. //    /*Multiple test cases?*/ cin >> t;
  97.     while (t--) {
  98.         solve();
  99.         if(!t) break;
  100.         cout << nl;
  101.     }
  102.  
  103. //    for(int tc = 1; tc <= t; tc++){
  104. //        cout << "Case #" << tc << ": ";
  105. //        solve();
  106. //        if(tc != t)
  107. //            cout << nl;
  108. //    }
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement