Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

CodeForce 1760D Challenging Valleys #150

Open
Woodyiiiiiii opened this issue Nov 22, 2022 · 0 comments
Open

CodeForce 1760D Challenging Valleys #150

Woodyiiiiiii opened this issue Nov 22, 2022 · 0 comments

Comments

@Woodyiiiiiii
Copy link
Owner

这道题使用的方法是状态机

  1. 观察,发现只有^形或者之后的变化是不符合条件的
  2. 将其命名为一个状态,只要不是该状态的都符合条件
  3. 分状态

还不是很懂状态机类型的题目,还需要多做题。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        while (T -- > 0){
            int n = scanner.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = scanner.nextInt();
            }
            System.out.println(checkV(arr) ? "YES" : "NO");
        }
    }

    public static boolean checkV(int[] arr) {
        int state = 0;
        // 0 - init, 1 - up, 2 - down, 3 - up and down

        for (int i = 1; i < arr.length; ++i) {
            // trend not change
            if (arr[i] == arr[i - 1]) {
                continue;
            }

            if (arr[i] > arr[i - 1]) {
                state = 1;
            } else {
                if (state == 0) {
                    state = 2;
                } else if (state == 1) {
                    state = 3;
                    break;
                }
            }
        }

        return state != 3;
    }



}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant