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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
class Program
{
    static IO io = new IO();

    static void Main(string[] args)
    {
        //入力
        io.Read();
        int N = io.Int();
        io.ReadSplit();
        int[] A = io.Int(N);

        //計算
        int xCount = 1;
        int xDirection = 0;
        for (int i = 1; i < A.Length; ++i)
        {
            int xNewDirection = Direction(A, i);
            if (xDirection == 0)
            {
                if (xNewDirection == 1)
                {
                    xDirection = 1;
                }
                else if (xNewDirection == -1)
                {
                    xDirection = -1;
                }
            }
            else if (xDirection == 1)
            {
                if (xNewDirection == -1)
                {
                    xCount += 1;
                    xDirection = 0;
                }
            }
            else if (xDirection == -1)
            {
                if (xNewDirection == 1)
                {
                    xCount += 1;
                    xDirection = 0;
                }
            }
        }
        
        //出力
        io.Write(xCount.ToString());

    }
    static int Direction(int[] A, int i)
    {
        if (A[i - 1] == A[i])
        {
            return 0;
        }
        else if(A[i - 1] < A[i])
        {
            return 1;
        }
        else if (A[i - 1] > A[i])
        {
            return -1;
        }
        else
        {
            return 999;
        }
    }
}

class IO
{
    private string[] input;
    private int index;
    public void Read()
    {
        input = new string[1];
        input[0] = Console.ReadLine();
        index = -1;
    }
    public void ReadSplit()
    {
        input = Console.ReadLine().Split(' ');
        index = -1;
    }
    public string String()
    {
        index += 1;
        return input[index];
    }
    public string[] String(int count)
    {
        index += 1;
        string[] rv = new string[count];
        Array.Copy(input, index, rv, 0, count);
        return rv;
    }
    public int Int()
    {
        index += 1;
        return int.Parse(input[index]);
    }
    public int[] Int(int count)
    {
        index += 1;
        int[] rv = new int[count];
        for (int i = 0; i < count; ++i)
        {
            rv[i] = int.Parse(input[index + i]);
        }
        return rv;
    }
    public long Long()
    {
        index += 1;
        return long.Parse(input[index]);
    }
    public long[] Long(int count)
    {
        index += 1;
        long[] rv = new long[count];
        for (int i = 0; i < count; ++i)
        {
            rv[i] = long.Parse(input[index + i]);
        }
        return rv;
    }
    public void Write(string str)
    {
        Console.WriteLine(str);
    }
}