50030. Activity Selection (special judge)
難度:4/5
有Hint的話就不會太難。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include "activity.h"
#include <stdio.h>
#include <stdlib.h>
// typedef struct activity {
// int start;
// int end;
// } Activity;
// #define PRINT
int cmp(const void* a, const void* b){
Activity* act_a = (Activity*)a;
Activity* act_b = (Activity*)b;
if(act_a->end > act_b->end) return 1;
else if(act_a->end < act_b->end) return -1;
else{
return act_a->start - act_b->start;
}
}
int select(Activity activities[], int n){
qsort(activities, n, sizeof(Activity), cmp);
int cnt = 0;
int now_time = -1;
for(int i = 0; i < n; i++){
if(activities[i].start >= now_time){
cnt += 1;
now_time = activities[i].end;
#ifdef PRINT
printf("%d %d\n", activities[i].start, activities[i].end);
#endif
}
}
return cnt;
}