Prompt
Note: If the last activity ends at t time, the next activity should start at t + 1 at the earliest.
Algorithm analysis:
There are three kinds of algorithms that can be considered for scheduling without overlapping intervals:
1. In the optional work, the earliest end time is selected each time.
2. In the optional work, the shortest job is selected every time.
3. In an optional job, work that overlaps with the minimum number of optional jobs is selected each time.
Facts have proved that only one of them is correct and can withstand the test.
For the first algorithm, the final end time of the algorithm when the same number of jobs started earlier is not later than that of other schemes.
That is to say, the number after the first ending interval must be greater than or equal to the number of other later intervals.
For details, see: http://blog.csdn.net/luoweifu/article/details/18195607
The code is as follows:
# Include <iostream> # include <utility> # include <algorithm> using namespace std; typedef pair <int, int> name; name arr [10005]; int cmp (name, name B) {return. second <B. second;} int main () {int N, n, I, ans, t; cin> N; while (N --) {cin> n; for (I = 0; I <n; I ++) cin> arr [I]. first> arr [I]. second; sort (arr, arr + n, cmp); ans = 0, t =-1; for (I = 0; I <n; I ++) {if (t <arr [I]. first) {ans ++; t = arr [I]. second ;}} cout <ans <endl ;}return 0 ;}
Because sort sorts the pair type first by default .. I realized it only after WA.