CF1272D. Remove One Element dynamic programming solution to a problem

Topic links: http://codeforces.com/contest/1272/problem/D

Title effect:
to give you a length \ (n-\) array, you delete at most one element (or may not be deleted), find the maximum increase in the sub-string length limit conditions.

Problem-solving ideas:
This question relates to algorithms: dynamic programming.

First, there is a condition. "You can remove a maximum of one element", this condition can cause us a lot of trouble, so in order to avoid trouble, we first try to solve the problem in the absence of the conditions are.

In our without permission to remove elements, the array \ (A \) of \ (n-\) element is fixed, so in this case we can define the state of \ (f [i] \) represented by \ (a [i ] \) at the end and comprising \ (a [i] \) to increase the length of the longest substring, then we can find (array coordinates provided from \ (1 \) starts):

\ (F [. 1] =. 1 \) ;
if (i \ gt 1 \) \ time,

  • 如果\ (A [I-1] <A [I] \) , Provisions \ (f [i] = f [i-1] +1 \)
  • Otherwise, \ (F [I] =. 1 \)

Code:

f[1] = 1;
for (int i = 2; i <= n; i ++) {
    if (a[i-1] < a[i]) f[i] = f[i-1]+1;
    else f[i] = 1;
}

Then we need to increase the required length of the substring is the longest of all \ (f [i] \) in the largest one.
Wait a minute, we do or do not add "you can delete a maximum of one element" of this condition.
Before adding this condition, then we define a state \ (g [i] \) represented by (a [i] \) \ beginning and comprising \ (a [i] \) length increase of the longest substring, so, we can get the state transition equation:

\ (G [n-] =. 1 \) ;
if (i <n \) \ time,

  • If \ (A [I] <A [I +. 1] \) , then \ (G [I] G = [I +. 1] + 1'd \) ;
  • Otherwise, \ (G [I] =. 1 \)

Code implementation (note \ (g [i] \) required from \ (n-\) to \ (1 \) trans push forward):

g[n] = 1;
for (int i = n-1; i >= 1; i --) {
    if (a[i] < a[i+1]) g[i] = g[i+1]+1;
    else g[i] = 1;
}

Then we finished beg \ (f [i] \) and \ (g [i] \) after it, let's add back "You can remove a maximum of one element" of this condition.

First of all, if we do not remove elements, the answer is all \ (f [i] \) is the maximum, we open a variable \ (ANS = \ max (f [i]) \) .

Secondly, if we remove the element coordinates \ (I \) , we assume that the increase in the longest sub-sequence corresponding to the element is deleted \ (a [l] \) to \ (A [R & lt] \) ,
then if \ ( i \) is not satisfied \ (l <i <r \ ) conditions, then I delete or not delete \ (a [i] \) does not affect me the answer (still \ (ans = \ max (f [i ]) \) ).

So when the answer will affect it?
That when \ (1 <i <n \ ) and \ (a [i-1] <a [i + 1] \) , I delete \ (a [i] \) enables \ (f [i- 1] + g [i + 1 ]> ans \) when instructions to remove the element \ (a [i] \) to the effect of increased growth of the longest substring, then we update \ (ans = f [i- . 1] + G [I +. 1] \) .

In summary, the answer should be

  • \ (\ max (F [I]) \) (where \ ((. 1 \ I Le \ n-Le) \) )
    and
  • \(\max(f[i-1]+g[i+1])\) (其中 \(1 \lt i \lt n\)\(a[i-1] < a[i+1]\)

The larger value.

Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200020;
int n, a[maxn], f[maxn], g[maxn], ans;
int main() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    f[1] = 1;
    for (int i = 2; i <= n; i ++) {
        if (a[i-1] < a[i]) f[i] = f[i-1]+1;
        else f[i] = 1;
    }
    g[n] = 1;
    for (int i = n-1; i >= 1; i --) {
        if (a[i] < a[i+1]) g[i] = g[i+1]+1;
        else g[i] = 1;
    }
    for (int i = 1; i <= n; i ++) ans = max(ans, f[i]);
    for (int i = 2; i < n; i ++) if (a[i-1] < a[i+1]) ans = max(ans, f[i-1] + g[i+1]);
    cout << ans << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/quanjun/p/12041388.html