Ribbon on the Christmas Present

면접 대비

시간 제한2초메모리 제한2048 MB

요약
각 구간의 목표 색조가 주어질 때, 더 어두운 색조로만 덧칠할 수 있다는 조건에서 최소 염색 횟수를 구한다.
난이도

보통10점 중 6점

유형
동적 계획법, 구현, 구간, 그리디
정답자
아직 제출이 없습니다

문제

You are preparing a ribbon to decorate the Christmas present box. You plan to dye the ribbon, initially white, to make a stripe pattern of different shades of red. The ribbon consists of a number of sections, each of which should be dyed as planned.

You want to prepare the ribbon with the least number of dyeing steps. Contiguous sections of the ribbon can be dyed in one step with the same shade of red. A ribbon section already dyed with some shade of red can be overdyed with dyestuff of a darker shade; it is colored with that darker shade. Overdyeing with a lighter shade is, however, not allowed. As the ribbon is initially white, all the sections must be dyed at least once.

Figure A.1. Stripe Pattern of Sample Input 1

Figure A.1 shows the pattern of Sample Input 1. The ribbon has six sections and the numbers in the sections mean the levels of shades to be dyed. Larger numbers mean darker shades. This can be made by three dyeing steps:

  1. dye the entire ribbon with red dyestuff of shade level 5050,
  2. dye the second section from the left with darker shade dyestuff of level 100100, and then 3.
  3. dye the fifth section with dyestuff of level 100100.

Write a program that computes the least number of dyeing steps to make the planned stripe pattern.

입력

The input consists of a single test case of the following format.

nn

d_1d\_1 d_2d\_2 ⋯\cdots d_nd\_n

The test case starts with an integer nn (1≤n≤1001 ≤ n ≤ 100), the number of sections of the ribbon. The second line contains nn integers, d_1,d_2,…,d_nd\_1, d\_2, \dots , d\_n, describing the planned shade levels of the nn sections. Here, d_id\_i means the planned shade level of the ii-th section, which is between 11 and 100100, inclusive, larger meaning darker.

출력

Output a line containing the least number of dyeing steps to make the planned stripe pattern.

예제5

  1. 예제 1

    입력
    6
    50 100 50 50 100 50
    
    예상 출력
    3
    
  2. 예제 2

    입력
    5
    1 2 3 2 1
    
    예상 출력
    3
    
  3. 예제 3

    입력
    5
    3 2 1 2 3
    
    예상 출력
    5
    
  4. 예제 4

    입력
    10
    1 20 100 1 20 20 100 100 20 20
    
    예상 출력
    5
    
  5. 예제 5

    입력
    5
    10 60 100 30 10
    
    예상 출력
    4