【非原创】ZOJ - 4062 Plants vs. Zombies【二分】
题目:戳这里
题意:机器人走过一个花,可以给那个花浇水,给定步数下,问花的最小的最大能量值。
学习博客:戳这里
本人代码:
1 #include <bits/stdc++.h> 2 typedef long long ll; 3 const int maxn = 1e6+10; 4 const ll inf = 1e18; 5 using namespace std; 6 ll a[maxn]; 7 ll b[maxn]; 8 ll m, n; 9 int check(ll mid) { 10 for(ll i = 1; i <= n+1ll; ++i) { 11 b[i] = 0; 12 } 13 for(ll i = 1; i <= n; ++i) { 14 ll temp = mid/a[i]; 15 if(mid%a[i]) ++temp;//所有的数必须大于等于mid 16 if(i == n && b[i] >= temp) break;//当走到n,要考虑是不是还需要往下走了 17 b[i]++; 18 if(b[i] < temp) { 19 b[i+1] += temp - b[i]; 20 b[i] = temp; 21 } 22 } 23 ll cnt = 0; 24 for(ll i = 1; i <= n+1ll; ++i) { 25 cnt += b[i]; 26 if(cnt > m) return 0; 27 } 28 return 1; 29 30 } 31 int main(){ 32 int t; 33 scanf("%d", &t); 34 while(t--) { 35 scanf("%lld %lld", &n, &m); 36 for(ll i = 1; i <= n; ++i) { 37 scanf("%lld", a+i); 38 } 39 ll l = 1, r = inf; 40 ll ans = 0ll; 41 while(l <= r) { 42 ll mid = (l + r) / 2ll; 43 if(check(mid)) l = mid + 1ll, ans = mid; 44 else r = mid - 1ll; 45 } 46 printf("%lld\n", ans); 47 } 48 49 return 0; 50 }