The 14th Huazhong University of Science and Technology Programming Contest K Walking in the Forest【Dichotomous answer/minimum maximum】

链接: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

【Question meaning】:
The meaning of the question is to say that there are n stones, each with a certain distance between them. You can skip multiple stones at a time, but you cannot exceed k steps. Find the minimum value of the maximum step.

In fact, it means that if you take a relatively small step, such as going over stone by stone, then the number of steps will be too many; but if you jump directly to the end, it will be a waste of energy, and it is not in line with the meaning of the question. The meaning of the question is to find such a balance point, you can take exactly three steps, and then find the distance of the largest one step (relative to the other two steps, it is the largest, but relative to the largest step in all cases, it is the smallest one. condition)

[Source]: 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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325177579&siteId=291194637