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
  1. 4
    1 2 1 2
Output
  1. 4
Input
  1. 10
    1 1 2 2 2 1 1 2 2 1
Output
  1. 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的 最长不降就行

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define inf 2147483647
  5. const ll INF = 0x3f3f3f3f3f3f3f3fll;
  6. #define ri register int
  7. template <class T> inline T min(T a, T b, T c)
  8. {
  9. return min(min(a, b), c);
  10. }
  11. template <class T> inline T max(T a, T b, T c)
  12. {
  13. return max(max(a, b), c);
  14. }
  15. template <class T> inline T min(T a, T b, T c, T d)
  16. {
  17. return min(min(a, b), min(c, d));
  18. }
  19. template <class T> inline T max(T a, T b, T c, T d)
  20. {
  21. return max(max(a, b), max(c, d));
  22. }
  23. #define scanf1(x) scanf("%d", &x)
  24. #define scanf2(x, y) scanf("%d%d", &x, &y)
  25. #define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
  26. #define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
  27. #define pi acos(-1)
  28. #define me(x, y) memset(x, y, sizeof(x));
  29. #define For(i, a, b) for (int i = a; i <= b; i++)
  30. #define FFor(i, a, b) for (int i = a; i >= b; i--)
  31. #define bug printf("***********\n");
  32. #define mp make_pair
  33. #define pb push_back
  34. const int N = ;
  35. // name*******************************
  36. int f1[N];
  37. int f2[N];
  38. int pre[N];
  39. int nxt[N];
  40. int n;
  41. int a[N];
  42. int ans=;
  43. // function******************************
  44.  
  45. //***************************************
  46. int main()
  47. {
  48. // ios::sync_with_stdio(0);
  49. // cin.tie(0);
  50. // freopen("test.txt", "r", stdin);
  51. // freopen("outout.txt","w",stdout);
  52. cin>>n;
  53. For(i,,n)
  54. cin>>a[i];
  55.  
  56. For(i,,n)
  57. {
  58. f1[i]=;
  59. For(j,,i-)
  60. if(a[i]>=a[j])
  61. f1[i]=max(f1[i],f1[j]+);
  62. pre[i]=max(pre[i-],f1[i]);
  63. }
  64.  
  65. FFor(i,n,)
  66. {
  67. f2[i]=;
  68. For(j,i+,n)
  69. if(a[j]>=a[i])
  70. f2[i]=max(f2[i],f2[j]+);
  71. nxt[i]=max(nxt[i+],f2[i]);
  72. }
  73.  
  74. For(i,,n)
  75. {
  76. ans=max(ans,nxt[i+]+pre[i]);
  77. }
  78. cout<<ans;
  79.  
  80. return ;
  81. }

