hihocoder-Week175-Robots Crossing River
hihocoder-Week175-Robots Crossing River
Robots Crossing River
時間限制:10000ms 單點時限:1000ms 內存限制:256MB描述
Three kinds of robots want to move from Location A to Location B and then from Location B to Location C by boat.
The only one boat between A and B and only one between B and C. Moving from A to B (and vise versa) takes 2 hours with robots on the boat. Moving from B to C (and vice versa) takes 4 hours. Without robots on the boat the time can be reduced by half. The boat between A and B starts at time 0 moving from A to B. And the other boat starts 2 hours later moving from B to C.
You may assume that embarking and disembarking takes no time for robots.
There are some limits:
1. Each boat can take 20 robots at most.
2. On each boat if there are more than 15 robots, no single kind of robots can exceed 50% of the total amount of robots on that boat.
3. At most 35 robots are allowed to be stranded at B. If a robot goes on his journey to C as soon as he arrives at B he is not considered stranded at B.
Given the number of three kinds robots what is the minimum hours to take them from A to C?
輸入
Three integers X, Y and Z denoting the number of the three kinds of robots. (0 ≤ X, Y and Z ≤ 1000)
輸出
The minimum hours.
- 樣例輸入
-
40 4 4
- 樣例輸出
-
24
使用貪心。
首先這是一道非常考驗解決問題能力的題目。
(參考hihocoder的題目分析才做出來的, from: http://hihocoder.com/discuss/question/5025 )
首先需要看出整個流程的瓶頸完全在B-C這一段,換句話說我們只需求出所有機器人從B到C的最少時間,再加上2小時就是答案。事實上這個時間恰好等於把所有機器人直接從B運到C最少需要的船次x6。
如果沒有“一船超過15個機器人則每種機器人不能超過半數”的限制,我們只需要20/船運走即可,最少船次是ceil((X+Y+Z)/20)。
由於有上面的限制,我們需要仔細討論一下XYZ的相對大小。不妨設X >= Y >= Z,同時我們稱三種機器人也為X、Y、Z類。
1、如果X <= Y + Z,那麽我們仍然可以20/船運走,同時所有船都沒有機器人超過半數。
這種情況最少船次仍然是是ceil((X+Y+Z)/20)。
2、 如果X > Y + Z,這時我們沒辦法使所有船都載20機器人。但是我們當然希望能盡量派出載20機器人的船。於是有如下貪心策略:
1) 首先盡量10個X類和10個非X類組成一船,派出若幹船直到非X類不足10個機器人。
2) 余下若幹(不足10個)非X類機器人,配合盡可能多X類機器人組成一船。這裏需要討論余下的非X類機器人有多少個,不妨設為K。如果K不足8個,那麽最多配合15-K個X類機器人,組成一船15個機器人;否則可以配合K個X類機器人,組成一船2K個機器人。
3) 最後只剩下若幹X類機器人,這些機器人只能15/船派出
#include <cstdio> #include <cstdlib> int cmp(const void *a, const void *b){ return (*(int *)a - *(int *)b); } int main(){ int ans, num[3]; while(scanf("%d %d %d", &num[0], &num[1], &num[2]) != EOF){ qsort(num, 3, sizeof(num[0]), cmp); ans = 0; while(num[0] + num[1] + num[2] > 0){ if( (num[0] + num[1]) >= num[2] ){ ans += ( num[0] + num[1] + num[2] ) / 20; if( (num[0] + num[1] + num[2]) % 20 != 0 ){ ++ans; } num[0] = num[1] = num[2] = 0; }else{ while(num[2] >= 10 && num[1] >= 10){ ++ans; num[2] -= 10; num[1] -= 10; qsort(num, 3, sizeof(num[0]), cmp); } if(num[2] >= 10){ if( num[1] + num[0] >= 10 ){ num[2] -= 10; num[1] -= (10 - num[0]); num[0] = 0; ++ans; }else if(num[1] + num[0] >= 8){ num[2] -= ( num[1] + num[0] ); num[1] = num[0] = 0; ++ans; } } ans += ( num[2] + num[1] + num[0] ) / 15; if( ( num[2] + num[1] + num[0] )%15 != 0 ){ ++ans; } break; } } ans = 4 * ans + 2*(ans - 1) + 2; printf("%d\n", ans ); } return 0; }
hihocoder-Week175-Robots Crossing River