Event Scheduling issues

Source: Internet
Author: User

#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<algorithm>using namespacestd;Const intMAXN =10000+1; typedefstruct{    intstart; intOver ;} Point;point P[MAXN];Const intcmpConstPoint A,ConstPoint B) {    returnA.over <=B.over;}intMain () {intN;  while(Cin >>N) {memset (P,0,sizeof(p));  for(inti =0; i<n;i++) Cin>> P[i].start >>P[i].over; Sort (p, p+N, CMP); intCNT =1;  for(inti =0; i<n;i++)        {             for(intj = i +1; j<n;j++)            {                if(P[i].over <=P[j].start) {i=J; CNT++; Continue; } }} cout<< CNT <<Endl; }    return 0;}
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?

Analysis: We just want to improve the utilization rate of classrooms and arrange activities as much as possible.
Consider several greedy strategies that are easy to think about:
(1) Start the earliest activities first, the goal is to end the activities early, let out the classroom.
However, this is obviously not possible, because the earliest activities can be very long, affecting our subsequent activities. For example, activity start and end times are [0, 100), [up], [2, 3], [3, 4], [4,5], schedule [0,100] After this activity, other activities cannot be scheduled, but the optimal solution is to arrange for 4 activities except for it.
(2) Short activities first, the goal is to empty the classroom as far as possible. But it is not difficult to construct the following counter example: [0,5] [5,10] [3, 7), here [3,7) the shortest, but if we arrange [3,7], the other two can not be arranged. But the optimal solution is obviously to arrange the other two, and give up [3,7], this greedy strategy can not be seen.

Finally, we provide input and output data, you write a program, the implementation of this algorithm, only write the correct program, to continue the course behind. 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

Event 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.