[编程之美]安排见面会

 
View Code
View Code
1 Build a sorted array consisting of start time and finish time of each event
2 traverse
3 if(element_type==begin)
4 add a new room
5 else
6 delete a room


 1 //Get the first finish time of rooms
2 int getFirst(int n,int* RoomFinish)
3 {
4 int first=0;
5 for(int i=1;i<n;++i)
6 if(RoomFinish[i]<RoomFinish[first])
7 first=i;
8
9 return first;
10 }
11
12 int Greedy(int* start,int* end,int n,int* RoomFinish)
13 {
14 //Add a room for the first event
15 int count=1;
16 RoomFinish[count-1]=end[0];
17
18 for(int i=1;i<n;++i)
19 {
20 int first=getFirst(count,RoomFinish);
21 //if the start time of this event is before the first finish time of rooms,we must add a new room for it
22 if(start[i]<RoomFinish[first])
23 {
24 count++;
25 RoomFinish[count-1]=end[i];
26 }
27 //if the start time of this event is after the first finish time of rooms, we just substitute the event with this event
28 else
29 {
30 RoomFinish[first]=end[i];
31 }
32 }
33 return count;
34 }
posted @ 2012-03-23 11:12  Cavia  阅读(262)  评论(0编辑  收藏  举报