Problem:
Activity Selection Problem

There are $n$ acitivities with start times $\{s_i\}$ and finish times $\{t_i\}$. Assuming that a person can only work on a single activity at a time, find the maximum number of activities that can be performed by a single person.

Input

$n$
$s_1$ $t_1$
$s_2$ $t_2$
:
$s_n$ $t_n$

The first line consists of the integer $n$. In the following $n$ lines, the start time $s_i$ and the finish time $t_i$ of the activity $i$ are given.

出力

Print the maximum number of activities in a line.

Constraints

$1 \le n \le 10^5$

$1 \le s_i \lt t_i \le 10^9 (1 \le i \le n)$

Sample Input 1

5
1 2
3 9
3 5
5 9
6 8

Sample Output 1

3

Sample Input 2

3
1 5
3 4
2 5

Sample Output 2

1

Sample Input 3

3
1 2
2 3
3 4

Sample Output 3

2

Solution in C (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>

typedef struct timeAct{
    int Tfirst;
    int Tend;
}timeAct;

timeAct med3(timeAct x, timeAct y, timeAct z)
{
    if( x.Tend < y.Tend )
    {
    	if( y.Tend < z.Tend )
    	{
    		return y;
    	}
    	else if( z.Tend < x.Tend )
    	{
    		return x;
    	}
    	else
    	{
    		return z;
    	}
    }
    else
    {
    	if( z.Tend < y.Tend )
    	{
    		return y;
    	}
    	else if( x.Tend < z.Tend )
    	{
    		return x;
    	}
    	else
    	{
    		return z;
    	}
    }
}
 
void quickSort(timeAct *A,int p, int r)
{
    if(p < r)
    {
    	int i=p, j=r;
    	timeAct tmp, pibot = med3( A[i], A[i+(j-i)/2], A[j] );
    	while(1)
    	{
    		while( A[i].Tend < pibot.Tend ) i++;
    		while( pibot.Tend < A[j].Tend ) j--;
    		if( i >= j ) break;

    		tmp = A[i];
    		A[i] = A[j];
    		A[j] = tmp;
    		i++;
    		j--;
    	}
        quickSort(A, p, i - 1);
        quickSort(A, j + 1, r);
    }
}

int main()
{
    int i,n,cnt;
    int end=0;

    scanf("%d",&n);
    timeAct *ST = malloc(sizeof(timeAct)*n);

    /* データ読みとり */
    for( i=0 ; i<n ; i++ )
    {
        scanf("%d%d",&ST[i].Tfirst,&ST[i].Tend);
    }
//    for(i=0;i<n;i++) printf("%d %d\n",ST[i].Tfirst,ST[i].Tend);
//    printf("\n\n");

    quickSort(ST,0,n-1);

//    for(i=0;i<n;i++) printf("%d %d\n",ST[i].Tfirst,ST[i].Tend);
    end = ST[0].Tend;
    cnt = 1;
    for(i=1;i<n;i++)
    {
        if( ST[i].Tfirst > end )
        {
            cnt++;
            end = ST[i].Tend;
        }
    }
    printf("%d\n",cnt);
    return 0;
}