Under construction

Still being built. Things may change

Wedding DJ

Interview

Time limit1sMemory limit1024 MB

Summary
Given song fun levels, find the fewest 'change all songs of level x to level y' operations that make the playlist's fun levels never decrease.
Level

Medium5 of 10

Topics
Intervals, Sorting, Hash map, Greedy
Solved
No attempts yet

Problem

You have always wanted to be a DJ and finally got your first opportunity! You have a list of songs that you will play at a wedding in the order the songs appear in the list. Each song has a "fun level" but the problem is that B\&G (the bride and the groom) do not want any song to be played after a song with a lower fun level, i.e., B\&G consider the wedding playlist good only if the fun level of the songs do not ever decrease. Fortunately, you can adjust the fun level of songs. You can choose to change all songs with fun level xx to fun level yy, e.g., you can choose to change all songs with fun level 10 to fun level 7 (or to fun level 18). Note that:

  • When you choose to change all songs with fun level xx to fun level yy, even though all the songs with fun level xx are changed, this is considered as one change.
  • When you choose to change all songs with fun level xx, all the songs with fun level xx change and not a selected subset of the songs with fun level xx.
  • If you change all songs with fun level xx to fun level yy and then you decide to change all the songs with fun level yy to fun level zz, the second change applies to the updated list and not the original list. For example, if the original list is {… 3 … 8 … 3 … 8 … 3 …} and you decide to change fun level 3 to 8 and then decide to change fun level 8 to 2, five songs change their fun level to 2 and not two songs.

Given the order of the fun level of a list of songs, determine the minimum number of playlist adjustments in the form of transforming all songs of level xx into level yy, such that the fun level of the songs of the playlist is non-decreasing.

Input

The first input line contains an integer, nn (1≤n≤100,0001 \le n \le 100{,}000), representing the number of songs in the playlist. The following input line contains nn space separated integers, representing the fun level for the songs in the order they appear in the playlist; each fun level is between 1 and 1,000,000,000 (inclusive).

Output

Print the minimum number of adjustments to make the playlist's fun level non-decreasing.

Hint

Explanation of the first Sample Input/Output:

One way to make the list non-decreasing is:

  • Change all 7's to 1
  • Change all 8's to 3
  • Change all 5's to 3

for a total of 3 changes.

Examples2

  1. Example 1

    Input
    10
    1 7 1 7 8 5 8 3 8 8
    
    Expected output
    3
    
  2. Example 2

    Input
    6
    1 6 3 4 2 1
    
    Expected output
    4