Problem Statement

There is an empty sequence $X$ and an empty stack $S$. Also, you are given an integer sequence $A=(a_1,\ldots,a_N)$ of length $N$.

For each $i=1,\ldots,N$ in this order, Takahashi will do one of the following operations:

  • Move the integer $a_i$ onto the top of $S$.
  • Discard the integer $a_i$ from $A$.

Additionally, Takahashi may do the following operation whenever $S$ is not empty:

  • Move the integer at the top of $S$ to the tail of $X$.

The score of the final $X$ is defined as follows.

  • If $X$ is non-decreasing, i.e. if $x_i \leq x_{i+1}$ holds for all integer $i(1 \leq i \lt |X|)$, where $X=(x_1,\ldots,x_{|X|})$, then the score is $|X|$ (where $|X|$ denotes the number of terms in $X$).
  • If $X$ is not non-decreasing, then the score is $0$.

Find the maximum possible score.

Constraints

  • $1 \leq N \leq 50$
  • $1 \leq a_i \leq 50$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$a_1$ $\ldots$ $a_N$

Output

Print the answer.


Sample Input 1

7
1 2 3 4 1 2 3

Sample Output 1

5

The following operations make the final $X$ equal $(1,1,2,3,4)$, for a score of $5$.

  • Move $a_1=1$ onto the top of $S$.
  • Move $1$ at the top of $S$ to the tail of $X$.
  • Move $a_2=2$ onto the top of $S$.
  • Discard $a_3=3$.
  • Move $a_4=4$ onto the top of $S$.
  • Move $a_5=1$ onto the top of $S$.
  • Move $1$ at the top of $S$ to the tail of $X$.
  • Move $a_6=2$ onto the top of $S$.
  • Move $2$ at the top of $S$ to the tail of $X$.
  • Move $a_7=3$ onto the top of $S$.
  • Move $3$ at the top of $S$ to the tail of $X$.
  • Move $4$ at the top of $S$ to the tail of $X$.

We cannot make the score $6$ or greater, so the maximum possible score is $5$.


Sample Input 2

10
1 1 1 1 1 1 1 1 1 1

正常dp很难做,这题其实有一点区间的感觉,考虑区间 dp.

首先发现其实放入栈再拿出其实就是反转操作。反转后还要满足递增.为了控制递增这个条件,我们需要给 dp 定义再加上值域两维去记录。定义 \(dp_{l,r,x,y}\) 代表从第 \(l\) 个数到第 \(r\) 个数进行操作,且最终序列的数再值域 \([x,y]\) 中时,最多能放入多少个数。

考虑是否把 \(a_l\) 放入栈中,如果不放,\(dp_{l,r,x,y}=dp_{l+1,r,x,y}\)

如果放入,首先要满足 \(a_l\in [x,y]\),然后枚举我把那个数放入后再弹出 \(a_l\),如果放入\(a_j\)后弹出 \(a_l\),那么\(dp_{l,r,x,y}\) 可以从 \(dp_{l,j,x,a_l}+dp_{j+1,r,a_l,y}\)。

#include<bits/stdc++.h>
using namespace std;
const int N=55;
int n,a[N],dp[N][N][N][N];
int dfs(int l,int r,int x,int y)
{
if(l>r||x>y)
return 0;
if(~dp[l][r][x][y])
return dp[l][r][x][y];
int ans=dfs(l+1,r,x,y);
if(a[l]<x||a[l]>y)
return dp[l][r][x][y]=ans;
for(int j=l;j<=r;j++)
ans=max(ans,dfs(l+1,j,x,a[l])+dfs(j+1,r,a[l],y)+1);
return dp[l][r][x][y]=ans;
}
int main()
{
memset(dp,-1,sizeof(dp));
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
printf("%d",dfs(1,n,1,50));
}

