Niuniu's sorting subsequence problem

1. Problem description

1) Sorting subsequence Niu Niu defines a sorting subsequence as a continuous subsequence in an array, and this subsequence is non-increasing or non-decreasing sorting . Niu Niu has an integer array A of length n. Now he has a task to divide the array A into several sorted subsequences. Niu Niu wants to know how many sorted subsequences he can at least divide this array into. As shown in the example , Niuniu can divide the array A into two sorted subsequences [1, 2, 3] and [2, 2, 1], at least need to be divided into 2 sorted subsequences

2) Input description: The first line of input is a positive integer n(1 ≤ n ≤ 10 ^ 5) The second line includes n integers A_i (1 ≤ A_i ≤ 10 ^ 9), representing each number of the array A.

3) Output description: Output an integer indicating how many sorted subsequences Niuniu can divide A into at least

4) Example:
input 6 1 2 3 2 2 1
output 2

2. Problem-solving ideas

1)
Subsequences have the following requirements: non-increasing or non-decreasing sorting;
increasing sequence: 1 2 3 4 ...
decreasing sequence: 9 7 6 5 ...
neither increasing nor decreasing sequence: 1 2 2 3 4 or 9 7 6 6 6 5

To sum up: the order of the subsequences can be sorted arbitrarily, which is not required

So how to judge it as a subsequence?
non-increasing then it is neither increasing nor decreasing or decreasing sequence;
non-decreasing then it is neither increasing nor decreasing or increasing sequence;

To sum up:
when a set of data is given, it was originally an increasing sequence (1 2 3 ...) until the next number changes the original increasing sequence (1 2 3 2 ... or 1 2 3 3 ), then this When all the numbers before this number can be divided into a group, it can be regarded as a subsequence that is increasing or neither increasing nor decreasing;
when a set of data is given, it is originally a decreasing sequence (6 5 4 ...) until the next number let It changes the original descending sequence (6 5 4 3 ... or 6 5 4 4 ), then all the numbers before this number can be grouped into a group, which can be regarded as a descending or neither increasing nor decreasing subsequence;

2) Output requirements: At least how many subsequences the array A can be divided into is at least the key! ! !
So, how to judge at most at least the problem?

Look at a set of examples:
insert image description herea According to the above rules for determining subsequences, the following subsequence groupings can be obtained

insert image description here
1) It was originally a descending sequence (6 2 1), and when the next 1 comes in, it becomes (6 2 1 1), which meets the requirements and turns the original descending sequence into a sequence that neither increases nor decreases, so ( 6 2 1) Can be used as a subsequence
insert image description here
2) It was originally a temporarily disordered sequence (1), and when the next 1 comes in, it becomes (1 1 ), meeting the requirements so that the original sequence becomes a non-increasing It is not a descending sequence, so (1) can be a separate subsequence
insert image description here
3) It was originally an increasing sequence (1 2 ), when the next 1 comes in, it becomes (1 2 1), which meets the requirements and makes the original increasing sequence It becomes a sequence that is neither increasing nor decreasing, so (1 2 ) can be a subsequence alone
insert image description here
4) The last increasing sequence (1 2), since the data has been traversed, but its sequence has not been changed, so, finally The increasing sequence (1 2) can be a subsequence alone

To sum up: The above set of data can have a total of four subsequences

However, the above set of data can also be divided as follows according to the requirements:
insert image description here
1) When 6, 2, 1 are arranged in sequence, it is a descending sequence at this time, but when the second 1 is arranged, it is found that it becomes a Neither increasing nor decreasing sequence (6 2 1 1), but when the third 1 also comes in, it can still remain as a neither increasing nor decreasing sequence, so we can classify it as a subsequence (6 2 1 1 1)
insert image description here
2) The three data (2 1 2) at this time can obviously be divided into two sets of subsequences (2 1) and (2). subsequences, one less than the first division.

To sum up: If at least this condition needs to be met, when the next number is compared with the current number, if they are the same, the current number and the previous number should be grouped together . Therefore, in order to keep the least subsequence, the judgment of the subsequence becomes the division condition when it encounters a violation of its increment or decrement

3. Code implementation (java)

    public static int fun2() {
    
    
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt(); // 数组长度
        int[] arr = new int[n + 1]; // 防止遍历数组越界
        int len = 0;
        while (scanner.hasNextInt()) {
    
    
            // 存储数据
            for (int i = 0; i < n; i++) {
    
    
                arr[i] = scanner.nextInt();
            }
            int i = 0;
            len = 0;
            while (i < n) {
    
    
                // 递增
                if (arr[i] < arr[i + 1]) {
    
    
                    // 向后继续执行 -- 前提为i不越界,且满足升序
                    while (i < n && arr[i] < arr[i + 1]) {
    
    
                        i++;
                    }
                    // 此时已经非递增 (arr[i+1] <= arr[i]) 则之前为一组
                    len++;
                    // i+1 为下一组子序列开头
                    i++;
                }
                // 相等 -- 既不递增也不递减序列 -- 继续向后排序
                else if (arr[i] == arr[i + 1]) {
    
    
                    while (i < n && arr[i] == arr[i + 1]) {
    
    
                        i++;
                    }
                    // 此时 arr[i+1] > arr[i]  或者  arr[i+1] < arr[i] 则当前arr[i]与之前分为一组
                    i++;
                }
                // 递减
                else {
    
    
                    while (i < n && arr[i] > arr[i + 1]) {
    
    
                        i++;
                    }
                    len++;
                    i++;
                }
            }
        }
        return len;
    }
  • Finally, I hope you can give me some pointers! *

Guess you like

Origin blog.csdn.net/qq_68288689/article/details/127985346