Codeforces Round #462 (Div. 2) C DP
1 second
256 megabytes
standard input
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.
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).
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
4
1 2 1 2
4
10
1 1 2 2 2 1 1 2 2 1
9
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 ;
}
Codeforces Round #462 (Div. 2) C DP的更多相关文章
- 严格递增类的dp Codeforces Round #371 (Div. 1) C dp
http://codeforces.com/contest/713 题目大意:给你一个长度为n的数组,每次有+1和-1操作,在该操作下把该数组变成严格递增所需要的最小修改值是多少 思路:遇到这类题型, ...
- 很好的一个dp题目 Codeforces Round #326 (Div. 2) D dp
http://codeforces.com/contest/588/problem/D 感觉吧,这道题让我做,我应该是不会做的... 题目大意:给出n,L,K.表示数组的长度为n,数组b的长度为L,定 ...
- Codeforces Round #548 (Div. 2) C dp or 排列组合
https://codeforces.com/contest/1139/problem/C 题意 一颗有n个点的树,需要挑选出k个点组成序列(可重复),按照序列的顺序遍历树,假如经过黑色的边,那么这个 ...
- Codeforces Round #536 (Div. 2) E dp + set
https://codeforces.com/contest/1106/problem/E 题意 一共有k个红包,每个红包在\([s_i,t_i]\)时间可以领取,假如领取了第i个红包,那么在\(d_ ...
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...
- Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希
https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...
- Codeforces Round #303 (Div. 2) C dp 贪心
C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 #427 (Div. 2) D dp
D. Palindromic characteristics time limit per test 3 seconds memory limit per test 256 megabytes inp ...
随机推荐
- 在页面rem布局中用到的js代码
在你的页面加入以下代码,就初步搞定了 function fontSize() { var view_width = document.getElementsByTagName('html')[0].g ...
- ArcGIS几种数据格式2
各种数据的组织形式不一样,其中shp.Coverage.Raster.CAD为文件类型,Geodatabase为空间数据库.Workstaion常用Coverage数据格式.现在ESRI公司推荐使用G ...
- [SVN]TortoiseSVN工具培训3─使用基本流程和图标说明
1.SVN的使用基本流程 注意:对于文件编辑方面,上图的编辑副本操作前建议进行Get lock操作,以防出现后续的冲突等异常报错. 2.SVN的基本图标说明
- ssh-agent && ssh-agent forward && SSH ProxyCommand
http://blog.csdn.net/sdcxyz/article/details/41487897 https://developer.github.com/guides/using-ssh-a ...
- Windows服务程序时钟调用
1 大概思路 设计服务程序 创建服务 安装必备组件 编写Service1 运行效果 2 设计服务程序 创建服务程序,通过添加System.Timers时钟进行定时向Wecome ...
- frame、window和dialog区别
属性 Window Frame Dialog 模式化 不是 不是 不是(可设置) 可调大小 不可 可 可 标题栏 无 有 有 边界 无 有 有 标题 无 有 有 菜单栏 无 有 无 焦点管理器 有 有 ...
- Selenium入门8 js调用
execute_script 可以执行js和jquery脚本 示例如下:修改百度首页的按钮字体颜色,按钮隐藏,按钮显示 #coding=utf-8 # 调用js jquery from seleniu ...
- python title() upper() lower() 以首字母大写的方式显示每个单词/将字符串改为全部大写或全部小写
以首字母大写的方式显示每个单词 [root@chenbj python]# cat name.py #!/usr/bin/env python # _*_ coding:utf-8 _*_ name ...
- Adobe Photoshop CS6下载安装
下载链接 http://yunpan.cn/cACgP6Lv5ygit (提取码:f37a) 第一步关键是断开网络,拔掉网线或断开无线 点击登录后,如果没有进入下面的界面,检查是否断网成功 至于选择安 ...
- apache以天为单位生成日志
编辑/etc/httpd/conf.d/vhost.conf,修改ErrorLog和CustomLog: ErrorLog "|rotatelogs /var/log/httpd/phpdd ...