zoj 3721 Final Exam Arrangement【贪心】
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3721
来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26644#problem/F
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
题意:感觉很奇怪,为什么重合的可以放在同一天考试,虽然AC了,但是还是没有看懂
题目意思,比赛时按照题目要求弄了下,两个排序一下就过了。(其实一个就够
了)
思路:先按照开始的时间和结束的时间从前往后排序。
当然是先开始的 排在前面,同时开始的,先结束的排在前面了
排序好后的第一门课当然是在第一天考试了。
然后依次遍历后面的每一门课,如果后面一门课和前面的有相交的区间,则它们在同一天考试,此时要注意缩小这天的可以考试的区间,后开始早结束,取相交的部分。如果没有相交的部分,那么区间还是自己的开始和结束时间,只是考试时间推后了一天。
最后是输出,我是按照考试时间排序了下,再调整了下输出格式。
F | Accepted | 1744 KB | 1210 ms | C++ (g++ 4.4.5) | 1206 B | 2013-07-20 12:19:25 |
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std; const int maxn = 100000+10;
struct Class{
int s,t;
int flag;
int index;
}c[maxn]; bool cmp(Class a, Class b)
{
if(a.s != b.s) return a.s < b.s;
else return a.t < b.t;
} bool cmp1(Class a, Class b)
{
if(a.flag == b.flag) return a.index <= b.index; else return a.flag < b.flag;
} int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
for(int i = 0; i < n; i++)
{
scanf("%d%d", &c[i].s, &c[i].t);
c[i].flag = 0;
c[i].index = i+1;
}
sort(c,c+n,cmp);
c[0].flag=1;
for(int i = 1; i < n; i++)
{
if(c[i].s < c[i-1].t)
{
c[i].flag = c[i-1].flag;
c[i].s = max(c[i].s, c[i-1].s);
c[i].t = min(c[i].t, c[i-1].t);
}
else c[i].flag = c[i-1].flag+1;
}
int day = c[n-1].flag; int d = 1;
sort(c,c+n,cmp1);
printf("%d\n",day); int f = 1; for(int i = 0; i < n; i++)
{ if(c[i].flag == d ) {
if(f == 1) { f = 2; printf("%d", c[i].index); }
else {
printf(" %d", c[i].index);
}
} else{
printf("\n");
d += 1; printf("%d", c[i].index);
}
}
printf("\n");
}
return 0;
}
zoj 3721 Final Exam Arrangement【贪心】的更多相关文章
- ZOJ-3721 Final Exam Arrangement 贪心
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3721 容易的贪心题,排个序.. //STATUS:C++_AC_ ...
- Final Exam Arrangement(ZOJ)
In Zhejiang University, there are N different courses labeled from 1 to N. Each course has its own t ...
- [CF] Final Exam Arrangement
问题链接:http://www.bnuoj.com/v3/contest_show.php?cid=4329#problem/F 问题大意: 就是有1--N们课程,每一个课程都有一 ...
- 【HDOJ6651】Final Exam(贪心)
题意:有n门课,价值之和为m,每门课的价值可能是0到m 一门价值为x的课需要花至少x+1时间准备才能通过 问不管价值如何分配都能通过至少k门课的最小总准备时间 m,n,k<=1e9 思路: #i ...
- HDU 6651 Final Exam (思维)
2019 杭电多校 7 1006 题目链接:HDU 6651 比赛链接:2019 Multi-University Training Contest 7 Problem Description Fin ...
- 2019 Multi-University Training Contest 7 Kejin Player Final Exam
Kejin Player 期望DP 题意: 初始等级为1,每一级有四个参数 r , s , x , a . 每一级有一个概率p=r/s花费a的代价升级到下一级,失败可能会倒退到x级 设从 l 到 r ...
- 2019HDU多校第七场 HDU6651 Final Exam
一.题目 Final Exam 二.分析 题目说的比较绕,总之一定要记住,$n$个题目都可以做,至少作对$k$到,但是做题目的人不知道每道题对应的分数. 作为出题人,如果他是田忌,肯定不会去在做题目的 ...
- Exam(贪心)
Exam Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 5240 Exam(贪心)
Exam Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- 初识Nginx及编译安装Nginx
初识Nginx及编译安装Nginx 环境说明: 系统版本 CentOS 6.9 x86_64 软件版本 nginx-1.12.2 1.什么是Nginx? 如果你听说或使用过Apache软件 ...
- 移动web之响应式布局
1.响应式布局的概念 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念.简而言之.就是一个站点可以兼容多个终端--而不是为每一个终端做一个特定的版本号. 这个概念是为解决移动互 ...
- JavaSE入门学习18:Java面向对象之多态
一Java多态 多态是同一个行为具有多个不同表现形式或形态的能力. 多态性是对象多种表现形式的体现.比方我们说"宠 物"这个对象.它就有非常多不同的表达或实现,比方有小猫.小狗.蜥 ...
- perl学习笔记——输入与输出
读取标准输入 用<STDIN>进行标准输入:chomp($line=<STDIN>); 如果读到文件尾,行输入操作符就会返回undef.便可利用这一性质跳出循环. while( ...
- 【转载】LR - 细节解析,为什么LR脚本会去访问“脚本中不存在的“资源?
问题描述 同事遇到的一个问题,LR执行性能测试脚本时,总报出错误,无法访问一个图片的地址,但脚本中明明没有对该资源的请求. Action4.c(12): Warning -27796: Failed ...
- Java学习从入门到精通(2) [转载]
Java Learning Path(二).书籍篇 学习一门新的知识,不可能指望只看一本,或者两本书就能够完全掌握.需要有一个循序渐进的阅读过程.我推荐Oreilly出版的Java系列书籍. 在这里我 ...
- linux系统下使用apt-get install 方法安装lamp环境
1.更新源,获得最近的软件包的列表,列表中包含一些包的信息,比如这个包是否更新过. sudo apt-get update 2.更新系统中已安装的软件包 sudo apt-get upgrade 3. ...
- 【SpringMVC学习08】SpringMVC中实现文件上传
之前有写过一篇struts2实现的文件上传,这一篇博文主要来总结下springmvc实现文件上传的步骤.首先来看一下单个文件的上传,然后再来总结下多个文件上传. 1. 环境准备 springmvc上传 ...
- Windows安装Redis的php扩展
Redis是一种常用的非关系型数据库,主要用作数据缓存,数据保存形式为key-value,键值相互映射.它的数据存储跟MySQL不同,它数据存储在内存之中,所以数据读取相对而言很快,用来做高并发非常不 ...
- C# 系统应用之注冊表使用具体解释
在平时做项目时,我们有时会遇到注冊表的操作,比如前面我们须要获取IE浏览器地址栏的信息.获取"我的电脑"地址栏输入的目录信息.USB近期使用信息等.注冊表项是注冊表的基本组织单位, ...