Codeforces Round #462 (Div. 2)C. A Twisty Movement

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
Copy
4
input
Copy
10
1 1 2 2 2 1 1 2 2 1
output
Copy
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.

题意:可以对一区间进行一次翻转,然后求最长不下降子序列 。

题解:可以枚举一个翻转的边界pos,然后枚举从左边的l翻转到pos或从右边的r翻转到pos,事先可先处理一下1的前缀和个数和2的后缀和个数,分别滑动l和r然后计算当前区间的最长不下降子序列,显然我们需要找到最长不降子序列中第一个2出现的位置pos,那么逆序后[l,pos]会使后面2个数目增加,[pos,r]会使前面1个数目增加。

扫描二维码关注公众号,回复: 2321672 查看本文章
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<time.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
const double PI = acos(-1.0);
const double eps = 1e-6;
typedef long long ll;
using namespace std;
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}

int a[2005],sum1[2005],sum2[2005];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        a[i]=read();
        sum1[i]=sum1[i-1]+(a[i]==1);
    }

    for(int i=n;i>=1;i--)
        sum2[i]=sum2[i+1]+(a[i]==2);

    int ans=0;
    for(int pos=1;pos<=n+1;pos++)
    {
        int num1=0,num2=0;
        for(int i=1;i<=pos;i++) num1=max(num1,sum1[i-1]+sum2[i]-sum2[pos]);
        for(int i=pos;i<=n+1;i++) num2=max(num2,sum2[i]+sum1[i-1]-sum1[pos-1]);
        ans=max(ans,num1+num2);
    }

    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sudu6666/article/details/80067260