24h購物| | PChome| 登入
2013-01-27 08:37:24| 人氣1,380| 回應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,380) | 回應(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
詳全文