解题报告 『活动安排(贪心)』
第一次提交WA了两个点,因此特地写一篇博客。
错误见代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (register int i = (a); i <= (b); i++) const int maxn = 1e3 + 5; int n, ans = 0; struct node { int start, end; }work[maxn]; struct cmp { inline bool operator()(const node &a, const node &b) { return a.end < b.end; } }; int read() { int x = 0, flag = 0; char ch = ' '; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') { flag = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return flag ? -x : x; } void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } int main() { n = read(); rep(i, 1, n) work[i].start = read(), work[i].end = read(); sort(work + 1, work + n + 1, cmp()); int now = -1; rep(i, 1, n) { if (work[i].start > now) {//此处应改为work[i].start >= now.条件允许的情况下,一个活动结束后可以马上开始下一个活动. ans++; now = work[i].end; } } write(ans); return 0; }