A1042 | Subsequence Reversal--Platinum
来源USACO
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
Farmer John is arranging his $N$ cows in a line to take a photo ($1 \leq N
\leq 50$). The height of the $i$th cow in sequence is $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.
To recall, a subsequence is a subset $a(i_1), a(i_2), \ldots, a(i_k)$ of
elements from the cow sequence, found at some series of indices $i_1 < i_2 <
\ldots < i_k$. We say the subsequence is increasing if $a(i_1) \leq a(i_2)
\leq \ldots \leq a(i_k)$.
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.
\leq 50$). The height of the $i$th cow in sequence is $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.
To recall, a subsequence is a subset $a(i_1), a(i_2), \ldots, a(i_k)$ of
elements from the cow sequence, found at some series of indices $i_1 < i_2 <
\ldots < i_k$. We say the subsequence is increasing if $a(i_1) \leq a(i_2)
\leq \ldots \leq a(i_k)$.
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.
输入格式
The first line of input contains $N$. The remaining $N$ lines contain $a(1)
\ldots a(N)$, each an integer in the range $1 \ldots 50$.
\ldots a(N)$, each an integer in the range $1 \ldots 50$.
输出格式
Output the number of elements that can possibly form a longest increasing
subsequence after reversing the contents of at most one subsequence.
subsequence after reversing the contents of at most one subsequence.
输入输出样例
输入 #1
9 1 2 3 9 5 6 8 7 4
输出 #1
9
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted