题目大意:有$n$个点,每个点最开始有$a_i$个弓箭手,在第$i$个位置的弓箭手可以给$[i-r,i+r]$区间加上$1$的防御,你还有$k$个弓箭手,要求你最大化最小防御值

题解:二分答案,从右向左扫,显然得知,给一个位置增加防御值最好方式是在最右段加一些弓箭手。

卡点:

C++ Code:

  1. #include <cstdio>
  2. #define maxn 500010
  3. const long long inf = 0x3f3f3f3f3f3f3f3f;
  4. int n, R;
  5. long long k, protect1[maxn], s[maxn], protect2[maxn], protect3[maxn], p[maxn], protect4[maxn];
  6. inline int min(int a, int b) {return a < b ? a : b;}
  7. inline bool check(long long mid) {
  8. __builtin_memset(p, 0, sizeof p);
  9. int l = -R, r = R;
  10. long long now = 0, K = k;
  11. for (int i = 0; i < r; i++) now += s[i];
  12. for (int i = 0; i < n; i++) {
  13. now += s[r], now -= s[l - 1] + p[l - 1];
  14. if (now < mid) {
  15. p[r] = mid - now;
  16. K -= p[r];
  17. if (K < 0) return false;
  18. now = mid;
  19. }
  20. r++, l++;
  21. }
  22. return true;
  23. }
  24. int main() {
  25. scanf("%d%d%lld", &n, &R, &k);
  26. for (int i = 0; i < n; i++) scanf("%lld", s + i);
  27. long long l = 0, r = inf, ans;
  28. while (l <= r) {
  29. long long mid = l + r >> 1;
  30. if (check(mid)) {
  31. l = mid + 1;
  32. ans = mid;
  33. } else r = mid - 1;
  34. }
  35. printf("%lld\n", ans);
  36. return 0;
  37. }

  

[CF954G]Castle Defense的更多相关文章

  1. Educational Codeforces Round 40 G. Castle Defense (二分+滑动数组+greedy)

    G. Castle Defense time limit per test 1.5 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces 954 G. Castle Defense

    http://codeforces.com/problemset/problem/954/G 二分答案 检验的时候,从前往后枚举,如果发现某个位置的防御力<二分的值,那么新加的位置肯定是越靠后越 ...

  3. [CodeForces954G]Castle Defense(二分答案+差分)

    Description 题目链接 Solution 二分答案,套一个差分标记即可 每次放弓箭手显然越右边越优 Code #include <cstdio> #include <alg ...

  4. Educational Codeforces Round 40 (Rated for Div. 2) 954G G. Castle Defense

    题 OvO http://codeforces.com/contest/954/problem/G 解 二分答案, 对于每个二分的答案值 ANS,判断这个答案是否可行. 记 s 数组为题目中描述的 a ...

  5. Educational Codeforces Round 40 (Rated for Div. 2) Solution

    从这里开始 小结 题目列表 Problem A Diagonal Walking Problem B String Typing Problem C Matrix Walk Problem D Fig ...

  6. Educational Codeforces Round 40 A B C D E G

    A. Diagonal Walking 题意 将一个序列中所有的\('RU'\)或者\('UR'\)替换成\('D'\),问最终得到的序列最短长度为多少. 思路 贪心 Code #include &l ...

  7. Castle Core 4.0.0 alpha001发布

    时隔一年多以后Castle 项目又开始活跃,最近刚发布了Castle Core 4.0.0 的alpha版本, https://github.com/castleproject/Core/releas ...

  8. 对Castle Windsor的Resolve方法的解析时new对象的探讨

    依赖注入框架Castle Windsor从容器里解析一个实例时(也就是调用Resolve方法),是通过调用待解析对象的构造函数new一个对象并返回,那么问题是:它是调用哪个构造函数呢? 无参的构造函数 ...

  9. AOP之Castle DynamicProxy 动态代理

    这里主要介绍使用castle这个动态代理,在.net一些开源的框架里可以找到它的影子,就连微软的rchard也是使用这个进行方法拦截等可以基于这个进行方法拦截,在这个方面PostSharp算是比较好用 ...

随机推荐

  1. 使用winsw将spring-boot jar包注册成windows服务

    背景:最近的项目中使用spring-boot, https://github.com/kohsuke/winsw/releases <service> <id>YJPSS< ...

  2. Vscode插件--微信小程序格式化以及高亮组件wxml-vscode

    wxml-vscode wxml-vscode 仓库 提问题 安装 通过 F1 或者 CMD + Shift + P 输入 install. 选择: Install Extension. 特性 格式化 ...

  3. Codeforces#498F. Xor-Paths(折半搜索)

    time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. 配置intellij idea中的欢迎页而不使用默认的index.jsp

    在web.xml中添加 <welcome-file-list> <welcome-file>abc.jsp</welcome-file> </welcome- ...

  5. Thymeleaf显示Map集合数据

    <select class="form-control zz-set-input-size" id="channel"> <option va ...

  6. 对Neural Machine Translation by Jointly Learning to Align and Translate论文的详解

    读论文 Neural Machine Translation by Jointly Learning to Align and Translate 这个论文是在NLP中第一个使用attention机制 ...

  7. linxu信号种类

    使用kill -l 命令,可看到linux支持的信号列表: 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGB ...

  8. POJ:3684-Physics Experiment(弹性碰撞)

    Physics Experiment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3392 Accepted: 1177 Sp ...

  9. POJ:1995-Raising Modulo Numbers(快速幂)

    Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9512 Accepted: 578 ...

  10. 初见spark-04(高级算子)

    今天,这个是spark的高级算子的讲解的最后一个章节,今天我们来介绍几个简单的算子, countByKey val rdd1 = sc.parallelize(List(("a", ...