PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000086. Số mũi tên ít nhất để làm vỡ bóng bay (Minimum Number of Arrows to Burst Balloons)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Số mũi tên ít nhất để làm vỡ bóng bay (Minimum Number of Arrows to Burst Balloons)

Minimum Number of Arrows to Burst Balloons

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn balloons, balloon ii spanning the closed horizontal interval [li,ri][l_i,r_i]. An arrow fired vertically at real coordinate xx bursts every balloon whose interval contains xx. Find the minimum number of arrows to burst all balloons.

Input

The first line contains nn. Each of the following nn lines contains integer endpoints li,ril_i,r_i.

Output

Print the minimum number of arrows.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; −2⋅109≤li≤ri≤2⋅109-2\cdot10^9\le l_i\le r_i\le2\cdot10^9.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; −2⋅109≤li≤ri≤2⋅109-2\cdot10^9\le l_i\le r_i\le2\cdot10^9.
  • Subtask 3 (50 points): 1≤n≤1051\le n\le10^5; −2⋅109≤li≤ri≤2⋅109-2\cdot10^9\le l_i\le r_i\le2\cdot10^9.

Examples

Input

4
10 16
2 8
1 6
7 12

Output

2

Explanation

An arrow at x=6x=6 bursts balloons [2,8][2,8] and [1,6][1,6]. Another at x=11x=11 bursts [10,16][10,16] and [7,12][7,12]. The first and third listed intervals are disjoint, so one arrow cannot suffice.