UVA907 Winterim Backpacking Trip最大化最小值+二分

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA907 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.
    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 N-th 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

问题链接UVA907 Winterim Backpacking Trip
问题简述:(略)
问题分析:最大化最小值问题,可以用二分搜索来做,也可以用贪心来做。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA907 Winterim Backpacking Trip */

#include <stdio.h>

const int N = 600 + 1;
int n, k, a[N];

int judge(int mid)
{
    int sum = 0, cnt = 0;
    for (int i = 0; i < n; i++) {
        if (a[i] > mid) return 0;
        if (sum + a[i] > mid) sum = a[i], cnt++;
        else sum += a[i];
    }
    if (sum) cnt++;
    return cnt <= k;
}

int main()
{
    while (scanf("%d%d", &n, &k) == 2) {
        n++, k++;
        for (int i = 0; i < n; i++)
            scanf("%d", &a[i]);

        int l = 0, r = 1000000, mid;
        while (l <= r) {
            mid = (l + r) / 2;
            if (judge(mid)) r = mid - 1;
            else l = mid + 1;
        }

        printf("%d\\n", r + 1);
    }

    return 0;
}

以上是关于UVA907 Winterim Backpacking Trip最大化最小值+二分的主要内容,如果未能解决你的问题,请参考以下文章

V853开发板开发进阶——在Linux下加载E907核心固件

V853开发板硬件资料——RISC-V核E907用户手册

Tina Linux E907开发指南

全志V853芯片 在Tina下RISC-V核E907启动方式的选择

CodeForces 907E Party(bfs+状压DP)

CodeForces 907F Power Tower(扩展欧拉定理)