There are several activities, the first start time and end time is [Si,fi], there is only one classroom, the activities can not overlap between the maximum number of activities to arrange?
input
Line 1th: 1 number N, number of segments (2 <= n <= 10000) 2-n + 1 lines: 2 numbers per line, start and end of line segments ( -10^9 <= s,e <= 10^9)
Output
Outputs the maximum number of segments that can be selected.
Input Example
31 52 33 6
Sample Output
2
We can know that the first scheduled activities can be arranged more. The first is to sort all the activity end times in order, and then retrieve the end time as a clue to determine whether the start time is earlier than the end time of the previous activity. Here you can use a struct or two array to relate the start time and end time of an activity.
#include <stdio.h>
#include <iostream>
#define MAX 10001
using namespace Std;
int main () {
int n,i,j,temps,tempo;
int Start[max],over[max];
while (scanf ("%d", &n)!=eof) {
int sum=0,t=-1000000000;
for (i=0;i<n;i++) {
cin>>start[i]>>over[i];
}
for (i=0;i<n;i++)
for (j=i;j<n;j++) {
if (Over[i]>over[j]) {
Tempo=over[i];
OVER[I]=OVER[J];
Over[j]=tempo;
Temps=start[i];
START[I]=START[J];
Start[j]=temps;
}
}
for (i=0;i<n;i++) {
if (T<=start[i]) {
T=over[i];
Sum+=1;
}
}
printf ("%d\n", sum);
}
return 0;
}
Originally I also considered the activity time can not be negative, but in the submission of the data given by the system to the negative number also to forget to go in ...
51nod Greedy Algorithm Introduction-----activity scheduling issues