1. 程式人生 > >hdu 2037(最簡單的貪心演算法)

hdu 2037(最簡單的貪心演算法)

#include<iostream>
using namespace std;
struct tv
{
    int s;
    int e;
}a[105];
int cmp(const void*a,const void*b){
    if (((tv*)a)->e == ((tv*)b)->e)
        return ((tv*)a)->s - ((tv*)b)->s;
    return ((tv*)a)->e - ((tv*)b)->e;
}
int n;
int main(){
    while (cin>>n&&n)
    {
        for
(int i = 0; i < n; i++){ cin >> a[i].s >> a[i].e; } qsort(a, n, sizeof(a[1]), cmp); int num = 0, l = 0; for (int i = 0; i < n; i++){ if (l <= a[i].s){ num++; l = a[i].e; } } cout << num << endl; } }