Codeforces 447C dzy Loves sequences DP

Source: Internet
Author: User

Title: Click here

Test instructions: The length of the longest continuous ascending subsequence after a given sequence changes one of the elements

Analysis: The longest continuous subsequence has 2 kinds, one is the length of the strict ascent (without changing the element) plus 1, one is the combination of two periods of strict rise. 、

1#include <bits/stdc++.h>2 using namespacestd;3 #defineF First4 #defineS Second5 #definePB Push_back6 #definePower (a) ((a) * (a))7 #defineENTER printf ("\ n");8typedef unsignedLong Longll;9 Const intINF =0x3f3f3f3f;Ten Const DoubleEPS = 1e-8; One Const intM = 1e5+5; A  - intN; - intA[m]; the intDPS[M];//Dps[i] Save longest continuous ascending subsequence starting with I - intDPE[M];//Dpe[i] Save longest continuous ascending subsequence ending with I - voidsolve () { -dpe[0] =1; +dps[n-1] =1; -     intRET =1; +      for(intI=1; i<n; i++ ) { A         if(A[i] > a[i-1]) Dpe[i] = dpe[i-1] +1; at         ElseDpe[i] =1; -RET =Max (ret, dps[i]); -     } -      for(inti=n-2; i>=0; i-- ) { -         if(A[i] < a[i+1]) Dps[i] = dps[i+1] +1; -         ElseDps[i] =1; inRET =Max (ret, dps[i]); -     } to      for(intI=0; i<n; i++ )  { +         if(i = =0 ) -RET = max (ret, dps[i]+1 ); the         Else if(i = = n1 ) *RET = max (ret, dpe[i]+1 ); $         Else    {Panax Notoginseng             if(a[i+1]-a[i-1] >=2 )     -RET = max (ret, dps[i+1]+dpe[i-1]+1 ); the             Else +RET = max (ret, Max (dps[i+1]+1, dpe[i-1]+1 ) ); A         } the     } +printf"%d\n", Ret>n?n:ret);//The maximum result is n - } $ intMain () { $      while(~SCANF ("%d", &N)) { -          for(intI=0; i<n; i++ )     -scanf"%d", A +i); the solve (); -     }Wuyi     return 0; the}

Codeforces 447C dzy Loves sequences DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.