[CF954G]Castle Defense
题目大意:有$n$个点,每个点最开始有$a_i$个弓箭手,在第$i$个位置的弓箭手可以给$[i-r,i+r]$区间加上$1$的防御,你还有$k$个弓箭手,要求你最大化最小防御值
题解:二分答案,从右向左扫,显然得知,给一个位置增加防御值最好方式是在最右段加一些弓箭手。
卡点:无
C++ Code:
#include <cstdio> #define maxn 500010 const long long inf = 0x3f3f3f3f3f3f3f3f; int n, R; long long k, protect1[maxn], s[maxn], protect2[maxn], protect3[maxn], p[maxn], protect4[maxn]; inline int min(int a, int b) {return a < b ? a : b;} inline bool check(long long mid) { __builtin_memset(p, 0, sizeof p); int l = -R, r = R; long long now = 0, K = k; for (int i = 0; i < r; i++) now += s[i]; for (int i = 0; i < n; i++) { now += s[r], now -= s[l - 1] + p[l - 1]; if (now < mid) { p[r] = mid - now; K -= p[r]; if (K < 0) return false; now = mid; } r++, l++; } return true; } int main() { scanf("%d%d%lld", &n, &R, &k); for (int i = 0; i < n; i++) scanf("%lld", s + i); long long l = 0, r = inf, ans; while (l <= r) { long long mid = l + r >> 1; if (check(mid)) { l = mid + 1; ans = mid; } else r = mid - 1; } printf("%lld\n", ans); return 0; }