[ABC262G] LIS with Stack的更多相关文章

  1. B. Once Again... 解析(思維、DP、LIS、矩陣冪)

    Codeforce 582 B. Once Again... 解析(思維.DP.LIS.矩陣冪) 今天我們來看看CF582B 題目連結 題目 給你一個長度為\(n\)的數列\(a\),求\(a\)循環 ...

  2. uva10635 LIS

    Prince and PrincessInput: Standard Input Output: Standard Output Time Limit: 3 Seconds In an n x n c ...

  3. POJ 2533 Longest Ordered Subsequence(LIS模版题)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47465   Acc ...

  4. hdu 5256 LIS变形

    给一个数列,问最少修改多少个元素使数列严格递增.如果不是要求“严格”递增,那就是求最长不降子序列LIS,然后n-LIS就是答案.要严格递增也好办,输入的时候用每个数减去其下标处理一下就行了. /* * ...

  5. HDU 5489 Removed Interval (LIS变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5489 给你n个数,要删去其中连续的L个,问你删去之后的LIS最大是多少? 我们先预处理出以i下标为开头 ...

  6. LIS (最长上升子序列)

    LIS两种写法 O(n^2) dp[i]表示以a[i]结尾的为LIS长度 #include <algorithm> #include <iostream> #include & ...

  7. HDU5087——Revenge of LIS II(BestCoder Round #16)

    Revenge of LIS II Problem DescriptionIn computer science, the longest increasing subsequence problem ...

  8. HDU-4742 Pinball Game 3D 三维LIS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4742 题意:求3维的LIS.. 用分治算法搞得,参考了cxlove的题解.. 首先按照x排序,然后每个 ...

  9. POJ 3670 , 3671 LIS

    题意:两题意思差不多,都是给你一个序列,然后求最少需要改变多少个数字,使得成为一个最长不升,或者最长不降子序列. 当然3671是只能升序,所以更简单一点. 然后就没有什么了,用二分的方法求LIS即可. ...

  10. SPOJ 3937 - Wooden Sticks 最长上升子序列LIS

    给了n个(n<=5000)木棍的长度hi与宽度wi(均小于10000),现在机器要打磨这些木棍,如果相邻连个木棍hi<=hj并且wi<=wj就不需要调整机器,问如何排序使得机器调整的 ...

随机推荐

  1. 浅谈基于QT的截图工具的设计与实现

    本人一直在做属于自己的一款跨平台的截图软件(w4ngzhen/capi(github.com)),在软件编写的过程中有一些心得体会,所以有了本文.其实这篇文章酝酿了很久,现在这款软件有了雏形,也有空梳 ...

  2. LeetCode46全排列(回溯入门)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 题目描述 难度:中等 给定一个不含重复数字的数组 nu ...

  3. 从壹开始前后端开发【.Net6+Vue3】(二)前端框架

    项目名称:KeepGoing(继续前进) 介绍 工作后,学习的脚步一直停停走走,希望可以以此项目为基础,可以不断的迫使自己不断的学习以及成长 将以Girvs框架为基础,从壹开始二次开发一个前后端管理框 ...

  4. 从软件工程师角度聊聊 Kubernetes

    作为软件工程师,我们应该熟悉 K8s,尽管它有点像 DevOps,但它能让我们更好地了解幕后发生的事情,让我们与部署工作更密切相关,更有责任感.本文将从软件工程师的角度探讨 Kubernetes (K ...

  5. HTML网页/KRPano项目一键打包EXE工具(HTML网页打包成单个windows可执行文件exe)

    HTML一键打包EXE工具使用说明 工具简介 HTML一键打包EXE工具(HTML封装EXE,桌件)能把任意HTML项目(网址)一键打包为单个EXE文件,可以脱离浏览器和服务器,直接双击即可运行.支持 ...

  6. MySQL的字段数量以及长度限制

    一.InnoDB行格式 行格式 紧凑的存储特性 增强的可变长度列存储 大型索引键前缀支持 压缩支持 支持的表空间类型 REDUNDANT N N N N system, file-per-table, ...

  7. 「tricks」平凡二分幻术

    其实这个的标题叫 平凡线段树上二分幻术,因为这是一个民科在乱叫. 如标题所言,这个东西确实非常 trivial.碍于网络上没有一个成体系的文章供参考就只能自己来炒炒冷饭. 如果出了什么 bug 就当个 ...

  8. E-GraphSAGE: A Graph Neural Network based Intrusion Detection System 笔记

    E-GraphSAGE: A Graph Neural Network based Intrusion Detection System 目录 E-GraphSAGE: A Graph Neural ...

  9. 【matplotlib 实战】--折线图

    折线图是一种用于可视化数据变化趋势的图表,它可以用于表示任何数值随着时间或类别的变化. 折线图由折线段和折线交点组成,折线段表示数值随时间或类别的变化趋势,折线交点表示数据的转折点. 折线图的方向表示 ...

  10. 如何vue3中使用全局变量,与Vue2的区别

    对比: 在vue2.x中我们挂载全局变量或方法是通过是使用Vue.prototype.$xxxx=xxx的形式来挂载,然后通过this.$xxx来获取挂载到全局的变量或者方法 但是 在vue3.x中显 ...