#abc262g. G - LIS with Stack

G - LIS with Stack

Score : 600600 points

问题描述

存在一个空的序列 XX 和一个空栈 SS。同时,你得到一个整数序列 A=(a1,,aN)A=(a_1,\ldots,a_N),其长度为 NN

对于每个 i=1,,Ni=1,\ldots,N(按照这个顺序),Takahashi 将执行以下操作之一:

  • 将整数 aia_i 移动到 SS 的顶部。
  • AA 中丢弃整数 aia_i

另外,当 SS 不为空时,Takahashi 可以执行以下操作:

  • SS 顶部的整数移动到 XX 的尾部。

最终 XX 的得分定义如下:

  • 如果 XX 是非递减的,即对于所有整数 i(1i<X)i(1 \leq i < |X|),满足 xixi+1x_i \leq x_{i+1}(其中 X=(x1,,xX)X=(x_1,\ldots,x_{|X|})),则得分是 X|X|(其中 X|X| 表示 XX 中项的数量)。
  • 如果 XX 不是非递减的,则得分是 00

找出可能的最大得分。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is an empty sequence XX and an empty stack SS. Also, you are given an integer sequence A=(a1,,aN)A=(a_1,\ldots,a_N) of length NN.
For each i=1,,Ni=1,\ldots,N in this order, Takahashi will do one of the following operations:

  • Move the integer aia_i onto the top of SS.
  • Discard the integer aia_i from AA.

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

  • Move the integer at the top of SS to the tail of XX.

The score of the final XX is defined as follows.

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

Find the maximum possible score.

Constraints

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

Input

Input is given from Standard Input in the following format:

NN

a1a_1 \ldots aNa_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 XX equal (1,1,2,3,4)(1,1,2,3,4), for a score of 55.

  • Move a1=1a_1=1 onto the top of SS.
  • Move 11 at the top of SS to the tail of XX.
  • Move a2=2a_2=2 onto the top of SS.
  • Discard a3=3a_3=3.
  • Move a4=4a_4=4 onto the top of SS.
  • Move a5=1a_5=1 onto the top of SS.
  • Move 11 at the top of SS to the tail of XX.
  • Move a6=2a_6=2 onto the top of SS.
  • Move 22 at the top of SS to the tail of XX.
  • Move a7=3a_7=3 onto the top of SS.
  • Move 33 at the top of SS to the tail of XX.
  • Move 44 at the top of SS to the tail of XX.

We cannot make the score 66 or greater, so the maximum possible score is 55.

Sample Input 2

10
1 1 1 1 1 1 1 1 1 1

Sample Output 2

10

update @ 2024/3/10 11:06:47