• Codeforces Round #462 (Div. 2) C DP


    C. A Twisty Movement
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.

    A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.

    Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.

    A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.

    Input

    The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.

    The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).

    Output

    Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.

    Examples
    input
    Copy
    4
    1 2 1 2
    output
    4
    input
    Copy
    10
    1 1 2 2 2 1 1 2 2 1
    output
    9
    Note

    In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.

    In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.

    题意:求最长不递减子序列可以选择一个区间逆转。

    题解:求出1的前缀和,2的后缀和,以及区间[i,j]的最长不递增子序列。

    f[i][j][0]表示区间i-j以1结尾的最长不递增子序列;

    f[i][j][1]表示区间i-j以2结尾的最长不递增子序列,显然是区间i-j 2的个数;

    所以转移方程为:

       f[i][j][1] = f[i][j-1][1] + (a[j]==2);
       f[i][j][0] = max(f[i][j-1][0], f[i][j-1][1]) + (a[j]==1);(1<=i<=n,i<=j<=n)

    代码:

     1 //#include"bits/stdc++.h"
     2 #include <sstream>
     3 #include <iomanip>
     4 #include"cstdio"
     5 #include"map"
     6 #include"set"
     7 #include"cmath"
     8 #include"queue"
     9 #include"vector"
    10 #include"string"
    11 #include"cstring"
    12 #include"time.h"
    13 #include"iostream"
    14 #include"stdlib.h"
    15 #include"algorithm"
    16 #define db double
    17 #define ll long long
    18 #define vec vector<ll>
    19 #define mt  vector<vec>
    20 #define ci(x) scanf("%d",&x)
    21 #define cd(x) scanf("%lf",&x)
    22 #define cl(x) scanf("%lld",&x)
    23 #define pi(x) printf("%d
    ",x)
    24 #define pd(x) printf("%f
    ",x)
    25 #define pl(x) printf("%lld
    ",x)
    26 //#define rep(i, x, y) for(int i=x;i<=y;i++)
    27 #define rep(i,n) for(int i=0;i<n;i++)
    28 const int N   = 2e3 + 5;
    29 const int mod = 1e9 + 7;
    30 const int MOD = mod - 1;
    31 const int inf = 0x3f3f3f3f;
    32 const db  PI  = acos(-1.0);
    33 const db  eps = 1e-10;
    34 using namespace std;
    35 int a[N];
    36 int l[N],r[N];
    37 int f[N][N][2];
    38 int main()
    39 {
    40     int n;
    41     ci(n);
    42     for(int i=1;i<=n;i++) ci(a[i]),l[i]=l[i-1]+(a[i]==1);
    43     for(int i=n;i>=0;i--) r[i]=r[i+1]+(a[i]==2);
    44     int ma=-1;
    45     for(int i=1;i<=n;i++){
    46         for(int j=i;j<=n;j++){
    47             f[i][j][1]=f[i][j-1][1]+(a[j]==2);
    48             f[i][j][0]=max(f[i][j-1][0],f[i][j-1][1])+(a[j]==1);
    49             ma=max(ma,f[i][j][1]+l[i-1]+r[j+1]);
    50             ma=max(ma,f[i][j][0]+l[i-1]+r[j+1]);
    51         }
    52     }
    53     pi(ma);
    54     return 0;
    55 }
  • 相关阅读:
    Highcharts之饼图
    设计模式学习之原型模式
    jQuery学习之结构解析
    JS学习之闭包的理解
    JS学习之prototype属性
    JS学习之事件冒泡
    Mybatis学习之JDBC缺陷
    Spring学习之Aop的基本概念
    Struts学习之值栈的理解
    Struts学习之自定义结果集
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/8650739.html
Copyright © 2020-2023  润新知