今年暑假不AC HDU - 2037 (水題-簡單貪心)
阿新 • • 發佈:2019-01-02
簡單貪心:每次選擇結束時間最早的,因此先用sort函式排序,之後就簡單了
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>using namespace std;
typedef struct Time
{
int ts, te;
}Time;Time time[100];
bool cmp(Time a, Time b)
{
return a.te < b.te;
}int main()
{
int n;
while(scanf("%d", &n) && n != 0)
{
for(int i = 0; i < n; i++)
scanf("%d %d", &time[i].ts, &time[i].te);
sort(time, time+n, cmp);
int ans = 1;
int tmp = time[0].te;
for(int i = 1; i < n; i++)
if(time[i].ts >= tmp)
{
ans++;
tmp = time[i].te;
}
printf("%d\n", ans);
}
return 0;
}