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 ...
随机推荐
- php面试题分享
1.nginx使用哪种网络协议? nginx是应用层 我觉得从下往上的话 传输层用的是tcp/ip 应用层用的是http fastcgi负责调度进程 2. <? echo 'hello tush ...
- Linux - 数值运算
Shell - 数值运算 因为shell脚本是属于弱语言,没有变量类型的概念,所以定义变量会默认为字符串.就算看上去是一个数字,当直接进行计算时,就会出错: x=1 echo $x+=1 # 输出1+ ...
- jar 压缩 解压 war包
Win+R 输入cmd进入命令行,进入到源码所在目录.所用工具,jdk自带的jar.exe 打包命令:jar -cvf xxx.war * 解包命令: jar -xvf xxx.war * 参数 说明 ...
- 自定义列表dl
语法格式 <dl> <dt>名词1</dt> <dd>名词1解释1</dd> <dd>名词1解释2</dd> ... ...
- #include< >和#include“ ”的区别
< >引用的是编译器的类库路径里面的头文件 " "引用的是你程序目录的相对路径中的头文件 假如你编译器定义的自带头文件引用在C:\Keil\c51\INC\下面 则#i ...
- hiho一下 第三十八周 二分答案
题目链接:http://hihocoder.com/contest/hiho38/problem/1 ,挺难想的解题思路,好题. 按照提示的算法来: 我们需要找什么? 在这个题目中我们需要找的是路径最 ...
- pta 编程题7 List Leaves
其它pta数据结构编程题请参见:pta 这次的编程作业要求从上到下,从左到右输出一棵树的叶子节点,即树的层序遍历,用队列的方式来实现. 注意enqueue和dequeue函数参数为Queue & ...
- 用户在设置密码时,提醒请输入半角字符(vue+element+valid)
要保证callback()只有一个出口 rules:{ newPassword: [{validator:(rule,newPassword,callback)=>{ var all = fal ...
- World Wind Java开发之十三——加载Geoserver发布的WMS服务(转)
这篇是转载的平常心博客,原地址见:http://www.v5cn.cn/?p=171 1.WMSTiledImageLayer类说明 一个WMSTiledImageLayer类对象只能对应一个WMS发 ...
- World Wind Java开发之十——AnalyticSurface栅格渲染(转)
http://blog.csdn.net/giser_whu/article/details/43017881 1.AnalyticSurfaceDemo ArcGIS下对栅格的各种分级渲染效果是非常 ...