一個自動刷題機,每次有兩種操做:寫下\(x\)行代碼或刪除\(x\)行代碼(不足則所有刪除)。存在一個\(n\),每當代碼量大於等於\(n\)時將提交一次並把代碼所有刪除。已知每次的操做類型和\(x\),已知一共提交了\(k\)次,問\(n\)的最大值和最小值。spa
能夠證實\(n\)增大時提交次數不減。因而二分便可。code
#include <algorithm> #include <cstdio> typedef long long LL; const int N = 100050; int x[N], n, k; int calc(LL m) { int ans = 0; for (LL i = 0, t = 0; i < n; ++i) if ((t = std::max(t + x[i], 0LL)) >= m) ++ans, t = 0; return ans; } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; ++i) scanf("%d", &x[i]); if (calc(1) < k) return puts("-1") & 0; LL l = 1, r = 1e15; while (l < r) { LL mid = (l + r) / 2; if (calc(mid) > k) l = mid + 1; else r = mid; } if (calc(l) != k) return puts("-1") & 0; printf("%lld ", l); l = 1, r = 1e15; while (l < r) { LL mid = r + (l - r) / 2; if (calc(mid) < k) r = mid - 1; else l = mid; } printf("%lld\n", l); return 0; }