1. 程式人生 > >第十四屆華中科技大學程序設計競賽 K Walking in the Forest【二分答案/最小化最大值】

第十四屆華中科技大學程序設計競賽 K Walking in the Forest【二分答案/最小化最大值】

ica ride int lib namespace coder 浪費 first efi

鏈接:https://www.nowcoder.com/acm/contest/106/K
來源:牛客網

題目描述 
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.

Now you're going to walk through a large forest. There is a path consisting of N stones winding its way to the other side of the forest. Between every two stones there is a distance. Let di indicates the distance between the stone i and i+1.Initially you stand at the first stone, and your target is the N-th stone. You must stand in a stone all the time, and you?can?stride over arbitrary number of stones in one step. If you stepped from the stone i to the stone j, you?stride a span of?(di+di+1+...+dj-1). But there is a limitation. You're so tired that you want to walk through the forest in no more than K steps. And to walk more comfortably, you have to minimize the distance of largest step.
輸入描述:
The first line contains two integer N and K as described above.
Then the next line N-1 positive integer followed, indicating the distance between two adjacent stone.
輸出描述:
An integer, the minimum distance of the largest step.
示例1
輸入
6 3
1 3 2 2 5
輸出
5

【題意】:
題意就是說有n塊石頭,每塊石頭中間有一定的距離,一次可以跳過多個石頭,但是不可以超過k步,求最大步的最小值 。

其實意思就是說,如果你每一步比較小,比如一塊一塊石頭地過去,那麽步數就太多了;但是如果你直接一步跳到最後,這樣又太浪費體力了,而且不符合題意,而題意就是要你找到這麽一個平衡點,可以恰好走到三步,然後求其中最大的一步的距離(相對於其他兩步是最大的,但相對於所有情況的最大步它是最小的一種情況)

【出處】:POJ 3272 Monthly Expense

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <set>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
#define ll long long
#define mod 1000000007
int n, k;
ll a[100005];
int main() {
    scanf("%d%d", &n, &k);
        ll s = 0;
        ll Max = 0;
        for (int i = 1; i <= n - 1; ++i) {
            scanf("%lld", &a[i]);
            s += a[i];
            Max = max(Max, a[i]);
        }
        ll l = Max, r = s, ans, mid;
        while (l <= r) {
            mid = (l + r) >> 1;
            s = 0;
            int c = 0;
            for (int i = 1; i <= n - 1; ++i) {
                s += a[i];
                if (s > mid) {   //多個跳不過,只能前面算跳一次,從這裏重新開始跳
                    s = a[i];
                    c++;
                }
            }

            if (c >= k) {
                l = mid + 1;
            }
            else {
                r = mid - 1;
                ans = mid;
            }
        }
        cout << ans << endl;
    return 0;
}

第十四屆華中科技大學程序設計競賽 K Walking in the Forest【二分答案/最小化最大值】