[ABC262G] LIS with Stack
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的更多相关文章
- B. Once Again... 解析(思維、DP、LIS、矩陣冪)
Codeforce 582 B. Once Again... 解析(思維.DP.LIS.矩陣冪) 今天我們來看看CF582B 題目連結 題目 給你一個長度為\(n\)的數列\(a\),求\(a\)循環 ...
- uva10635 LIS
Prince and PrincessInput: Standard Input Output: Standard Output Time Limit: 3 Seconds In an n x n c ...
- POJ 2533 Longest Ordered Subsequence(LIS模版题)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 47465 Acc ...
- hdu 5256 LIS变形
给一个数列,问最少修改多少个元素使数列严格递增.如果不是要求“严格”递增,那就是求最长不降子序列LIS,然后n-LIS就是答案.要严格递增也好办,输入的时候用每个数减去其下标处理一下就行了. /* * ...
- HDU 5489 Removed Interval (LIS变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5489 给你n个数,要删去其中连续的L个,问你删去之后的LIS最大是多少? 我们先预处理出以i下标为开头 ...
- LIS (最长上升子序列)
LIS两种写法 O(n^2) dp[i]表示以a[i]结尾的为LIS长度 #include <algorithm> #include <iostream> #include & ...
- HDU5087——Revenge of LIS II(BestCoder Round #16)
Revenge of LIS II Problem DescriptionIn computer science, the longest increasing subsequence problem ...
- HDU-4742 Pinball Game 3D 三维LIS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4742 题意:求3维的LIS.. 用分治算法搞得,参考了cxlove的题解.. 首先按照x排序,然后每个 ...
- POJ 3670 , 3671 LIS
题意:两题意思差不多,都是给你一个序列,然后求最少需要改变多少个数字,使得成为一个最长不升,或者最长不降子序列. 当然3671是只能升序,所以更简单一点. 然后就没有什么了,用二分的方法求LIS即可. ...
- SPOJ 3937 - Wooden Sticks 最长上升子序列LIS
给了n个(n<=5000)木棍的长度hi与宽度wi(均小于10000),现在机器要打磨这些木棍,如果相邻连个木棍hi<=hj并且wi<=wj就不需要调整机器,问如何排序使得机器调整的 ...
随机推荐
- Laf & 中大猫谱:让每一只流浪猫都有家
猫谱简介 中大猫谱是一款辅助校园流浪猫救助的开源小程序项目,服务端使用 Laf 云开发. 猫谱主要功能包括:猫咪信息登记.照片分享.拍照识猫.公告和留言等.项目创立的初衷,是解决校园猫猫交流群里的一个 ...
- 推荐vue脚手架工具 vue-cli
安装vue-cli之前,需要先装好vue 和 webpack npm install -g vue //全局安装vue npm install -g webpack //全局安装webpack npm ...
- 【krpano】多分类缩略图及多分类地图案例
该案例提供了场景多分类缩略图展示以及多地图展示,效果如下截图: 下载地址:http://pan.baidu.com/s/1hsA5ta8 感谢群内小伙伴H·T·T的分享 ...
- Llama2-Chinese项目:2.1-Atom-7B预训练
虽然Llama2的预训练数据相对于第一代LLaMA扩大了一倍,但是中文预训练数据的比例依然非常少,仅占0.13%,这也导致了原始Llama2的中文能力较弱.为了能够提升模型的中文能力,可以采用微调 ...
- 大模型时代,如何快速开发AI应用
本文分享自华为云社区 <[云享问答]第3期:大模型时代,如何快速开发AI应用>,作者:华为云社区精选. 大模型快速普及应用的当下,AI浪潮汹涌而至,对于开发者来说,开发一款属于自己的AI应 ...
- 「loj - 3489」「joisc 2021 day 1」Food Court
link. 感觉好久没写过题解了, 这就是永远在骚动的得不到吧. 星尘 infinity 真的非常行, 就算是 ja voicebase 都不知道吊打那群日 v 多少圈. 我推荐你们都去听一听. ch ...
- Linux挂载新磁盘
Linux挂载新磁盘 1. 查看磁盘 # df -lh # 查看磁盘占用情况,同时可以查看已挂载的磁盘及其挂载位置 # fdisk -l # 查看所有的磁盘分区 图中 /dev/sdb 下无分区信息, ...
- cmake构建32位应用程序
1. 背景介绍 2. 工具介绍 3. 环境搭建 4. MinGW编译器版本 1. 背景介绍 最近需要使用第三方动态库文件G33DDCAPI.dll进行二次开发.由于这个动态库文件生成的时间比较早,且只 ...
- u-boot启动流程
U-Boot(Universal Bootloader)是一个通用的开源引导加载程序,通常用于嵌入式系统中,负责引导操作系统或加载 Linux 内核等任务.U-Boot的启动流程可以概括为以下几个关键 ...
- mooc第五单元《管理组织》单元测试
第五单元<管理组织>单元测试 返回 本次得分为:30.00/50.00, 本次测试的提交时间为:2020-08-30, 如果你认为本次测试成绩不理想,你可以选择 再做一次 . 1 ...