您的位置:首页 > 编程语言

[编程之美]安排见面会

2012-03-23 11:12 148 查看
View Code

Build a sorted array consisting of start time and finish time of each event
traverse
if(element_type==begin)
add a new room
else
delete a room


//Get the first finish time of rooms
int getFirst(int n,int* RoomFinish)
{
int first=0;
for(int i=1;i<n;++i)
if(RoomFinish[i]<RoomFinish[first])
first=i;

return first;
}

int Greedy(int* start,int* end,int n,int* RoomFinish)
{
//Add a room for the first event
int count=1;
RoomFinish[count-1]=end[0];

for(int i=1;i<n;++i)
{
int first=getFirst(count,RoomFinish);
//if the start time of this event is before the first finish time of rooms,we must add a new room for it
if(start[i]<RoomFinish[first])
{
count++;
RoomFinish[count-1]=end[i];
}
//if the start time of this event is after the first finish time of rooms, we just substitute the event with this event
else
{
RoomFinish[first]=end[i];
}
}
return count;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: