4758: [Usaco2017 Jan]Subsequence Reversal

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 76  Solved: 52
[Submit][Status][Discuss]

Description

Farmer John is arranging his NN cows in a line to take a photo (1≤N≤50). The height of the iith co
w in sequence is a(i), and Farmer John thinks it would make for an aesthetically pleasing photo if t
he cow lineup has a large increasing subsequence of cows by height.To recall, a subsequence is a sub
set a(i1),a(i2),…,a(ik)) of elements from the cow sequence, found at some series of indices i1<i2<
…<ik, We say the subsequence is increasing if a(i1)≤a(i2)≤…≤a(ik).FJ would like there to be a l
ong increasing subsequence within his ordering of the cows. In order to ensure this, he allows himse
lf initially to choose any subsequence and reverse its elements.
 
For example, if we had the list
 
1 6 2 3 4 3 5 3 4
We can reverse the chosen elements
 
1 6 2 3 4 3 5 3 4
  ^         ^ ^ ^
to get
 
1 4 2 3 4 3 3 5 6
  ^         ^ ^ ^
Observe how the subsequence being reversed ends up using the same indices as it initially occupied, 
leaving the other elements unchanged.Please find the maximum possible length of an increasing subseq
uence, given that you can choose to reverse an arbitrary subsequence once.
给定一个长度为N的序列,允许翻转一个子序列,求最长不下降子序列长度。n和数字都<=50
 

Input

The first line of input contains N. The remaining N lines contain a(1)…a(N),
each an integer in the range 1…50.
 

Output

Output the number of elements that can possibly form a longest increasing subsequence 
after reversing the contents of at most one subsequence.
 

Sample Input

9
1
2
3
9
5
6
8
7
4

Sample Output

9

HINT

 

Source

Platinum

/*
感觉这道题没完全懂
开始设状态毫无思路,只知道可能很多维......
想到可能是道区间dp,emm那就考虑一段区间[l,r]怎么维护里面交换那些数呢?
发现可以用值域这个东西把数给框住。又,反转区间肯定是越靠右的反转到越靠左位置。
那么由小区间推大区间时,只需要判断端点处包不包括在这一次的交换中即可。
所以可dp[i][j][L][R]为区间[i,j]里面min(ak) >= L, max(ak) <= R时,反转一次的最长不下降子序列。
转移见代码。
*/
#include<bits/stdc++.h> #define N 51 using namespace std;
int n,a[N],ans;
int dp[N][N][N][N]; inline int read()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int main()
{
n=read();
for(int i=; i <= n; ++i) a[i]=read(),dp[i][i][a[i]][a[i]]=; for(int len=; len <= n; ++len) for(int i=; i+len- <= n; ++i)//当前区间
{
int j=i+len-;
for(int l=; l <= ; ++l) for(int L=; L+l- <= ; ++L)//当前值域
{
int R=L+l-;
ans=dp[i][j][L][R];
ans=max(ans,max(dp[i+][j][L][R],dp[i][j-][L][R]));
ans=max(ans,max(dp[i][j][L+][R],dp[i][j][L][R-]));
dp[i][j][L][R]=ans;
//copy小区间的答案
dp[i][j][min(L,a[i])][R]=max(dp[i][j][min(L,a[i])][R],dp[i+][j][L][R]+(a[i] <= L));
dp[i][j][L][max(R,a[j])]=max(dp[i][j][L][max(R,a[j])],dp[i][j-][L][R]+(a[j] >= R));
dp[i][j][min(L,a[i])][max(R,a[j])]=max(dp[i][j][min(L,a[i])][max(R,a[j])],dp[i+][j-][L][R]+(a[j] >= R)+(a[i] <= L));
//a[i]与a[j]不交换
dp[i][j][min(L,a[j])][R]=max(dp[i][j][min(L,a[j])][R],dp[i+][j-][L][R]+(a[j] <= L));
dp[i][j][L][max(R,a[i])]=max(dp[i][j][L][max(R,a[i])],dp[i+][j-][L][R]+(a[i] >= R));
dp[i][j][min(L,a[j])][max(R,a[i])]=max(dp[i][j][min(L,a[j])][max(R,a[i])],dp[i+][j-][L][R]+(a[i] >= R)+(a[j] <= L));
//a[i]与a[j]交换
}
}
cout<<dp[][n][][]<<endl;
return ;
}

最新文章

  1. 你必须知道的指针基础-7.void指针与函数指针
  2. 【翻译】jQuery是有害的
  3. RabbitMQ官方中文入门教程(PHP版) 第一部分:Hello World
  4. SqlServer代理执行[分发清除: distribution] 无法删除快照文件
  5. Navicat提示Access violation at address 004E9844 in module ‘navicat.exe’
  6. Ext.grid.EditorGridPanel分页和查看全部
  7. HP LasterJet 3050驱动安装技巧,面向win7,8,8.1,10
  8. MyEclipse 10 报错记录
  9. 把玩Pencil项目之编译并运行
  10. codeforces Hello 2019(未写完)
  11. 某些浏览器具有dns缓存功能,大家更改域名指向,建议清理下浏览器缓存
  12. UVALive - 6440(模拟)
  13. Oracle学习笔记--第2章 oracle 数据库体系结构
  14. Nuget EPPlus的使用
  15. golang 如何查看channel通道中未读数据的长度
  16. NodeJS + React + Webpack + Echarts
  17. [UE4GamePlay架构(九)GameInstance(转)
  18. android中activity向service中传值
  19. centos7源码安装vim8.0
  20. python爬虫入门(5)-Scrapy概述

热门文章

  1. POJ 1094 Sorting It All Out【拓扑排序】
  2. Python从文件中读取字符串,用正则表达式匹配中文字符的问题
  3. Java fail-fast 与 fail-safe 机制对比
  4. 使用Spring Data Redis操作Redis(单机版)
  5. sqlite中常见的问题总结
  6. JVM监控工具:jps、jstat、jinfo、jmap、jhat、jstack使用介绍
  7. Meteor集合
  8. 【APUE】关于信号的一些常用函数
  9. Bound mismatch: The typae CertificateDirectory is not a valid substitute for the bounded parameter &lt;M extends Serializable&gt;
  10. 微信小程序之 页面跳转 及 调用本地json的假数据调试