[UVA][二分搜+greedy] 907 - Winterim Backpacking Trip@Morris' Blog|PChome Online 人新台
2013-01-27 08:37:24| 人1,497| 回3 | 上一篇 | 下一篇

[UVA][二分搜+greedy] 907 - Winterim Backpacking Trip

0 收藏 0 0 站台

Problem C

Winterim Backpacking Trip

This Winter we are going on a trip along the Appalachian Trail. The trail is a continuous marked footpath that goes from Katahdin in Maine to Springer Mountain in Georgia, a distance of about 2160 miles. Even though our trip will only consider some part of the trail, it will be our first real backpacking experience and an excellent opportunity to acquire winter camping skills.

Part of the experience is also the route planning of the trip. We have a list of all possible campsites that we can use along the way and we want to do this trip so that we only stop K nights to camp. We also know in advance the distance between consecutive campsites and we are only allowed to camp at a campsite. Our goal is to plan the trip so that we minimise the maximum amount of walking done in a single day. In other words, if our trip involves 2 nights (3 days of walking), and we walk 9, 10, 5 miles on each day respectively, the cost (maximum amount of walking done in one day) is 10. Another schedule that involves walking 9, 6, 9 miles on each day has cost 9.

Problem

Given the distances between N consecutive campsites of a trail and given the number of nights for your trip, K, your task is to devise a camping strategy for the specified trail such that it minimises the maximum amount of walking done in a single day. Note that the first distance value given is the distance from our start-point of the trail to our 1st campsite, and the last distance value given is the distance from our Nth campsite to our end-point of the trail.

Input

The input file contains several test cases, each of them as describes below.
The first line of input consists of two numbers, the number of campsites (0 < N <= 600) and the number of nights of the trip (0 <= K <= 300). The following N+1 input lines indicate the distance in miles between consecutive campsite locations.

Output

For each test case, on a line by itself, your program must output the maximum amount of walking in a single day for the route that minimises such value.

Sample Input

4 3 7 2 6 4 5 

Sample Output

8


的另外一方法是 DP,效率 O(N*N*K)

那使用二分搜答案,使用 O(n) 去,效率 O(N*log(ans))




#include <stdio.h>
int A[605], n, m;
int check(int res) {
int sum = 0, cnt = 0, i;
for(i = 0; i <= n; i++) {
if(A[i] > res) return 0;
if(sum+A[i] > res)
sum = A[i], cnt++;
else
sum += A[i];
}
if(sum) cnt++;
return cnt <= m;
}
int main() {
int i;
while(scanf("%d %d", &n, &m) == 2) {
n++, m++;
for(i = 0; i < n; i++)
scanf("%d", &A[i]);
int l = 0, r = 1000000, mid;
while(l <= r) {
mid = (l+r)/2;
if(check(mid))
r = mid-1;
else
l = mid+1;
}
printf("%d\n", r+1);
}
return 0;
}
 

台: Morris
人(1,497) | 回(3)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA] 10573 - Geometry Paradox
此分上一篇:[UVA][字串理] 10438 - Meta Editor

xem phim online
nice article...
2017-10-03 14:09:12
sexe videos
Thank...
2018-06-09 23:35:37
sesso videos
Thank...
2018-06-09 23:36:11
是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86