第1章 游戏之乐——高效率地安排见面会
2015-06-29 16:04
369 查看
高效率地安排见面会
1. 问题描述:
在校园招聘的季节里,为了能让学生们更好地了解微软亚洲研究院各研究组的情况,HR部门计划为每一个研究组举办一次见面会,让各个研究组的员工能跟学生相互了解和交流。已知有n位学生,他们分别对m个研究组中的若干个感兴趣。为了满足所有学生的要求,HR希望每个学生都能参加自己感兴趣的所有见面会。如果每个见面会的时间为t,那么,如何安排才能够使得所有见面会的总时间最短?最简单的办法,就是把m个研究组的见面会时间依次排开,那我们就要用m * t的总时间,我们有10多个研究小组,时间会拖得很长,能否进一步提高效率?
2. 分析与解法
【解法一】假设某一个同学同时对k个小组感兴趣, 那么这k个小组两两之间都要有一条边, 这样就转化成了最少着色问题. 图的最少着色问题至今没有有效解法, 可以使用dfs枚举, 时间复杂度很高。【解法二】我们可以尝试对这个图进行K着色, 首先把K设置成1, 看看有没有适合的方案, 再逐渐把K提高. 当假设待求的图最少着色数远小于图的定点数时, 这个算法的复杂度远低于【解法一】。
扩展问题一
假设有 N 个面试要同时进行, 他们的面试时间分别是 B[i], E[i]. 我们希望将这N个面试安排在若干地点, 不同的面试在同一时间不能在相同的面试点。如果你是微软亚洲研究院的HR,现在给定这N个面试的时间之后,你能计算出至少需要多少个面试点吗?比如下图所示的4个面试。【解法一】不过这个问题和原始问题有些不同,因为每个面试者对应于一个时间区间。由这些区间之间的约束关系转化得到的图,属于区间图。 我们可以通过贪心算法来解决。算法就是对所有的面试,按照 B[i] 进行排序,对当前区间 i 着色时,必须保证所着的颜色没有出现在这个区间之前且时间段与当前区间有重叠的区间用到。假设面试的总数为N,那么相应的代码如下:
package chapter1youxizhileMeeting; /** * 面试时间段类 * @author DELL * */ class Timeperiod{ public int b; //开始时间 public int e; //结束时间 public Timeperiod(int b, int e){ this.b = b; this.e = e; } }
package chapter1youxizhileMeeting; /** * 高效率的安排见面会 * 扩展问题一【解法一】贪心算法 * @author DELL * */ public class Meeting1 { private int n; //面试总数 private Timeperiod[] tp; //用于存放相应面试时间段的数组 //构造函数 public Meeting1(int n, Timeperiod[] tp){ this.n = n; this.tp = tp; } /** * 判断两个时间段是否重叠 * @param tp1 时间段1 * @param tp2 时间段2 * @return 判断结果 */ public boolean isOverlap(Timeperiod tp1, Timeperiod tp2){ if((tp1.e<=tp2.b)||(tp1.b>=tp2.e)){ return false; }else return true; } /** * 计算所需的最少颜色 * @return 最少颜色 */ public int getminColors(){ int minColors = 0; //所需最少颜色数 boolean isForbidden[]; //判断颜色是否被禁止 int color[]; //存放颜色编号 color = new int ; isForbidden = new boolean ; int i,j,k; for(i=0;i<n;i++){ for(j=0;j<minColors;j++){ //初始赋值都不禁止 isForbidden[j] = false; } for(k=0;k<i;k++){ if(isOverlap(tp[k],tp[i])) isForbidden[color[k]]=true; //如果有重叠就禁止 } for(j=0;j<minColors;j++){ if(!isForbidden[j]) //遍历已用的颜色,看是否都冲突 break; } if(j<minColors) //如果有不冲突的 color[i]=j; else color[i] = minColors++; //否则新加一种颜色 } return minColors; } public static void main(String[] args) { Timeperiod tp1 = new Timeperiod(1,5); Timeperiod tp2 = new Timeperiod(2,3); Timeperiod tp3 = new Timeperiod(3,4); Timeperiod tp4 = new Timeperiod(3,6); Timeperiod tp[]={tp1,tp2,tp3,tp4}; Meeting1 mt = new Meeting1(4,tp); System.out.println("所需的最少颜色数为:"+mt.getminColors()); } }
程序运行结果如下:
所需的最少颜色数为:3
通过简单分析,我们可以知道这个算法的时间复杂度为O(N2)。
【解法二】有一个更简单的思路,先对面试数组排序,然后遍历面试数组,每见到一个 B(开始),color +1,并维护全局最大 color 数,每遇到一个对应的 E,color 数减一。返回全局最大 color 值。要注意的是:排序时要用到双关键字比较,当两个值相等时,属于时间段开始的一定要排在属于时间段结束的后面,只有这样才能保证结果的正确性。相应代码如下:
package chapter1youxizhileMeeting; /** * 高效率的安排见面会 * 扩展问题一【解法二】 * @author DELL * */ public class Meeting2 { private int n; //面试总数 private Timeperiod[] tp; //用于存放相应面试时间段的数组 private Tarr[] ta; //由所有时间段的时间组成的数组 //构造函数 public Meeting2(int n, Timeperiod[] tp){ this.n = n; this.tp = tp; ta = new Tarr[2*n]; } //时间数据结构 class Tarr { public int time; //时间 public char type; //类型,‘B'表示开始,'E'表示结束 public int tpi; //改数据在tp数组中的位置 //构造函数 public Tarr(int time, char type, int tpi){ this.time = time; this.type = type; this.tpi = tpi; } } /** * 对时间数组进行排序 */ private void insertSort(){ int i,j; Tarr temp; for(i=1;i<ta.length;i++){ temp = ta[i]; for(j=i-1;j>=0&&ta[j].time>temp.time;j--){ ta[j+1]=ta[j]; } //保证当两个值相等时,属于时间段开始的一定要排在属于时间段结束的后面 if(j>=0&&ta[j].time==temp.time){ if(ta[j].type=='E'&&temp.type=='B'){ ta[j+1]=ta[j]; ta[j]=temp; }else ta[j+1]=temp; }else ta[j+1]=temp; } } /** * 计算所需的最少颜色 * @return 最少颜色 */ public int getminColors(){ int nColorUsing = 0; //当前使用的颜色数量 Tarr begin = null; //记录循环中当前最近的开始时间 int i; //为ta赋值 for(i=0;i<tp.length;i++){ ta[2*i] = new Tarr(tp[i].b,'B',i); ta[2*i+1] = new Tarr(tp[i].e,'E',i); } insertSort(); for(i=0;i<2*n;i++){ if(ta[i].type=='B'){ nColorUsing++; begin = ta[i]; }else if((ta[i].type=='E')&&(ta[i].tpi==begin.tpi)){ nColorUsing--; } } return nColorUsing; } public static void main(String[] args) { Timeperiod tp1 = new Timeperiod(1,5); Timeperiod tp2 = new Timeperiod(2,3); Timeperiod tp3 = new Timeperiod(3,4); Timeperiod tp4 = new Timeperiod(3,6); Timeperiod tp[]={tp1,tp2,tp3,tp4}; Meeting2 mt = new Meeting2(4,tp); System.out.println("所需的最少颜色数为:"+mt.getminColors()); } }
程序运行结果如下:
所需的最少颜色数为:3
【解法三】计算最大重叠次数
问题的本质实际上就是求区间的最大重叠次数,代码如下:package chapter1youxizhileMeeting; /** * 面试时间段类 * @author DELL * */ class Timeperiod{ public int b; //开始时间 public int e; //结束时间 public Timeperiod(int b, int e){ this.b = b; this.e = e; } }
package chapter1youxizhileMeeting; /** * 高效率的安排见面会 * 扩展问题一【解法三】计算最大重叠次数 * @author DELL * */ public class Meeting3 { private int n; //面试总数 private Timeperiod[] tp; //用于存放相应面试时间段的数组 //构造函数 public Meeting3(int n, Timeperiod[] tp){ this.n = n; this.tp = tp; } /** * 判断两个时间段是否重叠 * @param tp1 时间段1 * @param tp2 时间段2 * @return 判断结果 */ public boolean isOverlap(Timeperiod tp1, Timeperiod tp2){ if((tp1.e<=tp2.b)||(tp1.b>=tp2.e)){ return false; }else return true; } /** * 计算所需的最少颜色 * @return 最少颜色 */ public int getminColors(){ int max = 0; //最大重叠次数 int count; //记录每个循环的重叠次数 for(int i=n-1;i>=0;i--){ count = 1; //本身为1 for(int j=0;j<i;j++){ if(isOverlap(tp[i],tp[j])) count++; } if(count>max) max = count; } return max; } public static void main(String[] args) { Timeperiod tp1 = new Timeperiod(1,5); Timeperiod tp2 = new Timeperiod(2,3); Timeperiod tp3 = new Timeperiod(3,4); Timeperiod tp4 = new Timeperiod(3,6); Timeperiod tp[]={tp1,tp2,tp3,tp4}; Meeting3 mt = new Meeting3(4,tp); System.out.println("所需的最少颜色数为:"+mt.getminColors()); } }
程序运行结果如下:
所需的最少颜色数为:3
其它参考链接:
《编程之美》1.9:高效率的安排见面会的一个解法
编程之美读书笔记_1.9 高效率的安排见面会
相关文章推荐
- webService之(一)java原生态服务端
- java通过给按钮添加监听器理解接口的作用
- Tomcat启动时加载某个servlet
- Nginx配置域名跳转实例
- Confluence5.1 最新版的安装&破解&汉化
- <iOS>关于viewWithTag的一点说明
- asp.net 预编译后代码的反编译成源码(演示:鹏为CRM E4版本)
- Java Web框架之二层模型-JSP+JavaBean
- 不同阶层数据分析师都在做什么呢
- json学习系列(2)-生成JSONObject的方法
- javamail邮件发送报错解决方案
- oracle分页查询
- oracle分页查询
- 互斥锁
- oracle分页查询
- android 防止bitmap 内存溢出
- oracle分页查询
- Python Django 开发 3 数据库CURD
- BZOJ1097 : [POI2007]旅游景点atr
- jquery.mmenu.js实现侧边导航非常适用于手机版站-最简使用教程(一)