447C - DZY Loves Sequences - CodeForces Solution


dp *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>indexed_multiset;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>indexed_set;


#pragma GCC optimize("Ofast")  
//#pragma GCC target("avx,avx2,fma") 
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("-O2")

#define all(a)             (a).begin(), (a).end()
#define ll                 long long
#define fi                 first
#define se                 second
#define pb                 push_back
#define pob                pop_back
#define bb                 begin()
#define ee                 end()
#define endll              '\n'

#define pii             pair<int,int>
#define pll             pair<long long int,long long int>
#define vi              vector<int>
#define vll             vector<long long int>
#define vii             vector<pii>
#define vpll            vector<pll>
 
#define inf               (1ll<<30ll)-1ll
#define llinf             (1ll<<62ll)-1ll

#define mod                1000000007
#define n_size             200005
#define g_size             20000005
#define eps                1e-9

const double PI=acos(-1);

//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 way Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[]={-1,-1,+0,+1,+1,+0};int dy[]={-1,+1,+2,+1,-1,-2}; //Hexagonal Direction

vi v;

inline void solve(){

    int n,m,t,res;

    int l,r,mid;

    int maxi=-inf,mini=inf,count=0;

    int i,j,k;

    bool flag;

    vi prefix,suffix;

    cin>>n;
    v.assign(n+2,0);
    prefix.assign(n+2,0);
    suffix.assign(n+2,0);

    v[0]=-inf;
    v[n+1]=inf;

    for(i=1;i<=n;i++) cin>>v[i];

    if(n==1){
        cout<<1<<endl;
        return;
    }

    for(i=1;i<=n;i++){
        if(v[i-1]<v[i]) prefix[i]=prefix[i-1]+1;
        else prefix[i]=1; 
    }

    suffix[n]=1;
    for(i=n-1;i>=1;i--){
        if(v[i]<v[i+1]) suffix[i]=suffix[i+1]+1;
        else suffix[i]=1;
    }

    for(i=1;i<n;i++){
        if(v[i+1]-v[i-1]>=2){
            maxi=max(maxi,prefix[i-1]+suffix[i+1]+1);
        }
        else maxi=max(maxi,prefix[i]+1);
    }

    for(i=n;i>=2;i--){
        maxi=max(maxi,suffix[i]+1);
    }

    // for(i=0;i<=n+1;i++) cout<<prefix[i]<<" ";
    // cout<<endl;
    // for(i=0;i<=n+1;i++) cout<<suffix[i]<<" ";
    // cout<<endl;

    cout<<maxi<<endl;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    //freopen("input.in","r",stdin);
    //freopen("output.out","w",stdout);
    //cout<<fixed<<setprecision(11);


    int T=1;
    //cin>>T;

    while(T--) solve();
    
    
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST