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
Sample Output
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; }
相关文章推荐
- 编译cocos2dx 工程,ndk-build 报错:
- MyEclipse6.6 汉化过程
- 在Android上面如何使用带有心跳检测的Socket
- 搭建java开发环境
- POJ 1741 Tree(树分治|ltc男人八题)
- canvas阴影
- Codeforces 245G Suggested Friends 暴力乱搞
- 对sql的查询语句做成对象式,简单实现。Where部分
- php-实例1:简单的文章管理系统
- hadoop之家族
- 博弈题集
- java封装AES加密算法
- HDU - 1827 Summer Holiday(强连通分量+贪心)
- vsftpd 安装与使用
- iOS- JSon和Xml解析,与服务器交互数据的解析详解与使用,各种解析方式详解
- 【转载】设计之路:如何进行软件需求分析?
- SYN Cookie的原理和实现
- HDU 4279 Number(找规律)
- [C语言]指针数组和指针数组|函数指针和指针函数
- Scala深入浅出实战经典《第83讲:Scala中List的实现内幕源码揭秘》笔记