标签:
题意:
二分答案,设答案为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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
Codeforces 551C GukiZ hates Boxes 二分答案
标签:
原文地址:http://blog.csdn.net/qq574857122/article/details/46911837