E - A Twisty Movement的更多相关文章

  1. Codeforces 934C - A Twisty Movement

    934C - A Twisty Movement 思路:dp 很容易想到要预处理出1的前缀和pre[i]和2的后缀和suf[i] 然后枚举区间,对于每个区间如果能求出最长递减序列的长度,那么就能更新答 ...

  2. Codeforces 934.C A Twisty Movement

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

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

    C. A Twisty Movement time limit per test1 second memory limit per test256 megabytes Problem Descript ...

  4. [Codeforces 933A]A Twisty Movement

    Description 题库链接 给你一个长度为 \(n\) 的只含有 \(1,2\) 的序列.你可以选择其中的一段 \([l,r]\) ,将区间翻转,翻转后使得单调不下降序列最长.求最长长度. \( ...

  5. cf934C. A Twisty Movement(思维题)

    题意 题目链接 Sol 这题最直接的维护区间以0/1结尾的LIS的方法就不说了. 其实我们可以直接考虑翻转以某个位置为中点的区间的最大值 不难发现前缀和后缀产生的贡献都是独立的,可以直接算.维护一下前 ...

  6. CF933A A Twisty Movement

    题意翻译 给定一个序列 A,你可以翻转其中的一个区间内的数,求翻转后的序列的最长不下降子序列的长度.(∣A∣≤2000,1≤ai≤2|A|\le 2000,1\le a_i \le 2∣A∣≤2000 ...

  7. Codeforces Round #462 (Div. 2), problem: (C) A Twisty Movement (求可以转一次区间的不递增子序列元素只有1,2)

    题目意思: 给长度为n(n<=2000)的数字串,数字只能为1或者2,可以将其中一段区间[l,r]翻转,求翻转后的最长非递减子序列长度. 题解:求出1的前缀和,2的后缀和,以及区间[i,j]的最 ...

  8. CF933A/934C A Twisty Movement

    思路: 实际上是求原序列中最长的形如1......2......1......2......的子序列的长度.令dp[i][j](1 <= j <= 4)表示在子序列a[1]至a[i]中形如 ...

  9. 【Codeforces 933A】A Twisty Movement

    [链接] 我是链接,点我呀:) [题意] [题解] 因为只有1和2. 所以最后肯定是若干个1接着若干个2的情况. 即11...11222...222这样的. 1.首先考虑没有翻转的情况. 那么就直接枚 ...

随机推荐

  1. 大数据java基础day01

    day01笔记 1.==操作符和equals方法 equals方法存在于Object类中,所有类的equals方法都继承于Object 2.String类的常用方法 ①.replace()替换字符串 ...

  2. js-权威指南学习笔记19

    第十九章 jQuery类库 1.传递HTML文本字符串给$()方法,jQuery会根据传入的文本创建好HTML元素并封装为jQuery对象返回. 2.想要遍历jQuery对象中的所有元素时,可以调用e ...

  3. Linux 系统下用源码包安装软件

    Linux系统下用源码包安装软件 by:授客 QQ:1033553122 下载源码安装包,解压或者直接双击打开(如果有安装zip或rar等压缩/解压缩软件的话),查找相关的安装说明文件,一般是READ ...

  4. Visual Studio编译C工程出现的错误

    错误1. エラー 1 error LNK1561: エントリー ポイントを定義しなければなりません. 解决办法:将工程的类型改为dll动态库,设置方式如下: 右键工程,选择[プロパティ].在弹出的面板 ...

  5. JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架)

    JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架) 本来想实现 QQ 登录,有域名一直没用过,还得备案,好麻烦,只能过几天再更新啦. 先把实现的发送邮箱验证码更能更新了. 老规矩,更多内容在注释 ...

  6. [iOS]圆形进度条及计时功能

    平时用战网安全令的时候很喜欢圆形倒计时的效果,然后简单看了一下Android的圆形进度条,后来又写了一个IOS的.整体界面参照IOS系统的倒计时功能,顺便熟悉了UIPickerView的一些特性的实现 ...

  7. EVE Online Third Party Development

    第一部分:price_history表 # 建表语句 CREATE TABLE IF NOT EXISTS `price_history` ( `regionID` INT NOT NULL, `ty ...

  8. python之绘制图形库turtle

    关于绘制图形库turtle# 画布上,默认有一个坐标原点为画布中心的坐标轴(0,0),默认"standard"模式坐标原点上有一只面朝x轴正方向小乌龟 一:海龟箭头Turtle相关 ...

  9. systemd 之 journalctl

    Systemd 日志系统 一.前言 昨天写了一篇文章,内容为:Systemd 常规操作与彩蛋,参考了 ArchLinux 官方文档并结合培训中的思路进行了部分修改补充.如果你懂得了基础的管理,那必然还 ...

  10. ELF文件结构描述

    ELF目标文件格式最前部ELF文件头(ELF Header),它包含了描述了整个文件的基本属性,比如ELF文件版本.目标机器型号.程序入口地址等.其中ELF文件与段有关的重要结构就是段表(Sectio ...