Product Delivery

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

요약
한 번의 배달은 0번 도시에서 출발해 지나는 가게마다 감소하지 않는 수량을 공급한다. 모든 가게 i가 l_i개 이상 m_i개 이하를 받도록 하는 최소 배달 횟수를 구한다.
난이도

어려움10점 중 8점

유형
그리디, 배열, 이분 탐색
정답자
아직 제출이 없습니다

문제

There is only one railway line connecting (n+1)(n + 1) cities developed along the coastline. When cities along the coast are sequentially identified by numbers between 00 and nn, city (i−1)(i - 1) and city ii (1≤i≤n1 ≤ i ≤ n) are connected by rail, but other cities are not connected by rail.

Since every city except city 00 is famous as a tourist destination, every city ii (1≤i≤n1 ≤ i ≤ n) excluding city 00 is preparing a variety of goods to welcome travelers ahead of the tourist season. Worldwide famous goods BFB is the most popular item in every city. However, the supplier of this product is located in city 00.

There is only one store that sells BFB in each city ii (1≤i≤n1 ≤ i ≤ n). Let S_iS\_i be the BFB specialty store in city ii. In each S_iS\_i, the number of BFBs expected to be sold in the tourist season is analyzed and reported to the supplier in the form of \[l_i,m_i]\[l\_i, m\_i]. Here, l_il\_i and m_im\_i represent the minimum and the maximum number of expected required products, respectively.

The BFB supply company in city 00 collects request information from stores in every city and supplies products according to the rules described below.

  • Select a city, say city kk (1≤k≤n1 ≤ k ≤ n). Then, take a train departing from city 00, travel to city kk, and supply BFBs only to the stores along the route. In other words, the BFB supplier supplies products to S_1,S_2,…,S_kS\_1, S\_2, \dots , S\_k.
  • Let c_ic\_i be the number of BFBs supplied to S_iS\_i (1≤i≤k1 ≤ i ≤ k) while moving along the route, the condition c_i≤c_i+1c\_i ≤ c\_{i+1} (1≤i≤k−11 ≤ i ≤ k - 1) must be satisfied.

If the supplier supplies products according to the supply rules described above, it may be impossible for every store to supply the desired number of items with a single supply procedure. Therefore, the supplier must go through several supply procedures to deliver the products but must comply with the supply rules described above each time. After completing all supply procedures, each S_iS\_i will have at least l_il\_i and at most m_im\_i items.

For example, suppose n=4n = 4 and the number of items required by each store S_iS\_i (1≤i≤41 ≤ i ≤ 4) are \[13,15]\[13,15], \[5,8]\[5,8], \[6,14]\[6,14], and \[3,7]\[3,7], respectively. In order for each store to supply the desired quantity of goods, there must be at least two delivery procedures. In the first delivery procedure, 66 items can be supplied to each of the 44 stores. Once delivery is completed in this first procedure, all stores' requests except S_1S\_1 are satisfied. Since 66 items have already been delivered to S_1S\_1, rr (7≤r≤97 ≤ r ≤ 9) additional products will be delivered to S_1S\_1 in the second delivery procedure. Of course, there may be other delivery methods. However, at least two delivery procedures are required.

Write a program to calculate the minimum number of supply procedures in order to supply the number of BFBs required by each store according to the above rules.

입력

Your program is to read from standard input. The input starts with a line containing an integer nn (1≤n≤1061 ≤ n ≤ 10^6), where nn is the number of cities in which the BFB specialty stores locate. In the following nn lines, the ii-th line contains two integers l_il\_i and m_im\_i (1≤l_i≤m_i≤1091 ≤ l\_i ≤ m\_i ≤ 10^9) which indicate the minimum and the maximum number of expected required products by S_iS\_i.

출력

Your program is to write to standard output. Print exactly one line. The line should contain the minimum number of supply processes in order to supply the number of products required by each store according to the delivery rules.

예제3

  1. 예제 1

    입력
    4
    13 15
    5 8
    6 14
    3 7
    
    예상 출력
    2
    
  2. 예제 2

    입력
    5
    1 2
    2 3
    33 44
    4 5
    6 7
    
    예상 출력
    2
    
  3. 예제 3

    입력
    5
    10 20
    3 6
    13 30
    7 8
    11 13
    
    예상 출력
    3