码迷,mamicode.com
首页 > 其他好文 > 详细

E - A Twisty Movement

时间:2018-04-07 15:02:15      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:ack   span   tle   post   ike   test   second   new   one   

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
4
1 2 1 2
Output
4
Input
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 1 2

就交换中间的2 1 就行

所以找前面的1 2和后面的1 2的 最长不降就行

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c)
{
    return min(min(a, b), c);
}
template <class T> inline T max(T a, T b, T c)
{
    return max(max(a, b), c);
}
template <class T> inline T min(T a, T b, T c, T d)
{
    return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d)
{
    return max(max(a, b), max(c, d));
}
#define scanf1(x) scanf("%d", &x)
#define scanf2(x, y) scanf("%d%d", &x, &y)
#define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define bug printf("***********\n");
#define mp make_pair
#define pb push_back
const int N = 10005;
// name*******************************
int f1[N];
int f2[N];
int pre[N];
int nxt[N];
int n;
int a[N];
int ans=0;
// function******************************


//***************************************
int main()
{
//    ios::sync_with_stdio(0);
//    cin.tie(0);
    // freopen("test.txt", "r", stdin);
    //  freopen("outout.txt","w",stdout);
    cin>>n;
    For(i,1,n)
    cin>>a[i];

    For(i,1,n)
    {
        f1[i]=1;
        For(j,1,i-1)
        if(a[i]>=a[j])
            f1[i]=max(f1[i],f1[j]+1);
        pre[i]=max(pre[i-1],f1[i]);
    }

    FFor(i,n,1)
    {
        f2[i]=1;
        For(j,i+1,n)
        if(a[j]>=a[i])
            f2[i]=max(f2[i],f2[j]+1);
        nxt[i]=max(nxt[i+1],f2[i]);
    }

    For(i,1,n)
    {
        ans=max(ans,nxt[i+1]+pre[i]);
    }
    cout<<ans;

    return 0;
}

 

E - A Twisty Movement

标签:ack   span   tle   post   ike   test   second   new   one   

原文地址:https://www.cnblogs.com/planche/p/8733163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!