#P14093. [ICPC 2023 Seoul R] Product Delivery

[ICPC 2023 Seoul R] Product Delivery

题目描述

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 (i1)(i-1) and city ii (1in1\le i\le 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(1in1\le i\le 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(1in1\le i\le n). Let SiS_i be the BFB specialty store in city ii. In each SiS_i, the number of BFBs expected to be sold in the tourist season is analyzed and reported to the supplier in the form of [li,mi][l_i,m_i]. Here, lil_i and mim_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(1kn1\le k\le 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 S1,S2,,SkS_1,S_2,\dots,S_k.
  • Let cic_i be the number of BFBs supplied to SiS_i(1ik1\le i\le k) while moving along the route, the condition cici+1c_i\le c_{i+1}(1ik11\le i\le 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 SiS_i will have at least lil_i and at most mim_i items.

For example, suppose n=4n=4 and the number of items required by each store SiS_i(1i41\le i\le 4) are [13,15],[5,8],[6,14][13,15], [5,8],[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 S1S_1 are satisfied. Since 66 items have already been delivered to S1S_1, rr(7r97\le r\le 9) additional products will be delivered to S1S_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 (1n1061\le n\le 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 lil_i and mim_i (1limi1091\le l_i\le m_i\le 10^9) which indicate the minimum and the maximum number of expected required products by SiS_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.

4
13 15
5 8
6 14
3 7
2
5
1 2
2 3
33 44
4 5
6 7
2
5
10 20
3 6
13 30
7 8
11 13
3