【BNU Summer Training 2014.07.25】 Final Exam Arrangement (贪心)

来源:互联网 发布:缺少网络协议 编辑:程序博客网 时间:2024/04/27 13:25


In Zhejiang University, there are N different courses labeled from 1 to N. 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 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 ease, the 1st line contains one integer N(1<=N<=100000).

Then N lines, the i+1th line contains s and t of the interval [s, t) for the ith course.(0<=s<t<=231-1)

There is a blank line after each test case.

Output

For each case, the 1st line contains the days P in the shortest final exam period.

Next P lines, the i+1th line contains the numbers of courses whose final exam is arranged on the ith 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


思路:先按照开始的时间和结束的时间从前往后排序。

    当然是先开始的 排在前面,同时开始的,先结束的排在前面了

          排序好后的第一门课当然是在第一天考试了。

          然后依次遍历后面的每一门课,如果后面一门课和前面的有相交的区间,则它们在同一天考试,此时要注意缩小这天的可以考试的区间,后开始早结束,取相交的部分。如果没有相交的部分,那么区间还是自己的开始和结束时间,只是考试时间推后了一天。

代码:

#include <stdio.h>#include <string.h>#include <algorithm>#define len 100010using namespace std;struct p{    int begin,end,day,num;};bool cmp(p a,p b){    if(a.begin==b.begin)        return a.end<b.end;    return a.begin<b.begin;}p a[len];int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        for(int i=0; i<n; ++i)        {            scanf("%d%d",&a[i].begin,&a[i].end);            a[i].num=i+1;        }        sort(a,a+n,cmp);        a[0].day=1;        for(int i=1; i<n; ++i)        {            if(a[i].begin<a[i-1].end)            {                a[i].day=a[i-1].day;                if(a[i].end>a[i-1].end)                    a[i].end=a[i-1].end;            }            else                a[i].day=a[i-1].day+1;        }        printf("%d\n",a[n-1].day);        printf("%d",a[0].num);        for(int i=1;i<n;++i)        {            if(a[i].day==a[i-1].day)                printf(" %d",a[i].num);            else                printf("\n%d",a[i].num);        }        printf("\n\n");    }    //for(int i=1;i<=)    return 0;}



0 0
原创粉丝点击