remove alternating sub sequence code example

Example: alternating subsequence codeforces

#include<bits/stdc++.h>using namespace std; typedef long long ll;typedef long long int li;#define pb push_back#define ff first#define ss second#define FOR(n) for(int i=0;i<n;i++)#define FORD(n) for(int i=n-1;i>=0;i--) int main() {    ios_base::sync_with_stdio(false);     cin.tie(NULL); cout.tie(NULL);    //your code starts here    ll t; cin>>t;    while(t--)    {        ll n; cin>>n;        vector<ll> a(n);        for(int i=0;i<n;i++) cin>>a[i];        deque<ll> st;        st.push_front(a[0]);         int init_sign=a[0]/abs(a[0]);        for(int i=1;i<n;i++)        {            int sign=a[i]/abs(a[i]);            if(init_sign!=sign) st.push_front(a[i]);            else            {                if(a[i]>st.front()) {                    st.pop_front(); st.push_front(a[i]);                }            }            init_sign=sign;         }        ll ans=0;        for(auto x:st) ans+=x;        cout<<ans<<"\n";    }    return 0;}

Tags:

Cpp Example