Codeforces 446A dzy Loves sequences topic link: http://codeforces.com/problemset/problem/446/A
The main topic: give a fixed length of N of the series A, asked to change one number. What is the length of the longest strictly ascending sub-segment that may occur.
Topic Analysis: First of all, do not consider the "change of one number" this condition, so the problem is much simpler, before and after the traversal count can be (define an array inc[] length with a, initialize all points to 1, Traverse assumes the current point a[i]>a[i-1] set inc[i]=inc[i-1]+ 1). So how does the problem turn out to be the condition of changing a number? The original sequence may have some discrete strict ascending sub-segments, some of which can be continued into a paragraph (when the middle of the two strict ascending sub-paragraph is only separated by a number && the last segment of the paragraph is less than the next section of Chito-1, we can be separated in the middle of the number of changes to meet the requirements), perhaps no such , the answer is the longest length of +1 (assuming the sequence itself is strictly ascending, the answer is N).
In short is to find the longest, other situations to say, two consecutive how to engage? Use the INC array you just mentioned to make a note of all the results from the front and back, so that the maximum length of the sub-segment of the i-1 point can be obtained directly during traversal. Then the first i+1 point will have to counter its way, and then define a DEC array, from the back forward to traverse the count, the two arrays together to obtain the maximum value of the continuation (may not be clear enough.) Detailed code).
Code
#include < stdio.h> #include <string.h>int max (int a,int b,int c) {if (c>=a&&c>=b) return C;return a>b?a:b ;} int main () {int i,j,n,a[100020],inc[100020],dec[100020],ans=0;scanf ("%d", &n); if (n==1| | n==2) {printf ("%d\n", N); return 0;} for (i=0;i<n;i++) {scanf ("%d", a+i);} Inc[0]=dec[n-1]=1;for (i=1;i<n;i++) {inc[i]=a[i]>a[i-1]?inc[i-1]+1:1;dec[n-i-1]=a[n-i-1]<a[n-i]?dec[n-i ]+1:1;} for (i=1;i<n-1;i++) {int temp=-1;if (a[i-1]<a[i+1]-1&& (inc[i]==1| | Dec[i]==1) Temp=inc[i-1]+dec[i+1]+1;else if (A[i-1]<a[i]) Temp=inc[i-1]+2;else if (a[i]<a[i+1]) temp=dec[i+1]+ 2;else Temp=max (inc[i-1]+1,dec[i+1]+1,0);//if (Temp>ans&&temp>inc[i]) printf ("temp==%d\n", temp); ans =max (Temp,ans,inc[i]);} Ans=max (ans,inc[n-2]+1,dec[1]+1);//Because the loop above is running from the second to the penultimate, so the last thing to do is to close the tail printf ("%d\n", ans); return 0;} /*56763455676789567834567 2 3 1 4 5inc1 1 2 1 2 3dec1 2 1 3 2 1*/
PS: The title only requires a number of changes. What if I change the number of M? I think it's time to write down the optimal change point, and after the traversal, I really have to change the value of the array A, Inc, Dec, and then go through. In this way, the time complexity will be increased to the original m times, perhaps the need for more optimized array storage methods ...
PSS: This method is not DP, just a label DP on the topic. Expect DP practice ...
Codeforces 446A Dzy Loves sequences