题目描述

Farmer John is arranging his NN cows in a line to take a photo (1 \leq N \leq 501≤N≤50). The height of the iith cow in sequence is a(i)a(i), and Farmer John thinks it would make for an aesthetically pleasing photo if the cow lineup has a large increasing subsequence of cows by height.

FJ would like there to be a long increasing subsequence within his ordering of the cows. In order to ensure this, he allows himself 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 subsequence, given that you can choose to reverse an arbitrary subsequence once.

FJ正在安排他的N头奶牛排成一队以拍照(1<=n<=50)。队列中的第i头奶牛的身高为a[i],并且FJ认为如果奶牛的身高序列中含有一个很长的不下降子序列的话,这会是一张很好的照片。

回忆一下,子序列是由牛序列中的一些元素a[i_1],a[i_2],.....a[i_k]组成的子集。(i_1<i_2<...<i_k) 如果a[i_1]<=a[i_2]<=a[i_3]<=......<=a[i_k]的话,我们就说这个序列是不下降的。

FJ想要在他的奶牛序列中包括一个长期增长的子序列(也就是很长的不下降子序列)。为了确保这一点,他允许自己在一开始选择任何子序列并逆转其元素。

观察这个子序列(上方英文)是如何反转并占据他们最初的相同的位置的,且其他元素保持不变。

在只能反转一次任意子序列的情况下,请找到不下降子序列的最大可能长度。

输入输出格式

输入格式:

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

第一行一个整数N。

接下来N行包括N个整数a[1]...a[n],每行一个在1到50之间的整数。

输出格式:

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

输出反转一次任意子序列后所得到的不下降子序列的最大可能长度。

输入输出样例

输入样例#1:

9

1

2

3

9

5

6

8

7

4

输出样例#1:

9

解题报告:

很巧妙的思维题啊,一开始丝毫没有思路,瞟了一眼题解,看到一句话:交换一个序列相当于不相交的交换几个元素.因为这几个元素交换之后就相当于是交换了一个子序列

有了这句话的提示,问题就迎刃而解了.关键要做到交换的元素不能相交,其实只需要DP的顺序没问题即可,那么显然假设[L,R]已经解决了,那么交换的元素就强制只能在[L,R]之外.

下面正式开始DP,定义\(f[l][r][i][j]\)表示区间[l,r]间,值域在[i,j]间的最长上升子序列的长度,那么就转移就是交换和不交换的区别.

\(f[l][r][i][j]=Max(f[l+1][r][i][j]+(a[l]==i),f[l][r-1][i][j]+(a[r]==j))\)

\(f[l][r][i][j]=Max(f[l+1][r-1][i][j]+(a[l]==j)+(a[r]==i))\)

一式是不交换的转移,二式是交换的转移

注意:\(f[l][r][i-1][j]=f[l][r][i][j]\),\(f[l][r][i][j+1]=f[l][r][i][j]\)

注意值域是可以向两边扩展的

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=55;
int n,a[N],f[N][N][N][N];
void work()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++){
for(int j=1;j<=a[i];j++)
for(int k=a[i];k<=50;k++)
f[i][i][j][k]=1;
}
for(int k=2;k<=n;k++){
for(int l=1;l<=n;l++){
int r=l+k-1;if(r>n)break;
for(int i=1;i<=50;i++){
for(int j=i;j<=50;j++){
f[l][r][i][j]=Max(f[l][r][i][j],
Max(f[l+1][r][i][j]+(a[l]==i),
f[l][r-1][i][j]+(a[r]==j)));
f[l][r][i][j]=Max(f[l][r][i][j],
f[l+1][r-1][i][j]+
(a[r]==i)+(a[l]==j));
f[l][r][i][j+1]=
Max(f[l][r][i][j+1],f[l][r][i][j]);
f[l][r][i-1][j]=
Max(f[l][r][i-1][j],f[l][r][i][j]);
}
}
for(int j=1;j<=50;j++)
for(int i=j;i>=1;i--)
f[l][r][i-1][j]=Max(f[l][r][i-1][j],f[l][r][i][j]);
}
}
printf("%d\n",f[1][n][1][50]);
} int main()
{
work();
return 0;
}

