This page is still under construction.

Parts of this page are still being built. What you see may change.

Postering

Time limit1sMemory limit128 MB

Summary
Given adjacent buildings with widths and heights, find the minimum number of non-overlapping rectangles needed to cover the whole skyline shape.
Level

Medium7 of 10

Topics
Stack, Greedy
Solved
No attempts yet

Problem

All the buildings in the eastern district of Byteburg were built in the old style: they stand right next to one another with no gaps in between. Together they form a very long chain of buildings of varying heights, stretching from east to west.

The mayor of Byteburg, Byteasar, has decided to cover the northern face of the chain with posters. He wonders about the smallest number of posters that suffices to cover the whole northern face. Each poster is a rectangle with vertical and horizontal sides. Posters may not overlap, but they may touch, that is, share points along their edges. Every poster must lie flush against the walls of some buildings, and the entire surface of the northern face must be covered.

Write a program that:

  • reads the description of the buildings from standard input,
  • determines the minimum number of posters needed to cover their northern faces completely,
  • writes the result to standard output.

Input

The first line contains one integer nn (1≤n≤250 0001 \le n \le 250\,000), the number of buildings in the chain. Each of the next nn lines contains two integers did_i and wiw_i (1≤di,wi≤1091 \le d_i, w_i \le 10^9), separated by a single space, giving respectively the width and the height of the ii-th building in the row.

Output

Output a single integer: the minimum number of rectangular posters that suffice to cover the northern faces of the buildings.

Hint

The figures below illustrate an example. The first shows the northern face of a chain of buildings, and the second shows one way to cover that face with four posters.

Examples1

  1. Example 1

    Input
    5
    1 2
    1 3
    2 2
    2 5
    1 4
    
    Expected output
    4