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

#CT0000147. Lịch sử dụng phòng (Room Scheduling)

    ID: 2558 Loại: Thông thường 2000ms 256MiB Tried: 3 Đã chấp nhận: 2 Độ khó: 1 Đăng bởi: Nhãn>Greedy AlgorithmsActivity selectionInterval schedulingSorting and SearchingSorting algorithmsExchange arguments

Bài tập này chưa được xếp vào lộ trình.

Lịch sử dụng phòng (Room Scheduling)

Room Scheduling

Version: Phuoc Hung OJ Extended

Problem Statement

A school has one computer room and receives nn requests to use it. Request ii uses the room from time sis_i until tit_i, where si<tis_i<t_i.

Each interval is half-open [si,ti)[s_i,t_i): the room becomes occupied at sis_i and is released at tit_i. Therefore a session may start at exactly the time when another ends.

The school may accept or reject each request, but no two accepted sessions may occupy the room at the same instant.

Find the maximum number of requests that can be accepted.

Input

The first line contains a positive integer nn.

Each of the next nn lines contains two integers sis_i and tit_i, the starting and ending times of request ii, respectively.

Output

Print one integer: the maximum number of requests that can be accepted without overlapping sessions.

Subtasks

  • Subtask 1 (30 points): 1≤n≤201\le n\le 20; 0≤si<ti≤1090\le s_i<t_i\le 10^9 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 0≤si<ti≤1090\le s_i<t_i\le 10^9 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (40 points): 1≤n≤2⋅1051\le n\le 2\cdot 10^5; 0≤si<ti≤1090\le s_i<t_i\le 10^9 for every 1≤i≤n1\le i\le n.

Examples

Example 1

Input

5
1 3
2 5
3 4
4 7
6 8

Output

3

Explanation

Accept [1,3)[1,3), [3,4)[3,4), and [4,7)[4,7). Their endpoints meet without overlap. The other two intervals overlap the selected schedule; a schedule with four sessions is impossible for these five requests. The answer is 33.

Example 2

Input

4
1 10
2 3
3 4
4 5

Output

3

Explanation

Accepting [1,10)[1,10) prevents all other requests. Reject it and accept [2,3)[2,3), [3,4)[3,4), and [4,5)[4,5) instead, obtaining 33 sessions. These are all three remaining requests, so no better schedule exists.

Example 3

Input

3
1 2
2 3
3 4

Output

3

Explanation

The intervals [1,2)[1,2), [2,3)[2,3), and [3,4)[3,4) run consecutively. An ending time may equal the next starting time, so all 33 requests can be accepted.