Problem:
Score : 300 points

Problem Statement

You are given an array A of length N.
Your task is to divide it into several contiguous subarrays.
Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order.
At least how many subarrays do you need to divide A into?

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Each A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of subarrays after division of A.

Sample Input 1

6
1 2 3 2 2 1

Sample Output 1

2

One optimal solution is to divide the array into [1,2,3] and [2,2,1].

Sample Input 2

9
1 2 1 2 1 2 1 2 1

Sample Output 2

5

Sample Input 3

7
1 2 3 2 1 999999999 1000000000

Sample Output 3

3

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

use std::fmt::Debug;
use std::io;
use std::io::{Read, Stdin};
use std::str;
use std::str::FromStr;
use std::usize;
use std::cmp;
use std::collections::vec_deque::VecDeque;
use std::i64::MAX;

fn main() {
    let mut sc = Scanner::new();
    let n = sc.parse::<usize>();
    let l = sc.parse::<i64>();
    let t = sc.parse::<i64>();

    let mut x: Vec<i64> = vec![0; n];
    let mut w: Vec<usize> = vec![0; n];

    for i in 0..n {
        x[i] = sc.parse::<i64>();
        w[i] = sc.parse::<usize>();
    }

    let mut cross_count: Vec<i64> = vec![0; n];
    for i in 1..n {
        let w0 = w[0];
        let wi = w[i];
        if w0 == wi {
            continue;
        }

        let interval;
        if w0 == 1 {
            interval = x[i] - x[0];
        } else {
            interval = x[0] + l - x[i];
        }

        if interval > t * 2 {
            continue;
        }

        let time = t * 2 - interval;
        cross_count[i] = time / l + 1;
        if time % l == 0 {
            cross_count[i] -= 1;
        }
    }

    let count_sum: i64 = cross_count.iter().sum::<i64>() % (n as i64);
    let position;
    let num;
    if w[0] == 1 {
        position = (x[0] + t) % l;
        num = count_sum;

    } else {
        position = (x[0] - (t % l) + l) % l;
        num = (n as i64) - count_sum;
    }

    let mut positions: Vec<i64> = Vec::new();
    for i in 0..n {
        let p;
        if w[i] == 1 {
            p = (x[i] + t) % l;
        } else {
            p = (x[i] - (t % l) + l) % l;
        }
        positions.push(p);
    }

    positions.sort();

    let mut a = 0;
    for i in 0..n {
        if positions[i] == position {
            a = i;
            break;
        }
    }

    let mut ans: Vec<i64> = vec![0;n];
    for i in 0..n {
        let idx = (i + num as usize) % n;
        let p = (a + i as usize) % n;
        ans[idx] = positions[p];
    }

    for a in &ans {
        println!("{}", a);
    }

}

struct Scanner {
    stdin: Stdin,
    buf: Vec<u8>,
}

impl Scanner {
    fn new() -> Scanner {
        Scanner {
            stdin: io::stdin(),
            buf: Vec::with_capacity(256),
        }
    }

    fn parse<T: FromStr>(&mut self) -> T
        where <T as FromStr>::Err: Debug
    {
        self.buf.clear();
        let mut it = self.stdin.lock().bytes();
        let mut c = it.next().unwrap().unwrap();
        while c == ' ' as u8 || c == '\n' as u8 {
            c = it.next().unwrap().unwrap();
        }
        while !(c == ' ' as u8 || c == '\n' as u8) {
            self.buf.push(c);
            c = it.next().unwrap().unwrap();
        }
        str::from_utf8(&self.buf).unwrap().parse::<T>().unwrap()
    }
}