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 ...
随机推荐
- >>我要做特工系列 之 CSS 3_animation_向右滑出后下滑并停止
新手入门还没有正式发点啥东西,都是在装潢博客这个家了,到现在为止还是没有装修好..熟悉了这边的发布规范之后会持续在这里记录,给自己留下学习的脚印~ 这正式的第一篇随笔写个使用css3的动画效果. 总感 ...
- 秒懂JSON.parse()与JSON.stringify()的区别
在网站开发中,Json是最为常见的一种数据交互手段.在使用过程中,常会遇到Json字段串和对象之间进行转换.很多朋友对于JSON.parse() 和JSON.stringify() 的区别,下面为大家 ...
- Design Pattern ->Composite
Layering & Contract Philosophy With additional indirection class CComponent { ; ; ; public: virt ...
- Oracle的oci.dll加载错误解决办法
开始 -> 程序 -> Oracle -> Configuration and Migration Tools -> Net Manager→本地→概要文件→Oracle高级安 ...
- Java1.7新特性
1.switch语句支持字符串变量 public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) { String typeOf ...
- django定时任务小插件
需求 每天请求一封邮件,并读取该邮件 这个其实可以使用linux 自带了crontab实现,但是毕竟是django 开发.想着不知道有没有方法可以从django 中实现. 简单搜索了下,这方面的方法确 ...
- Homestead 中使用 laravel-mix 问题汇总
按照 laravel 官方文档在准备使用 laravel-mix 时遇到了很多问题,许多同学应该会遇到同样的问题,自己花了一些时间来解决这些问题,在此做个笔记帮助大家减少填坑的时间. 环境 larav ...
- 微信小程序实现获得用户手机号
具体操作方法如下: 使用方法 需要将 <button> 组件 open-type 的值设置为 getPhoneNumber,当用户点击并同意之后,可以通过 bindgetphonenumb ...
- May 7th 2017 Week 19th Sunday
A chain is no stronger than its weakest link. 链条的坚固程度取决于它最薄弱的环节. The same as the well-known buckets ...
- iRecognizer号码扫描开发实录
iRecognizer——这是一款可以帮助你快速扫描获得印刷体数字的软件 现已上架 腾讯应用宝,酷安 提供的功能:扫一扫(相册或当场扫描),获得电话号码,之后就可以拨打或者发送短信,自动复制到剪贴板, ...