Zoj 3721 final exam arrangement [greedy]

Source: Internet
Author: User

Question: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3721.

Source: http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 26644 # Problem/F

Final exam arrangement

Time Limit: 4 seconds memory limit: 65536 kb Special Judge

In Zhejiang University, there areNDifferent Courses labeled from 1N. Each course has its own time slot during the week. We can represent the time
Slot of a course by an left-closed right-open interval[S, T).

Now we are going to arrange the final exam time of all the courses.

The final exam period will contain in multiple days. In each day, multiple final exams will be held simultaneously. If two courses 'time slots are not overlapped, there may be students who
Are attending both of them, so we cannot arrange their final exams at the same day.

Now you're to arrange the final exam period, to make the total days as small as possible.

Input

There are multiple test cases separated by blank lines.

For each keys, the 1st line contains one integerN(1 <=N<= 100000 ).

ThenNLines,I + 1Th line containsSAndTOf the Interval[S, T)ForITh course. (0 <= S <t <= 231-1)

There is a blank line after each test case.

Output

For each case, the 1st line contains the daysPIn the shortest final exam period.

NextPLines,I + 1Th line contains the numbers of courses whose final exam is arranged onITh day separated by one space.

Output a blank line after each test case.

Sample Input

40 11 22 33 440 21 32 43 540 41 52 43 6

Sample output

4123421 23 411 2 3 4

Q: It's strange. Why can I take the same test on the same day? Even though it's AC, I still don't understand it.
The question means that the question is sorted according to the question requirements during the competition. (In fact, one is enough.
)

Train of Thought: first sort by Start Time and end time before and after.

Of course, it is the beginning, the beginning, and the end.

The first course after sorting is of course the first day of the exam.

Next, we will traverse each of the following courses in sequence. If the next course has an intersection interval with the previous one, they will take the same day's exam. At this time, pay attention to narrowing down the exam range for this day, end early and take the intersection part. If there is no intersection, the interval is still the start time and end time, but the test time is postponed for one day.

Finally, I sorted the output according to the exam time and adjusted the output format.

F Accepted 1744 KB 1210 MS C ++ (G ++ 4.4.5) 1206 B 2013-07-20 12:19:25

#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int maxn = 100000+10;struct Class{int s,t;int flag;int index;}c[maxn];bool cmp(Class a, Class b){if(a.s != b.s) return a.s < b.s;else return a.t < b.t;}bool cmp1(Class a, Class b){if(a.flag == b.flag) return a.index <= b.index;else return a.flag < b.flag;}int main(){int n;while(scanf("%d", &n) != EOF){for(int i = 0; i < n; i++){scanf("%d%d", &c[i].s, &c[i].t);c[i].flag = 0;c[i].index = i+1;}sort(c,c+n,cmp);         c[0].flag=1;       for(int i = 1; i < n; i++){if(c[i].s < c[i-1].t){c[i].flag = c[i-1].flag;c[i].s = max(c[i].s, c[i-1].s);c[i].t = min(c[i].t, c[i-1].t);}else c[i].flag = c[i-1].flag+1;}int day = c[n-1].flag;int d = 1;sort(c,c+n,cmp1);printf("%d\n",day);int f = 1;for(int i = 0; i < n; i++){if(c[i].flag == d  ) {if(f == 1) { f = 2; printf("%d", c[i].index); }else {  printf(" %d", c[i].index);}}else{printf("\n");d += 1;printf("%d", c[i].index);}}printf("\n");}return 0;}

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.