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 ;
}

bzoj4758: [Usaco2017 Jan]Subsequence Reversal(区间dp)的更多相关文章

  1. HDU Palindrome subsequence(区间DP)

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/Oth ...

  2. [BZOJ4760][Usaco2017 Jan]Hoof, Paper, Scissors dp

    4760: [Usaco2017 Jan]Hoof, Paper, Scissors Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 136  Solv ...

  3. Palindrome subsequence(区间dp+容斥)

    In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting so ...

  4. HDU 4632 Palindrome subsequence (区间DP)

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  5. HDU 4632 Palindrome subsequence(区间dp)

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  6. 【HDU4632 Palindrome subsequence】区间dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题意:给你一个序列,问你该序列中有多少个回文串子序列,可以不连续. 思路:dp[i][j]表示序 ...

  7. 区间dp提升复习

    区间\(dp\)提升复习 不得不说这波题真的不简单... 技巧总结: 1.有时候转移可以利用背包累和 2.如果遇到类似区间添加限制的题可以直接把限制扔在区间上,每次只考虑\([l,r]\)被\([i, ...

  8. HDU4632:Palindrome subsequence(区间DP)

    Problem Description In mathematics, a subsequence is a sequence that can be derived from another seq ...

  9. BZOJ 1719--[Usaco2006 Jan] Roping the Field 麦田巨画(几何&区间dp)

    1719: [Usaco2006 Jan] Roping the Field 麦田巨画 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 82  Solved ...

随机推荐

  1. vijos 2035 奇数偶数与绚丽多彩的数

    描述 Q先生是一个热爱学习的男孩子. 他认为一个 n 位的正整数 x 若能被称作是绚丽多彩的,一定要满足对于{1,3,5,7,9} 中任意一个奇数或者没有在 x 中出现,或者在 x 中出现了恰好奇数次 ...

  2. try catch finally执行顺序 (return / 变量覆盖)

    finally有return 始终返回finally中的return 抛弃 try 与catch中的return 情况1:try{} catch(){}finally{} return x; try{ ...

  3. JAVA实验--统计文章中单词的个数并排序

    分析: 1)要统计单词的个数,就自己的对文章中单词出现的判断的理解来说是:当出现一个非字母的字符的时候,对前面的一部分字符串归结为单词 2)对于最后要判断字母出现的个数这个问题,我认为应该是要用到ma ...

  4. easyUI排序问题

    使用easyUI时,需要在点击页面的某一列进行desc或asc排序,那么在jsp中可以把该列js的sortable 设置true. 加在某字段上时,该字段点击时页面会出现一小三角图案 ,此时easyU ...

  5. 获取鼠标位置的几个通用的JS函数

    原文:http://www.open-open.com/code/view/1421401009218 /*两个通用函数,用于获取鼠标相对于整个页面的当前位置*/ function getX(e) { ...

  6. System表空间大小有10Gb,使用率达到95%,

    System表空间大小有10Gb,使用率达到95%,很好奇, 随后执行如下SQL,查看system表空间中使用空间最多的对象 SQL>SELECT * FROM DBA_SEGMENTS T W ...

  7. 从壹开始前后端分离【重要】║最全的部署方案 & 最丰富的错误分析

    缘起 哈喽大家好!今天是周一了,这几天趁着午休的时间又读了一本书<偷影子的人>,可以看看

  8. JAVA原始的导出excel文件,快捷通用 方便 还能够导出word文档哦

    如今导出excel基本上都是用poi了,当报表格式非常负责的时候 开发难度会加大 假设报表有格式有变化 那就更复杂了,先发现一个非常老的技术.能够解决格式复杂的报表. 实例代码例如以下: <%@ ...

  9. [转]三层架构与MVC之间的区别

    我们平时总是将三层架构与MVC混为一谈,殊不知它俩并不是一个概念.下面我来为大家揭晓我所知道的一些真相. 首先,它俩根本不是一个概念. 三层架构是一个分层式的软件体系架构设计,它可适用于任何一个项目. ...

  10. CentOS 7.0安装Zimbra 8.6邮件服务器

    Zimbra的核心产品是Zimbra协作套件(Zimbra Collaboration Suite,简称ZCS). 系统:Centos7 ip地址:192.168.127.131 安装前准备 1.关闭 ...