hdu 2037(贪心)

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
using namespace std;

typedef struct Channel{
  int s,t;
}channel;


int cmp(const void *a, const void *b)
{
    channel *c = (channel *) a;
    channel *d = (channel *) b;
    if(c->t != d->t) return c->t - d->t;
    else return c->s - d->s;
}
//qsort

/*
int cmp(const channel a, const channel b)
{
    if(a.t != b.t) return a.t < b.t;
    else return a.s < b.s;
}
//sort
*/
int main()
{
    int n;
    channel x[105];
    while(scanf("%d", &n) != EOF){
        if(n == 0)  break;
        for(int i = 0; i < n; i ++)
            scanf("%d%d", &x[i].s, &x[i].t);
        qsort(x, n, sizeof(channel), cmp);
       // sort(x, x+n, cmp);
        int count = 1;
        for(int i = 1, j = 0; i < n; i ++){
            if(x[j].t <= x[i].s ){
                count ++;
                j = i;
            }
        }
        cout << count << endl;
    }
    return 0;
}

  对于贪心,是要先做出一个决策,来确定解答树。

posted @ 2013-10-20 15:57  i梦  阅读(172)  评论(0)    收藏  举报