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)

代码:

 //#include"bits/stdc++.h"
#include <sstream>
#include <iomanip>
#include"cstdio"
#include"map"
#include"set"
#include"cmath"
#include"queue"
#include"vector"
#include"string"
#include"cstring"
#include"time.h"
#include"iostream"
#include"stdlib.h"
#include"algorithm"
#define db double
#define ll long long
#define vec vector<ll>
#define mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
//#define rep(i, x, y) for(int i=x;i<=y;i++)
#define rep(i,n) for(int i=0;i<n;i++)
const int N = 2e3 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const int inf = 0x3f3f3f3f;
const db PI = acos(-1.0);
const db eps = 1e-;
using namespace std;
int a[N];
int l[N],r[N];
int f[N][N][];
int main()
{
int n;
ci(n);
for(int i=;i<=n;i++) ci(a[i]),l[i]=l[i-]+(a[i]==);
for(int i=n;i>=;i--) r[i]=r[i+]+(a[i]==);
int ma=-;
for(int i=;i<=n;i++){
for(int j=i;j<=n;j++){
f[i][j][]=f[i][j-][]+(a[j]==);
f[i][j][]=max(f[i][j-][],f[i][j-][])+(a[j]==);
ma=max(ma,f[i][j][]+l[i-]+r[j+]);
ma=max(ma,f[i][j][]+l[i-]+r[j+]);
}
}
pi(ma);
return ;
}

最新文章

  1. vmware备忘
  2. javascript 核心语言笔记 4 - 表达式和运算符
  3. 个人博客作业week5
  4. Java中的TreeMap、Comparable、Comparator
  5. static_cast 和 dynamic_cast 的区别
  6. golang 常用网址收藏
  7. Python异常记录
  8. AJAX异步同步
  9. TCL语言笔记:TCL中的列表操作
  10. AngularJS 拦截器和好棒例子
  11. Delphi动态创建组件,并释放内存
  12. html5 canvas 运行起来绝对让你震撼!
  13. [置顶] 图书推荐:SQL Server 2012 T-SQL基础 Itzik Ben-Gan
  14. Python统计栏目页面数量
  15. 浅谈Android中Serializable和Parcelable使用区别
  16. typedef介绍
  17. &lt;HTML&gt; 模块
  18. Python-类的特性(property)
  19. 使用JAVA实现的一个简单IOC注入实例
  20. mysql c-api 预处理语句

热门文章

  1. intellijidea课程 intellijidea神器使用技巧1-5 idea界面介绍
  2. 1064. 计算斐波那契第n项 通项公式
  3. 手把手教你用android studio创建第一个安卓程序加载html5页面(二)
  4. Java -GUI开发九九乘法表
  5. webpack gulp grunt 简单介绍
  6. .net core 2.0 WIndows IIS下发布(WIN 10环境)
  7. 将命令的输出生成一个Web页面
  8. 2017.11.23 利用Cookie管理实现自动登陆
  9. 安卓装tensorflow
  10. EF Database first 中,实现 多个表对应一个 实体的 查询