CodeForces 447C DZY Loves Sequences (dp 子序列)

来源:互联网 发布:怎么检查网络是否连接 编辑:程序博客网 时间:2024/05/29 18:24
因为change at most one number (change one number to any integer you want)

所以找到左边递增的最大区间长度

然后找到右边递增的最大区间长度

如果最大长度就是n,那么说明原来就是升序排好了的。


注意以下数据:

5

1 2 2 3 4

输出: 4


有个细节见下方代码

#include<stdio.h>#include<iostream>#include<math.h>#include<string.h>#include<iomanip>#include<stdlib.h>#include<ctype.h>#include<algorithm>#include<deque>#include<functional>#include<iterator>#include<vector>#include<list>#include<map>#include<queue>#include<set>#include<stack>#define CPY(A, B) memcpy(A, B, sizeof(A))typedef long long LL;typedef unsigned long long uLL;const int MOD = 1e9 + 7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const double EPS = 1e-9;const double OO = 1e20;const double PI = acos (-1.0);const int dx[] = {-1, 0, 1, 0};const int dy[] = {0, 1, 0, -1};using namespace std;const int maxn=1e5+10;struct AA {    int left/*left length*/,right,element;};AA dp[maxn];int main() {    int n; scanf ("%d",&n);    memset (dp,0,sizeof (dp) );    for (int i=1; i<=n; i++) {        scanf ("%d",&dp[i].element);    }    if (n==1) {printf ("1\n");}/**/    else {        int ans=0;        dp[1].left=1;        for (int i=2; i<=n; i++) {            dp[i].left= (dp[i].element>dp[i-1].element?dp[i-1].left+1/*length add 1*/:1);            ans=max (ans,dp[i].left);//大于左边的,那么就是左边长度再加1        }        dp[n].right=1;        for (int i=n-1; i>=1; i--) {            dp[i].right= (dp[i+1].element>dp[i].element?dp[i+1].right+1:1);            ans=max (ans,dp[i].right);        }        if (ans==n) { printf ("%d",n); }//等于n意味着之前顺序就是排好的        else {            for (int i=1; i<=n; i++) {                if (dp[i+1].element>dp[i-1].element+1) {//must be dp[i+1]>dp[i-1]+1,because: 1 ? 2 and 1 ? 1                    ans=max (ans,dp[i-1].left+dp[i+1].right+1);                } else {                    ans=max (ans,max (dp[i].left+1,dp[i].right+1) );                }            }            printf ("%d\n",ans);        }    }    return 0;}

0 0