OvO http://codeforces.com/contest/954/problem/G

  二分答案,

  对于每个二分的答案值 ANS,判断这个答案是否可行。

  记 s 数组为题目中描述的 a 数组

  以下为初始化:

    首先建一个 pre 数组, pre[i] 表示 s 数组中第 个元素到第 i 个元素的和

    然后建一个 p 数组,其中 p[i] 代表能射到第 i 个区域的弓箭手数量, p[i] 显然可以由 pre 计算得到

  以下是对于二分的每个答案值 ANS,判断这个值是否可行的做法:

    创建一个 d 数组, d[i] = p[i] - p[i-1],

    从左到右枚举 d 数组,用变量 now 累加得到当前区域的坚固程度(即能有多少弓箭手能射到这个块)

    枚举的过程中,如果出现 now 比 ANS 值小的话,则得到这个差值, 记这个差值为 tmp ,那么显然要在 i+r 的地方放 tmp 个弓兵,那么要做如下操作

      1. now+=tmp (则当前节点的坚固程度+tmp)

      2.d[i+2*r+1)]-=tmp (区域 [i,i+2*r] 的坚固程度均加 tmp,等价的就是 1,2 这两个操作)

      3.记总的可用弓兵消耗为为 cst ,则 cst+=tmp

    如果 cst>k 显然不行,否则可行。

  注意数据范围,容易中间爆 long long 之类的,用线段树时间大概不够(吧)

  

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio> using namespace std; typedef long long ll; const ll M=11e5+44;
const ll INF=4e18+44; ll n,r;
ll s[M];
ll k;
ll p[M],pre[M],d[M]; bool check(ll spl)
{
ll cst=0,now=0,tmp;
for(ll i=1;i<=n;i++)
d[i]=p[i]-p[i-1];
for(ll i=1;i<=n;i++)
if((now+=d[i])<spl)
{
tmp=spl-now;
if((cst+=tmp)>k) return false;
d[i]+=tmp,now+=tmp,d[min(n+1,i+2*r+1)]-=tmp;
}
return true;
} ll solve()
{
ll ret;
ll li,ri,mid;
li=0,ri=INF;
while(li<ri-1)
{
mid=((li+ri)>>1);
if(check(mid)) li=mid;
else ri=mid;
}
return ret=li;
} int main()
{
ll li,ri;
scanf("%I64d%I64d%I64d",&n,&r,&k);
for(ll i=1;i<=n;i++)
scanf("%I64d",&s[i]);
memset(pre,0,sizeof(pre));
memset(p,0,sizeof(p));
for(ll i=1;i<=n;i++)
pre[i]=pre[i-1]+s[i];
for(ll i=1;i<=n;i++)
{
li=i-r,ri=i+r;
if(li<1) li=1;
if(ri>n) ri=n;
p[i]=pre[ri]-pre[li-1];
}
printf("%I64d\n",solve());
return 0;
}

  

      

Educational Codeforces Round 40 (Rated for Div. 2) 954G G. Castle Defense的更多相关文章

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

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

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

    A. Diagonal Walking time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  4. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  5. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  6. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  7. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  8. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  9. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

随机推荐

  1. spring boot 2.x版本:java.lang.ClassNotFoundException: org.springframework.boot.bind.RelaxedDataBinder

    标题 ##搭建spring boot 2.0.3版本 使用alibaba的druid数据库连接池,com.github.pagehelper的分页插件,启动项目报错. 错误提示:java.lang.C ...

  2. 【C++札记】指针函数与函数指针

    指针函数 指针函数是一个函数,只不过指针函数返回的类型是某一类型的指针. 格式: 类型名* 函数名(函数参数列表) 如下代码存在问题 void test(char *p) { p = (char*)m ...

  3. 【C++札记】类的分离式写法

    介绍 类的分离式写法,使得代码更加规范,增强了阅读性. 分离式写法的规则: 1.类的变量:写在类的里面 2.成员函数:类中写函数的声明,函数的定义写在类体外. 3.写在类外函数定义时,类名前加限定(O ...

  4. 数据类型 _python

    字符串 str # a ="asd bfg" # print(a.capitalize()) #首字母大写 # print(a.title()) #每个单词首字母大写 # prin ...

  5. go map的定义和使用 键值对存储

    定义map    var m map[string]int //定义map 初始化map    m = make(map[string]int) //初始化map 修改map中ok 的值  m[&qu ...

  6. SAS学习笔记20 CAT函数

  7. docker部署Eurake服务,服务节点无法注册服务

    前言 昨天在部署一个docker项目时遇到了一个问题,故记录下来. 环境说明 Centos7 Docker version 18.06.3-ce, build d7080c1 问题说明 该项目分别启动 ...

  8. idea jetty:run 启动

    1.首先pom    文件 <!-- jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> ...

  9. (二)CXF之用CXF官方工具生成客户端Client

    一.CXF工具的下载与使用 登录CXF官网:http://cxf.apache.org/download.html 下载,本系列使用的是3.1.5版本: 添加path环境变量 二.案例 2.1 发布w ...

  10. 【Transact-SQL】让人快遗忘的游标

    原文:[Transact-SQL]让人快遗忘的游标 最初学SQL Server的时候,当学到游标的时候,突然有了一种亲切感,因为这种通过一个while循环,一条一条的处理数据的方式,很像学过的过程式语 ...