博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【算法概论】贪心算法:区间划分问题
阅读量:4176 次
发布时间:2019-05-26

本文共 2153 字,大约阅读时间需要 7 分钟。

区间划分问题Interval Partitioning

问题描述:

       Lecture j starts at sj and finishes at fj

       Goal:find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room.

       下面给出一个例子?:

❗算法描述❗:

       定义 depth 为 在一段时间内,需要的最多教室数。

       (Def:The depth of a set of intervals is the maximum number that pass over and single point on the time-line.)

       如:在上面所举的例子中,depth = 3。

       按讲座的开始时间,对讲座进行排序。从开始时间最早的讲座开始遍历,如果下一个讲座与当前讲座兼容,继续;如果不兼容,另外开辟一个教室。

       伪代码如下:

Sort intervals by starting time so that s[1] ≤ s[2] ≤ ... ≤ s[n] .d = 1    //number of allocated classroomsfor j = 1 to n    if (lecture j is compatible with some classroom k)        schedule lecture j in classroom k    else        allocate a new classroom d + 1        schedule lecture j in classroom d + 1        d = d + 1

       实现该算法,复杂度为O(n * log n)。

       ① 对于每一个教室 k ,要记录在该教室进行的最后一个讲座的结束时间,以确定下一个讲座是否可以在该教室中举办;

       ② 当开辟新教室后,再对下一个讲座进行兼容性检查时,就需要与depth个教室进行比较,选择兼容性最好的教室。所以,开辟新教室时,应该把该教室的使用结束时间也加入到一个队列中;对一个教室进行再次添加讲座时,也应该把该教室的使用结束时间更新!

       在该算法中,会用到  a priority queue,优先队列要用到堆结构实现但我们可以使用C++标准库。(上学期数据结构学的“堆”也要复习了!)

代码✍:

#include 
#include
#include
#include
using namespace std;struct LECTURE{ double start; double finish;};bool comp(const LECTURE &a, const LECTURE &b);//定义比较结构struct cmp{ bool operator ()(double &a, double &b) { return a > b;//最小值优先 }};int main(){ int num; cout << "请输入讲座的数目:" << endl; cin >> num; vector
lectures(num); cout << "请依次输入讲座的开始时间、结束时间:" << endl; for (int i = 0; i < num; ++i) { cin >> lectures[i].start >> lectures[i].finish; } //按讲座开始时间对讲座进行排序 sort(lectures.begin(), lectures.end(), comp); priority_queue
, cmp> classrooms; //记录教室的使用结束时间,最小级优先 classrooms.push(0); //初始化一个教室 for (int i = 0; i < num; ++i) { //如果讲座的开始时间早于教室的最早使用结束时间,开辟一个新教室 if (lectures[i].start < classrooms.top()) { classrooms.push(lectures[i].finish); //将新教室的使用结束时间保存 } else { //将教室的最早结束时间替换成该讲座的结束时间 classrooms.pop(); classrooms.push(lectures[i].finish); } } cout << "需要教室的最大数目:" << endl; cout << classrooms.size() << endl; return 0;}bool comp(const LECTURE &a, const LECTURE &b){ return a.start < b.start;}

测试:

我把12:30这种都写成了12.3

 

转载地址:http://uxtai.baihongyu.com/

你可能感兴趣的文章
Docker容器实例通过非默认的网络命名空间访问外部网络
查看>>
RHEL/CentOS 7的FirewallD及其firewall-cmd命令概述
查看>>
RHEL/CentOS 7的kernel tunables及其sysctl命令概述
查看>>
RHEL/CentOS 7中Nginx的systemd service
查看>>
RHEL/CentOS 7的systemd及其systemctl命令概述
查看>>
RHEL/CentOS 7的systemd target及其中的multi-user.target
查看>>
RHEL/CentOS 7中通过systemd service持久化网络命名空间
查看>>
Docker daemon及容器实例的DNS配置详解
查看>>
RHEL/CentOS 7中的网络暨network.service与NetworkManager.service详解
查看>>
RHEL/CentOS 7中的iptables.service与firewalld.service概述
查看>>
RHEL/CentOS 7中的SELinux概述
查看>>
SELinux的运行模式与安全策略详解
查看>>
Linux资源的SELinux context详解
查看>>
SELinux检查与Nginx的反向代理的典型错配案例及解决
查看>>
SELinux检查与Apache HTTP服务器的文件访问典型错配案例及解决
查看>>
Docker容器启动时的第一个进程的设置总结
查看>>
eclipse遇到的第一个问题
查看>>
Java中的内存管理(栈和堆、方法区)
查看>>
二进制 转换方法
查看>>
数组相关(定义、访问、遍历、复制/扩容、排序)
查看>>