按 \(f_{i}\) 从小到大排序,结束时间越早后面能选的活动越多
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 |
#include<stdio.h> #include<algorithm> using namespace std; struct Activity { int a, b; bool operator < (const Activity &A) const { if (b != A.b) return b < A.b; return a > A.a; } } a[1010]; int n, tot, time; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i].a, &a[i].b); sort(a+1, a+n+1); for (int i = 1; i <= n; ++i) { if (time <= a[i].a) { time = a[i].b; tot++; } } printf("%d", tot); return 0; } |