#P11106. [ROI 2023] 峰值 (Day 1)

[ROI 2023] 峰值 (Day 1)

Background

Translated from ROI 2023 D1T3

If for all 1j<i1 \le j < i, we have aj<aia_j < a_i, then aia_i is called a peak.

If for all 1j<i1 \le j < i, we have aj>aia_j > a_i, then aia_i is called an anti-peak.

Problem Description

You are given a permutation p1,p2,,pnp_1,p_2,\dots,p_n of size nn. You need to split it into two non-empty subsequences qq and rr. Each element of pp must be assigned to exactly one subsequence. You need to maximize the sum of the number of peaks in qq and the number of anti-peaks in rr.

Input Format

Each test consists of multiple test cases. The first line contains an integer t(1t100,000)t(1 \le t \le 100,000), the number of test cases. The next 2t2t lines describe the test cases, with each test case given in two lines.

The first line of each test case contains an integer n(2n200,000)n(2 \le n \le 200,000), the size of the permutation.

The second line of each input test case contains nn integers p1,p2,,pnp_1,p_2,\dots,p_n, the original permutation. It is guaranteed that each number appears exactly once in pp.

The sum of nn over all test cases does not exceed 200,000200,000.

Output Format

For each test case, output one integer: the maximum possible value of the number of peaks in qq plus the number of anti-peaks in rr after splitting pp.

4
5
4 1 2 3 5
10
3 8 10 4 1 2 7 9 5 6
3
1 2 3
6
4 2 5 1 6 3
5
6
3
5

Hint

Explanation of the first two samples:

Let N=nN=\sum n.

Subtask Score n,Nn,N Special Properties
11 2121 n16n\le16 t120t\le120
22 2222 n200,N2000n\le200,N\le2000
33 1414 N2000N\le2000
44 1010 N20000N\le20000
55 1313 N200000N\le200000 The length of the longest decreasing subsequence of pp is at most 22
66 2020

Translated by ChatGPT 5