【二分答案】CF1613 C. Poisoned Dagger
本题的优解是二分答案,但我其实不会二分,本质是用了两个指针作为边界,然后不断对半缩小范围来快速确定答案。
神奇的二分法
代码:
#include <iostream> using namespace std; typedef long long ll; ll a[110]; ll n, h; bool check(ll k) { ll t = h - k; for (int i = 2; i <= n; ++i) { t -= min(k, a[i] - a[i - 1]); } if (t <= 0) return 1; else return 0; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { cin >> n >> h; for (int i = 1; i <= n; ++i) { cin >> a[i]; } ll l = 1, r = h; while (l <= r) { ll mid = (l + r) >> 1;//取中点 if (check(mid)) { r = mid - 1; } else l = mid + 1; } cout << l << endl; } return 0; }