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 ...
随机推荐
- SpringBoot | 第八章:统一异常、数据校验处理
前言 在web应用中,请求处理时,出现异常是非常常见的.所以当应用出现各类异常时,进行异常的捕获或者二次处理(比如sql异常正常是不能外抛)是非常必要的,比如在开发对外api服务时,约定了响应的参数格 ...
- echarts折线图相关
optionJKDLine = { title: { text: '告警数量趋势图', textStyle:{ //标题样式 fontStyle:'normal', fontFamily:'sans- ...
- eCharts图表(polar极坐标图)
极坐标图 HTML: <div id="eChart"></div> css: #eChart{ width:500px; height:500px; } ...
- OpenCV之cvAddWeighted直接C语言实现版addWeighted,应对上下平滑融合拼接
关于OpenCV中的cvAddWeighted的介绍可参见<opencv中的cvAddWeighted函数> cvAddWeighted有个问题,它只能实现两张图片的直接融合,往往产生明显 ...
- VMware-Ubuntu16.04LTS-安装ssh
1,检查是否安装ssh 2,安装ssh
- UVA 11990 ``Dynamic'' Inversion (线段树套BIT,分治)
题目要求可转化为查询一个区间内有多少数比val大(或者小). 区间用线段树分解(logN),每个区间维护一rank树. rank可用BIT查询,往BIT里面插值,为了保证不同区间的BIT互不影响要先离 ...
- 【HHHOJ】NOIP模拟赛 玖 解题报告
点此进入比赛 得分: \(100+20+100=220\)(还不错) 排名: \(Rank\ 16\) \(Rating\):\(+20\) \(T1\):[HHHOJ263]「NOIP模拟赛 玖」三 ...
- 【洛谷3796】【模板】AC自动机(加强版)
点此看题面 大致题意: 一道模板题,给你\(N\)个模式串和一个文本串,要你求出在文本串中出现次数最多的若干个模式串并输出它们. \(AC\)自动机 都说了是\(AC\)自动机的模板题,做法肯定是\( ...
- python_14_sys_mod
import sys #1 print(sys.path)#打印环境变量 #2 print(sys.argv)#打印相对路径 print(sys.argv[2])#在cmd命令窗口运行本文件
- 软件杯python-flask遇到的坑有感!
大三下,对于我考研的人来说,时间不要太紧张,参加软件杯也是系主任要求,题目是公共地点人流量的检测,个人还是个菜鸟,但是把遇到的一些大家可能不小心会出现的问题贴出来,困扰我很久,还没睡好觉!!! Que ...