codeforces446—— A.DZY Loves Sequences(DP+枚举)

简介: codeforces446—— A.DZY Loves Sequences(DP+枚举)

原题链接

题意:

20200401134307494.png

思路:

n只有1e5,枚举每一个点作为分界点,维护每个点的递增子序列和递减子序列,如果这两个序列能够接起来,答案就是两个序列和的长度,否则,就是两个序列的最大值。

代码:

const int maxn=1e5+100;
int a[maxn],l[maxn],r[maxn],n;
int main(){
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    a[0]=0;
    for(int i=1;i<=n;i++)
        if(a[i]>a[i-1]) l[i]=l[i-1]+1;
        else l[i]=1;
    a[n+1]=inf;
    for(int i=n;i;i--)
        if(a[i]<a[i+1]) r[i]=r[i+1]+1;
        else r[i]=1;
    int maxx=0;
    for(int i=1;i<=n;i++){///枚举分界点
        if(a[i-1]+1>=a[i+1]) maxx=max(maxx,max(l[i-1],r[i+1])+1);
        else maxx=max(maxx,r[i+1]+l[i-1]+1);
        ///cout<<l[i]<<" "<<r[i]<<" "<<maxx<<endl;
    }
    out(maxx);
    return 0;
}
目录
相关文章
|
人工智能
Codeforces1343D - Constant Palindrome Sum + UPC-鸭子游戏 (差分)
Codeforces1343D - Constant Palindrome Sum + UPC-鸭子游戏 (差分)
114 1
codeforces118——D. Caesar‘s Legions(DP)
codeforces118——D. Caesar‘s Legions(DP)
109 0
codeforces118——D. Caesar‘s Legions(DP)
codeforces1426——F. Number of Subsequences(DP)
codeforces1426——F. Number of Subsequences(DP)
120 0
|
机器学习/深度学习 Java
Codeforces Round #748 (Div. 3) D2. Half of Same(数学 枚举 思维)
Codeforces Round #748 (Div. 3) D2. Half of Same(数学 枚举 思维)
113 0
|
人工智能 JavaScript BI
AtCoder Beginner Contest 222 D - Between Two Arrays(前缀和优化dp)
AtCoder Beginner Contest 222 D - Between Two Arrays(前缀和优化dp)
120 0
|
网络架构
Codeforces Round #755 D. Guess the Permutation(交互 二分)
Codeforces Round #755 D. Guess the Permutation(交互 二分)
101 0
HDOJ 1391 Number Steps(打表DP)
HDOJ 1391 Number Steps(打表DP)
139 0
HDOJ 1391 Number Steps(打表DP)
|
机器学习/深度学习
CodeForces 629A-Far Relative’s Birthday Cake(枚举/暴力)
CodeForces 629A-Far Relative’s Birthday Cake(枚举/暴力)
CodeForces 6A-Triangle(枚举/暴力)
CodeForces 6A-Triangle(枚举/暴力)

热门文章

最新文章