UVA 11729

Job order problem.

greedy :

Sort the people by their work time decreasingly. And that's the order.

The total time is max {start_time[i] + work_time[i]} 

Demonstration:

Assumes that x1x2x3...xixj...xn is the order.

If we swap xi,xj,  all of the other man's ending time is not changed. (start_time[i] + work_time[i]) is not changed.

Case 1:

Before:

if (start_time[i] + work_time[i]) >= start_time[j] + work_time[j]

That means tell_time[i] + work_time[i] >= tell_time[i] + tell_time[j] + work_time[j]

equals to:

work_time[i] >= tell_time[j] + work_time[j] (work_time[i] >= work_time[j])

After:

j's ending time = tell_time[j] + work_time[j],

i's ending time = tell_time[j] + tell_time[i] + work_time[i] 

so we know now i's ending time is later than before. That means we should not swap i and j when work_time[i] > work_time[j].

 

Case 2:

Before :

if (start_time[i] + work_time[i]) < start_time[j] + work_time[j]

that means tell_time[i] + work_time[i] < tell_time[i] + tell_time[j] + work_time[j]

work_time[i] < tell_time[j] + work_time[j]

Before: i's ending time is tell_time[i] + work_time[i]; j's is tell_time[i] + tell_time[j] + work_time[j]

after: i's ending time is tell_time[j] + tell_time[i] + work_time[i]; j's ending time is tell_time[j] + work_time[j]

Before: The max endingtime(i,j) is tell_time[i] + tell_time[j] + work_time[j];

After: The max endingtime(i,j) is tell_time[j] + tell_time[i] + work_time[i];

case 2.1: work_time[i] >= work_time[j]

 

Since work_time[i] >= work_time[j], so we should not swap i and j in this case.

case 2.2:work_time[i] < work_time[j]

Since work_time[i] < work_time[j], so we should swap i and j in this case.

So the greedy strategy  is right.

 

         

 

 

 

 

 

 

posted @ 2012-11-16 15:26  HardWay  阅读(159)  评论(0编辑  收藏  举报