ZOJ3721 Final Exam Arrangement(贪心(区间选点))

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3721


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

题意:现在有n门课程,上课时间为一个左闭右开的区间。如果区间有重叠部分,说明没有学生同时上这两门课,那么这两门课的考试就可以在同一天进行。求出最短需要几天才可以考完所有课程。


很容易发现,考试所需要的天数就是没有重叠部分的区间的个数。也就可以看作是区间选点问题。

区间选点:数轴上有n个区间[ai,bi),要选择尽量少的点,使得每个区间内都至少有一个点。

思路:将区间按照右端点从小到大排序,然后对于每一个区间,如果当前区间的左端点小于ri(已经选择的区间的最右端),则跳过这个区间(本题中则将其加入当天的考试科目),否则则天数+1,并且更新ri为当前区间的右端点。

最后输出每一天的考试科目即可。


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
using namespace std;

struct sett
{
	int x,y,id;
	sett(){}
	sett(int _x,int _y)
	{
		x=_x;y=_y;
	}
	bool operator < (const sett &b) const
	{
		return y<b.y||y==b.y&&x<b.x;
	}
}s[100005];

int n;
vector <int> ans[100005];

int  main()
{
	while (scanf("%d",&n)!=EOF)
	{
		for (int i=0;i<n;i++)
		{
			scanf("%d%d",&s[i].x,&s[i].y);
			s[i].id=i+1;
		}
		sort(s,s+n);//按照右端点从小到大排序
		for (int i=0;i<=n;i++) ans[i].clear();
		int i=0,ri=0,day=0;
		while (i<n)//进行区间选点的操作
		{
			if (s[i].x<ri) {ans[day].push_back(s[i].id);++i;continue;}
			++day;ri=s[i].y;ans[day].push_back(s[i].id);++i;
		}
		printf("%d\n",day);
		for (int i=1;i<=day;++i)//按照格式输出
		{
			printf("%d",ans[i][0]);
			for (int j=1;j<ans[i].size();j++)
			{
				printf(" %d",ans[i][j]);
			}
			printf("\n");
		}
		printf("\n");
	}
	return 0;
}
			


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值