[(Vocational Technical Group) Answers to the 11th Blue Bridge Cup Provincial Demo Competition] Given a sequence, how long is the longest increasing sequence in the sequence

Title: Ascending sequence

The problem is described
  in the sequence a [1], a [2],…, a [n], if a [i] <a [i + 1] <a [i + 2] <… <a [j], then Call a [i] to a [j] an increasing sequence with a length of j-i + 1.
  Given a sequence, how long is the longest increasing sequence in the sequence.

Input format
  The first line of input contains an integer n.
  The second line contains n integers a [1], a [2],…, a [n], and adjacent integers are separated by spaces to represent the given sequence.

Output format The
  output line contains an integer, indicating the answer.

Sample input
7
5 2 4 1 3 7 2

Sample output
3

Main points

The problem is given a sequence of numbers. He may have an increasing sequence in the first part, and then cut it off, and then there is an increasing sequence in the back.

Code

import java.util.Scanner;

public class 递增序列 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = sc.nextInt();
        }
        sc.close();

        int temp = array[0];
        int count = 1;
        int max = 0;
        for (int i = 1; i < n; i++) {
            if (temp < array[i]) {
                temp = array[i];
                count++;
            } else { //重新开始一段递增序列
                temp = array[i];
                max = Math.max(count, max);
                count = 1;
            }
        }
        System.out.println(max);
    }
}
Published 139 original articles · praised 129 · 20,000+ views

Guess you like

Origin blog.csdn.net/weixin_43124279/article/details/105666516