51nod Greedy Algorithm Introduction-----activity scheduling issues

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.