[USACO17JAN]Subsequence Reversal序列反转的更多相关文章

  1. [USACO17JAN] Subsequence Reversal序列反转 (dfs+记忆化)

    题目大意:给你一个序列,你可以翻转任意一段子序列一次,求最长不下降子序列长度 tips:子序列可以不连续,但不能破坏在原序列中的顺序 观察数据范围,n<=50,很小,考虑dfs *dfs来跑区间 ...

  2. [USACO17JAN]Subsequence Reversal

    嘟嘟嘟 这题刚开始是什么思路也没有,关键是不知道怎么解决序列反转的问题. 然后我就想到如果暴力反转一个序列的话,实际上就是不断交换数组中的两个数ai和aj,同时要满足交换的数不能交叉. 然后又看了一眼 ...

  3. bzoj4758: [Usaco2017 Jan]Subsequence Reversal(区间dp)

    4758: [Usaco2017 Jan]Subsequence Reversal Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 76  Solved ...

  4. Python-序列反转和序列反转协议-reversed __reversed__

    reversed 将序列反转,依次把最后的元素放到第一个位置,把第一元素放到最后一个位置,变成生成器对象 name = "beimenchuixue" print(next(rev ...

  5. 2018.08.16 洛谷P3607 [USACO17JAN]序列反转(线性dp)

    传送门 一道感觉比较简单的dp. 注意是要求翻转一个子序列而不是一段连续的数(被坑了很多次啊)... 看到数据范围果断开一个四维数组来dp一波. 我们显然可以用f[i][j][k][t]表示下标在[l ...

  6. Common Subsequence 最大公共子序列问题

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  7. Subsequence(序列自动机模板题)

    题目链接:https://nanti.jisuanke.com/t/38232 题目大意:给你一个字符串,然后再给你m个字符串,然后问你在第一个字符串中不连续的子串能不能构成输入的子串. 具体思路:构 ...

  8. 376 Wiggle Subsequence 摆动序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  9. 学习笔记TF020:序列标注、手写小写字母OCR数据集、双向RNN

    序列标注(sequence labelling),输入序列每一帧预测一个类别.OCR(Optical Character Recognition 光学字符识别). MIT口语系统研究组Rob Kass ...

随机推荐

  1. Django 模版语法

    一.简介 模版是纯文本文件.它可以产生任何基于文本的的格式(HTML,XML,CSV等等). 模版包括在使用时会被值替换掉的 变量,和控制模版逻辑的 标签. {% extends "base ...

  2. Django 个性化管理员站点

    from django.contrib import admin # Register your models here. from .models import Moment class Momen ...

  3. Lucene 的索引文件锁原理

    Lucene 的索引文件锁原理 2016/11/24 · IT技术 · lucene   环境 Lucene 6.0.0Java “1.8.0_111”OS Windows 7 Ultimate 线程 ...

  4. bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪

    http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...

  5. c# gridview 新增行

    string[] newRow = {"long","d","b"}; Gridview.Rows.Insert(Gridview.Rows ...

  6. java的<<左移,>>右移,>>>无符号右移

    >>右移 右移,道在二进制中,假设用一个32位的Int表示一个64,那么高位就都是0,所以当我们把整个二进制数右移,如0100000 >> 2 = 0001000,可以看到右移 ...

  7. 基于ssm的poi反射bean实例

    一:该例子是笔者在实际项目应用过程中,针对项目完成的一套基于poi的导入导出例子,其中一些与项目有关的代码大家直接替换成自己的需求即可. 二:笔者在项目中使用的是poi的XSSF,对应maven的po ...

  8. lodash源码分析之获取数据类型

    所有的悲伤,总会留下一丝欢乐的线索,所有的遗憾,总会留下一处完美的角落,我在冰峰的深海,寻找希望的缺口,却在惊醒时,瞥见绝美的阳光! --几米 本文为读 lodash 源码的第十八篇,后续文章会更新到 ...

  9. Python内置函数(46)——format

    英文文档: format(value[, format_spec]) Convert a value to a "formatted" representation, as con ...

  10. 深入了解GOT,PLT和动态链接

    之前几篇介绍exploit的文章, 有提到return-to-plt的技术. 当时只简单介绍了 GOT和PLT表的基本作用和他们之间的关系, 所以今天就来详细分析下其具体的工作过程. 本文所用的依然是 ...