zoj-3721(贪心)

来源:互联网 发布:天策雪河军爷捏脸数据 编辑:程序博客网 时间:2024/06/16 09:12

In Zhejiang University, there are N different courses labeled from 1 toN. 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 andt 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 theith 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

题目题意:题目会给我们n组时间区间(左闭右开),表示有考试,题目说,考试可以同一天,为了尽可能的减少考试天数,求最小的考试天数(能够分配所有的考试)

题目分析:为了使天数最小,也就是让尽可能多的考试,分配在同一天,我们按照考试的开始时间从小到大排序,开始时间相同的,按照时间结束短的优先,然后从第二开始,与一个比较,如果在范围内,就可以在同一天,而且(注意:缩小范围) ,也就是说,能够在同一天考试,那么就要保证所有的考试的时间区间有公共子区间,这里错了很多遍,

所以要缩小范围。

为了让方便理解(其实是我自己表达的不好),我打个比方:

考试1为 [1 ,5) 与考试二[2,6) 可以在同一天举行,那么他们和在一起的新区间为[2,5),考试三为[3,4),也有公共区间,那么它们三和在一起,新区间为[3,4),考试四为[4,5)它就不能在一起考了。

所以我们就要不断更新公共区间,之到没有了,就在下一天举行。

代码如下:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;const int maxn=1e5+10;struct note{    int l, r;    int flag;//考试的序号}p[maxn];vector<int> vec[maxn];//保存每一天的考试bool cmp(struct note a,struct note b)//排序规则{    if (a.l==b.l)        return a.r<b.r;    return a.l<b.l;}int main(){    int n;    while (scanf("%d",&n)!=EOF) {        for (int i=1;i<=n;i++) {            scanf("%d%d",&p[i].l,&p[i].r);            p[i].flag=i;        }        sort (p+1,p+n+1,cmp);        int res=1;        vec[res].push_back(p[1].flag);//把第一天丢进出        int maxl=p[1].l,maxr=p[1].r;        for (int i=2;i<=n;i++) {            if (p[i].l<maxr) {//在第一天范围内,                vec[res].push_back(p[i].flag);                if (p[i].r<maxr)//更新区间范围                    maxr=p[i].r;            }            else {                maxl=p[i].l;//不在,另起一个区间,新的一天                maxr=p[i].r;                res++;//天数++                vec[res].push_back(p[i].flag);            }        }        for (int i=1;i<=res;i++)            sort(vec[i].begin(),vec[i].end());//保证答案,升序        printf("%d\n",res);        for (int i=1;i<=res;i++) {            for (int j=0;j<vec[i].size();j++) {                printf("%d",vec[i][j]);                if (j==vec[i].size()-1) printf("\n");                else printf(" ");            }            vec[i].clear();        }        printf("\n");    }    return 0;}




















































原创粉丝点击