This page is still under construction.

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

Lake

Time limit1sMemory limit1024 MB

Summary
Given chords between distinct points on a circle, find the maximum number of chords that can be chosen so that no two chosen chords cross.
Level

Medium7 of 10

Topics
Dynamic programming, Intervals, Sorting, Geometry
Solved
No attempts yet

Problem

In southeastern Canada, on the border with the United States, lie the five well-known lakes called the Great Lakes. Now that the IOI is being held in Canada, several proposals have come up to run sightseeing boats on Lake Ontario, the lake closest to the venue.

Each sightseeing boat proposal connects two points on the shore of the lake, and there are N proposals in total. The i-th proposal is to run a sightseeing boat connecting point sis_i and point tit_i. Here point xx means the point reached by traveling a distance of xx meters counterclockwise along the shore from the eastern end of the lake. The circumference of the lake is 500,000 meters.

We want to carry out as many of these proposals as possible, but to avoid collisions between boats, two routes must not cross.

Given N boat proposals, write a program to find the maximum number of proposals that can be carried out.

Input

Read the following input from standard input.

  • The first line of the input contains the integer N. It represents the number of sightseeing boat proposals.
  • The i+1-th line of the input (1≤i≤N1 \le i \le N) contains two integers si,tis_i, t_i separated by a space. They represent the two points to be connected by the i-th proposal. The 2N2N values s1,…,sN,t1,…,tNs_1, \ldots, s_N, t_1, \ldots, t_N are all distinct.

Output

Print to standard output a single integer representing the maximum number of proposals that can be carried out among the given boat proposals.

Constraints

  • 1≤N≤2,0001 \le N \le 2,000 (number of proposals)
  • 0≤si<500,0000 \le s_i < 500,000, 0≤ti<500,0000 \le t_i < 500,000 (coordinates of points)

Hint

A figure showing the five proposals in the input example above (the spacing between points is not exact). If you choose the three proposals drawn with thick lines, the boats can be run without their routes crossing.

Examples1

  1. Example 1

    Input
    5
    50000 150000
    450000 100000
    200000 300000
    260000 350000
    0 230000
    
    Expected output
    3