Codeforces Round #462 (Div. 2) C. A Twisty Movement
C. A Twisty Movement
time limit per test1 second
memory limit per test256 megabytes
Problem Description
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.
解题心得:
- 题意很简单就是输出最长不递减子序列的长度。
- 读错题了啊,把子序列看成子区间弄错了,。
- 就是一个dp加一点思维
- 先求出1的前缀和,2的后缀和
- dp[i][j][1]代表在区间(i,j)之间以1结尾的最长不递增子序列的长度。
dp[i][j][2]代表在区间(i,j)之间以2结尾的最长不递增子系列的长度。 - 状态转移方程式就很容易出来了dp[i][j][1] = max(dp[i][j-1][1],dp[i][j-1][2]) + (num[j] == 1)
dp[i][j][2] = dp[i][j-1][2] + (num[j] == 2)
- 至于为什么要求不递增的dp,那就是要翻转啊,翻转之后不递增不就变成不递减了吗。而前缀和和后缀和就是考考思维。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2010;
int dp[maxn][maxn][3],num[maxn],sum1[maxn],sum2[maxn];
int Max = -1,n;
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
for(int i=0;i<n;i++)
sum1[i] = sum1[i-1] + (num[i] == 1);
for(int i=n-1;i>=0;i--)
sum2[i] = sum2[i+1] + (num[i] == 2);
for(int i=0;i<n;i++)
for(int j=i;j<n;j++){
dp[i][j][2] = dp[i][j-1][2] + (num[j] == 2);
dp[i][j][1] = max(dp[i][j-1][1],dp[i][j-1][2]) + (num[j] == 1);
Max = max(dp[i][j][1] + sum1[i-1] + sum2[j+1],Max);
Max = max(dp[i][j][2] + sum1[i-1] + sum2[j+1],Max);
}
printf("%d",Max);
return 0;
}
Codeforces Round #462 (Div. 2) C. A Twisty Movement的更多相关文章
- 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 ...
- Codeforces Round #462 (Div. 2), problem: (C) A Twisty Movement (求可以转一次区间的不递增子序列元素只有1,2)
题目意思: 给长度为n(n<=2000)的数字串,数字只能为1或者2,可以将其中一段区间[l,r]翻转,求翻转后的最长非递减子序列长度. 题解:求出1的前缀和,2的后缀和,以及区间[i,j]的最 ...
- 【Codeforces Round #462 (Div. 1) A】 A Twisty Movement
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] ans初值值为a[1..n]中1的个数. 接下来考虑以2为结尾的最长上升子序列的个数. 枚举中间点i. 计算1..i-1中1的个数c ...
- Codeforces Round #462 (Div. 2) B-A Prosperous Lot
B. A Prosperous Lot time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #462 (Div. 2)
这是我打的第三场cf,个人的表现还是有点不成熟.暴露出了我的一些问题. 先打开A题,大概3min看懂题意+一小会儿的思考后开始码代码.一开始想着贪心地只取两个端点的值就好了,正准备交的时候回想起上次A ...
- Codeforces Round #462 (Div. 2) D. A Determined Cleanup
D. A Determined Cleanup time limit per test1 second memory limit per test256 megabytes Problem Descr ...
- Codeforces Round #462 (Div. 2) A Compatible Pair
A. A Compatible Pair time limit per test1 second memory limit per test256 megabytes Problem Descript ...
- 【Codeforces Round #462 (Div. 1) B】A Determined Cleanup
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设\(设f(x)=a_d*x^{d}+a_{d-1}*x^{d-1}+...+a_1*x+a_0\) 用它去除x+k 用多项式除法除 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- 最简实例演示asp.net5中用户认证和授权(1)
asp.net5中,关于用户的认证和授权提供了非常丰富的功能,如果结合ef7的话,可以自动生成相关的数据库表,调用也很方便. 但是,要理解这么一大堆关于认证授权的类,或者想按照自己项目的特定要求对认证 ...
- 2019年我的nodejs项目选型
选型项目比较激进.发现基于 go 语言的工具变多了.
- C 碎片十 关键字&库函数
一.关键字 1, sizeof sizeof关键字用于计算所占空间大小的 格式:sizeof(类型名/变量名); 2, typedef typedef关键字用于重命名数据类型的,相当于给原来的数据类型 ...
- AJPFX总结java创建线程的三种方式及其对比
Java中创建线程主要有三种方式: 一.继承Thread类创建线程类 (1)定义Thread类的子类,并重写该类的run方法,该run方法的方法体就代表了线程要完成的任务.因此把run()方法称为执行 ...
- springboot 学习笔记(五)
(五)springboot整合thymeleaf模板,实现简单的登陆 1.修改上一节笔记中的user表,新增一个password字段,同时要求username为UNIQUE,以实现登陆校验,表结构如下 ...
- vue中background-image图片路径问题
按照以往在css文件中写background:url('图片路径'),完成后加载竟然显示出错,起初以为路径不对,检查了几遍,仍然没有问题.最后百度找答案,发现不少同行都遇到过这种问题,遂记录下自己所采 ...
- java基础概念整理综合 及补充(jdk1.8)
2018 java基础 笔记回顾摘要 一 1,html 与 注释: <!-- --> 注释不能嵌套 代码都得有注释. 2,空格符: 3,css选择的优先级: id选择器 > ...
- MvvmCross框架在XamarinForms中的使用入门
做XamarinForms快一年了,最近趁着项目不是很紧,有点空闲的时间,研究了一下MvvmCross这个框架,感觉挺高大上的.一边研究一下写点入门的东西吧,大部分的东西github都有. 1添加Pa ...
- 分布式缓存memcached介绍,win7环境安装,常用命令set,get,delete,stats, java访问
一.memcached是什么? 二.memcached不互相通信的分布式 三.安装步骤 四.本文介绍的命令主要包括: 存入命令(Storage commands) 取回命令(Retrieval com ...
- Java栈,PC寄存器,本地方法栈,堆,方法区(静态区)和运行常量池
详情参考:https://my.oschina.net/wangsifangyuan/blog/711329 前言:当要判断一个变量存在什么空间上哪儿时,先分析它是哪一种(是实例变量还是局部变量),实 ...