319B - Psychos in a Line - CodeForces Solution


data structures implementation *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int  mod = 1000000007; 

/*=================================Debug==================================*/
template <typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << '(' << p.first << ", " << p.second << ')'; }
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream& os, const T_container& v) { os << '{'; string sep; for (const T& x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << " " << H; dbg_out(T...); }
 
//#define MY
#ifdef MY
#define debug(args...) cerr << "(" << #args << "):", dbg_out(args)
#else
#define debug(args...)
#endif
int tree[4000009];
void update(int node, int l , int r, int idx, int step ){
    if(l>idx or r<idx)return ; 
    if(l==r){
        tree[node]= step ; 
        return ; 
    }
    int mid =l+r>>1;
    update(node<<1, l, mid, idx, step); 
    update((node<<1)|1, mid+1, r , idx, step); 
    tree[node]=max(tree[node*2], tree[node*2+1]);
}
int query(int node, int l , int r , int st, int ed ){
    debug(node);
    if(l>ed or r<st)return 0; 
    if(l>=st and r<=ed){
        return tree[node];
    }
    return max(query(node<<1, l, (l+r>>1), st, ed), query((node<<1)|1, ((l+r>>1)+1),r, st, ed));
}
int main() {
    //return 0;
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int n; 
    cin>>n; 
    vector<int > v; 
    for(int i =1;i <=n;i++){
        int x; 
        cin>>x; 
        v.push_back(x); 
    }
    int ans =0;
    stack<int> s; 
    s.push(0);
    for(int i=1;i<n; i++){
        while( not s.empty() and v[s.top()]<v[i]){
            s.pop(); 
        }
        debug(s.top());
        int step=-1 ; 
        if(not s.empty()){
            step= query(1, 0, n, s.top()+1, i)+1; 
            debug(step); 
        }
        ans =max(ans, step);
        update(1, 0, n, i, step); 
        s.push(i);
    }
    cout<<ans<<endl; 
    return 0;
}
		 		 		  	 			 	   			 	  				


Comments

Submit
0 Comments
More Questions

Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array