題意:
1 0 2
二分答案,設答案爲x,即需要x秒來搬完石頭
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) pt(x / 10);
putchar(x % 10 + '0');
}
typedef long long ll;
typedef pair<ll, ll> pii;
const int inf = 1e9;
const int N = 1e5 + 10;
int n, m;
int a[N], b[N];
bool ok(ll x) {
for (int i = 1; i <= n; i++)b[i] = a[i];
int top = n, tmp = m;
while (tmp-->0 && top) {
ll lef = x - top;
while (lef && top) {
if (b[top] == 0) { top--;continue; }
if (b[top] <= lef) {
lef -= b[top--];
}
else { b[top] -= (int)lef;lef = 0; }
}
}
while (top && b[top] == 0)top--;//找到最後一個並且不是0的點
return top == 0;
}
int main() {
rd(n); rd(m);
int d = 1;
for (int i = 1; i <= n; i++) {
rd(a[i]);
}
while (a[n] == 0)n--; //把最後的0刪掉
ll l = 1, r = 1e15, ans;
while (l <= r) {
ll mid = (l + r) >> 1;
if (ok(mid)) {
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
pt(ans);
return 0;
}