Farm

면접 대비

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

요약
직각으로 이루어진 농장 경계와 해충 위치들이 주어질 때, 농장 안의 모든 해충을 덮는 서로 분리된 축에 평행한 직사각형의 최소 개수를 구한다.
난이도

보통10점 중 7점

유형
기하, 그리디, 정렬, 구현
정답자
아직 제출이 없습니다

문제

There is a farm that borders a straight road. Suppose the road is on the xx-axis. Each boundary edge of the farm field is either horizontal or vertical. The leftmost and the rightmost edges are vertical and adjacent to the base edge which lies on the road. The length of the base edge is equal to the sum of the lengths of all other horizontal edges. See Figure C.1 (a).

(a)(b)

Figure C.1. A farm field and the pest infestation locations

In Figure C.1, the dots on the boundary or in the interior of the farm field represent the locations where the pests have infested. To effectively eradicate the infestation, a farmer tries to divide the infested area into several rectangular areas that satisfy the following conditions

  • Each rectangular area must be contained within the farm. It is allowed for the edges of a rectangle to overlap the boundary of the farm.
  • Each edge of a rectangular area is either horizontal or vertical.
  • Rectangular areas are completely separated from each other, including their boundaries.
  • Each pest infestation location must be contained within one of the rectangular areas. It is allowed for a pest infestation location to lie on an edge of a rectangle.

Figure C.1 (b) shows four rectangular areas covering all pest infestation locations. The farmer wants to minimize the number of rectangular areas for efficient pest management.

Given the boundary of a farm and the pest infestation locations, write a program to compute the minimum number of rectangular areas that satisfy the above conditions.

입력

Your program is to read from standard input. The input starts with a line containing two integers, mm (4≤m≤100,0004 ≤ m ≤ 100\\,000) and nn (0≤n≤100,0000 ≤ n ≤ 100\\,000), where mm is the number of edges of the farm field and nn is the number of the pest infestation locations. In the second line, mm integers v_1,v_2,⋯ ,v_mv\_1, v\_2, \cdots , v\_m (v_1=v_m=0v\_1 = v\_m = 0, 0≤v_i≤1060 ≤ v\_i ≤ 10^6) are given, which represent the xx-coordinates of the vertical edges and the yy-coordinates of the horizontal edges. These vertical and horizontal edges are met alternately when traversing the upper boundary of the farm field clockwise from the left end of the base edge to the right end. From the third line, each of the nn lines has two integers xx and yy, representing the coordinate (x,y)(x, y) of a pest infestation location. All locations are on the boundary or in the interior of the farm field.

출력

Your program is to write to standard output. Print exactly one line. The line should contain an integer representing the minimum number of rectangular areas that satisfy the above conditions.

예제3

  1. 예제 1

    입력
    12 8
    0 30 20 20 30 40 40 10 50 20 70 0
    4 5
    15 26
    25 15
    35 15
    35 35
    50 5
    55 15
    60 20
    
    예상 출력
    4
    
  2. 예제 2

    입력
    4 0
    0 10 50 0
    
    예상 출력
    0
    
  3. 예제 3

    입력
    12 3
    0 3 2 6 4 1 6 4 8 2 10 0
    3 5
    7 3
    3 1
    
    예상 출력
    2