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;
}
对于贪心,是要先做出一个决策,来确定解答树。

浙公网安备 33010602011771号