G. Castle Defense

time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Today you are going to lead a group of elven archers to defend the castle that is attacked by an army of angry orcs. Three sides of the castle are protected by impassable mountains and the remaining side is occupied by a long wall that is split into n sections. At this moment there are exactly a**i archers located at the i-th section of this wall. You know that archer who stands at section i can shoot orcs that attack section located at distance not exceeding r, that is all such sections j that |i - j| ≤ r. In particular, r = 0 means that archers are only capable of shooting at orcs who attack section i.

Denote as defense level of section i the total number of archers who can shoot at the orcs attacking this section. Reliability of the defense plan is the minimum value of defense level of individual wall section.

There is a little time left till the attack so you can't redistribute archers that are already located at the wall. However, there is a reserve of k archers that you can distribute among wall sections in arbitrary way. You would like to achieve maximum possible reliability of the defence plan.

Input

The first line of the input contains three integers n, r and k (1 ≤ n ≤ 500 000, 0 ≤ r ≤ n, 0 ≤ k ≤ 1018) — the number of sections of the wall, the maximum distance to other section archers can still shoot and the number of archers yet to be distributed along the wall. The second line contains n integers a1, a2, ..., a**n (0 ≤ a**i ≤ 109) — the current number of archers at each section.

Output

Print one integer — the maximum possible value of defense plan reliability, i.e. the maximum possible value of minimum defense level if we distribute k additional archers optimally.

Examples

input

Copy

5 0 65 4 3 4 9

output

Copy

5

input

Copy

4 2 01 2 3 4

output

Copy

6

input

Copy

5 1 12 1 2 1 2

output

Copy

3

https://codeforces.com/contest/954/problem/G

题意:

给你n个位置,每一个位置现在有a[i]个士兵驻守,,a[i]的士兵可以守护\([i-r,i+r]\),r是给定的数值。

现在可以添加k个士兵,问你在最优分配的情况下,每一个位置\(i\)最小有多少个士兵守护?

思路:

最优分配情况的最小值,显然是可以二分答案。

那么我们可以怎么check呢?

假设当前二分到了mid

我们对于每一个位置\(i\)可以获得当前num个士兵守护,如果num>=mid,继续i+1位置,

如果num<mid,即需要用一些补充的士兵来增加该位置的驻守。

最优分配策略是贪心的在\(a[i+r]\)位置加\(mid-num\)个士兵。

这样就涉及到了动态操作问题:

单点修改,区间查询。

如果用树状数组或者线段树来解决,会TLE(亲测),因为会多个log。

那么我们可以用一个双指针进行滑动数组的方式来解决该问题。

这样check的部分时间复杂度就是\(O(n)\)

外部的二分部分时间复杂度是\(O(log_2R)\)

R是二分的边界范围,通过数据范围分析可以知道R上限是2e18;

那么整体的时间复杂度\(O(n*log_2R)\)

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll k;
int w;
int n;
ll a[maxn];
ll tree[maxn];
int lowbit(int x)
{
return -x & (x);
}
void add(int x, ll val)
{
while (x <= n)
{
tree[x] += val;
x += lowbit(x);
}
}
ll ask(int x)
{
ll res = 0ll;
while (x)
{
res += tree[x];
x -= lowbit(x);
}
return res;
}
typedef pair<int, ll> pil;
std::vector<pil> v;
bool check(ll mid)
{
ll temp = k;
bool res = 1;
ll num = 0ll;
int l = 1;
int r = 0;
repd(i, 1, n)
{
while (r - i < w)
{
num += a[++r];
}
while (i - l > w)
{
num -= a[l++];
}
if (num < mid)
{
if (temp >= mid - num)
{
temp -= mid - num;
} else
{
res = 0;
break;
}
a[min(i + w, n)] += mid - num;
v.push_back(mp(min(i + w, n), mid - num));
num = mid;
}
}
for (auto &t : v)
{
a[t.fi] += -1ll * t.se;
}
v.clear();
return res; }
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
cin >> n >> w >> k;
repd(i, 1, n)
{
cin >> a[i];
// add(i, a[i]);
}
ll l = 0ll;
ll r = 2e18;
ll mid;
ll ans;
while (l <= r)
{
mid = (l + r) >> 1;
if (check(mid))
{
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans << endl;
return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Educational Codeforces Round 40 G. Castle Defense (二分+滑动数组+greedy)的更多相关文章

  1. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  2. Educational Codeforces Round 40千名记

    人生第二场codeforces.然而遇上了Education场这种东西 Educational Codeforces Round 40 下午先在家里睡了波觉,起来离开场还有10分钟. 但是突然想起来还 ...

  3. Educational Codeforces Round 40 C. Matrix Walk( 思维)

    Educational Codeforces Round 40 (Rated for Div. 2) C. Matrix Walk time limit per test 1 second memor ...

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

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

  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 (Rated for Div. 2) 954G G. Castle Defense

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

  7. Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  8. Educational Codeforces Round 21 D.Array Division(二分)

    D. Array Division time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  9. Educational Codeforces Round 11 C. Hard Process 二分

    C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...

随机推荐

  1. Java基础——值传递

    值传递? 参数传递的值的拷贝,改变拷贝不影响原参数. 引用传递? 参数传递的是参数本身,改变形参,实参也改变. Java中是什么传递? Java中只有值传递 实际情况为什么不对呢? 1. 基本数据类型 ...

  2. ETH&EOS开发资源及工具集合(完整汇总版)

    ETH&EOS开发资源及工具集合(完整汇总版) 3113 ETH开发资源篇 一.开发语言 ·         Solidity - 官方推荐以太坊智能合约开发语言,也是目前最为主流的智能合约语 ...

  3. ES6 var,const , let三者区别

    每天学一点,知识涨一张 var 默认是会变量提升的,变量可以修改: let 定义变量,变量可以修: const 定义必须有常量值,const的值一但写上不可更改:let 与const相同之处:1> ...

  4. Java学习笔记-抽象类与接口

    抽象类用于在类中不用具体实现,而在子类中去实现的类 抽象类 抽象类概述 抽象定义:抽象就是从多个事物中将共性的,本质的内容抽取出来 抽象类:Java中可以定义没有方法体的方法,该方法的具体实现由子类完 ...

  5. DDE 的知识和使用

    在github上下载.net 版本的NDde 开发包 或者在此处下载开发包 MSDN 地址 创建服务器 class BasicDDE:DdeServer { public BasicDDE(strin ...

  6. Nginx 小入门记录 之 初识Nginx和环境准备(一)

    前置知识准备: 如果还不知道服务器是干什么的,只是刚踏入程序员之路的,大家还是先学习基础,虽然以下文档很简单,但至少知道为什么要学: 一般服务器环境现在基本上都是放在Linux系统上了,如果对Linu ...

  7. nginx文件服务器搭建

    一.安装 (CentOS 7) yum install nginx -y 如果报错: [u3@L3 /]$ sudo yum install nginx -y Loaded plugins: fast ...

  8. 剑指offer24:二叉树中和为输入整数值的所有路径。(注意: 在返回值的list中,数组长度大的数组靠前)

    1 题目描述 输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长 ...

  9. 笔记-3:mysql数据定义

    1.定义数据库 1.1 创建数据库:创建数据库是在系统磁盘上划分一块区域用于数据的存储和管理. # 基本语法: create {database | schema} [if not exists] d ...

  10. gitlab安装指南(gitlab-ce-9.4.3-ce.0.el7.x86_64 centos7)

    1,安装gitlab wget https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-9.4.3-ce.0.el7. ...