/* 有理有据的二分答案 因为在过程中最多减到零 所以n越小显然就能刷更多的题 无解时就是无论如何也无法得到k , 这个特判一下即可 */ #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #define M 100010 #define ll long long #define inf 100000000000ll using namespace std; ll read() { ll nm = 0, f = 1; char c = getchar(); for(; !isdigit(c); c = getchar()) if(c == ‘-‘) f = -1; for(; isdigit(c); c = getchar()) nm = nm * 10 + c - ‘0‘; return nm * f; } ll note[M], n, k; ll check(ll x) { ll sum = 0, ans = 0; for(int i = 1; i <= n; i++) { sum += note[i]; if(sum >= x) ans++, sum = 0; if(sum < 0) sum = 0; } return ans; } int main() { n = read(), k = read(); for(int i = 1; i <= n; i++) note[i] = read(); ll l = 1, r = inf, ln, rn; while(l + 1 < r) { ll mid = (l + r) >> 1; if(check(mid) <= k) r = mid; else l = mid; } if(check(l) == k) r = l; ln = r; l = 1, r = inf; while(l + 1 < r) { ll mid = (l + r) >> 1; if(check(mid) >= k) l = mid; else r = mid; } if(check(r) == k) l = r; rn = l; if(ln > rn || check(ln) != k) return puts("-1"); cout << ln << " " << rn << "\n"; return 0; }
原文地址:https://www.cnblogs.com/luoyibujue/p/9246240.html
时间: 2024-10-10 04:46:45