#graph604. 最长递增子序列

最长递增子序列

Longest Increasing Subsequence

Problem Source

Network Flow 24 Problems

Problem Description

Given a sequence of positive integers x1∼xnx_1 \sim x_n (an increasing subsequence below means non-strictly increasing):

  1. Compute the length ss of its longest increasing subsequence.
  2. Compute the maximum number of increasing subsequences of length ss that can be taken from the given sequence (no element may be reused).
  3. If x1x_1 and xnx_n are allowed to be used multiple times in the taken subsequences, compute the maximum number of increasing subsequences of length ss that can be taken.

Input Format

The first line contains one positive integer n(1≤n≤500)n (1\leq n\leq 500), the length of the given sequence. The next line contains nn positive integers x1,x2,…,xnx_1, x_2, \dots, x_n.

Output Format

Three lines, each with one integer, giving the answer to the corresponding question above.

Sample Input

4
3 6 2 5

Sample Output

2
2
3

Constraints

1≤n≤5001\leq n\leq 500.

Hint

  • First compute the longest increasing subsequence length ss with DP. Questions 2 and 3 are classic maximum-flow problems: build a layered network where each element is split into an in-node and an out-node (capacity 1, or infinite for x1x_1 and xnx_n in question 3), and connect elements that can extend an increasing subsequence.