您的位置:首页 > 其它

ZOJ 3721 Final Exam Arrangement(贪心)

2015-08-12 00:47 323 查看
Final Exam Arrangement

Time Limit: 4 Seconds Memory Limit: 65536 KB Special Judge

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
4
0 1
1 2
2 3
3 4

4
0 2
1 3
2 4
3 5

4
0 4
1 5
2 4
3 6

Sample Output
4
1
2
3
4

2
1 2
3 4

1
1 2 3 4



题意:有一些考试,已知开始时间和结束时间,左闭右开,如果考试时间有重叠,则可以安排在同一天。求最少要安排几天,以及方案。
题解:按左区间小排前面,左区间一样右区间小排前面,然后贪心。

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#define N 100010
using namespace std;
int n;
struct node {
    int l,r;
    int id;
} a
;

bool cmp(node a,node b) {
    return a.l<b.l||(a.l==b.l&&a.r<b.r);
}

vector<int>ans
;

int main() {
    //freopen("test.in","r",stdin);
    while(cin>>n) {
        for(int i=0; i<n; i++) {
            scanf("%d%d",&a[i].l,&a[i].r);
            a[i].id=i;
            ans[i].clear();
        }
        sort(a,a+n,cmp);
        int num=0;
        for(int i=0; i<n; i++) {
            int MinR=a[i].r;///***
            int j=i;
            while(j<n&&a[j].l<MinR) {
                ans[num].push_back(a[j].id+1);
                MinR=min(MinR,a[j].r);
                j++;
            }
            num++;
            i=j-1;
        }
        printf("%d\n",num);
        for(int i=0; i<num; i++) {
            for(int j=0; j<ans[i].size(); j++) {
                if(j==0)
                    printf("%d",ans[i][j]);
                else printf(" %d",ans[i][j]);
            }
            printf("\n");
        }
        puts("");
    }
    return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: