Codeforces 954 G. Castle Defense
http://codeforces.com/problemset/problem/954/G
二分答案
检验的时候,从前往后枚举,如果发现某个位置的防御力<二分的值,那么新加的位置肯定是越靠后越好
差分即可
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; #define N 500001 typedef long long LL; int n,r;
LL k; LL sum[N];
LL cf[N]; template<typename T>
void read(T &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} bool check(LL x)
{
LL rest=k,need;
LL add=;
memset(cf,,sizeof(cf));
for(int i=;i<=n;++i)
{
add+=cf[i];
if(sum[i]+add<x)
{
need=x-sum[i]-add;
rest-=need;
if(rest<) return false;
cf[i+]+=need;
if(i+*r<n) cf[i+*r+]-=need;
}
}
return true;
} int main()
{
read(n); read(r); read(k);
int x;
for(int i=;i<=n;++i)
{
read(x);
sum[max(,i-r)]+=x;
if(i+r<n) sum[i+r+]-=x;
}
for(int i=;i<=n;++i) sum[i]+=sum[i-];
LL l=,r=2e18,mid,ans=;
while(l<=r)
{
mid=l+r>>;
if(check(mid)) ans=mid,l=mid+;
else r=mid-;
}
cout<<ans;
}
Codeforces 954 G. Castle Defense的更多相关文章
- 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 ...
- Educational Codeforces Round 40 (Rated for Div. 2) 954G G. Castle Defense
题 OvO http://codeforces.com/contest/954/problem/G 解 二分答案, 对于每个二分的答案值 ANS,判断这个答案是否可行. 记 s 数组为题目中描述的 a ...
- [codeforces 549]G. Happy Line
[codeforces 549]G. Happy Line 试题描述 Do you like summer? Residents of Berland do. They especially love ...
- CodeForces 794 G.Replace All
CodeForces 794 G.Replace All 解题思路 首先如果字符串 \(A, B\) 没有匹配,那么二元组 \((S, T)\) 合法的一个必要条件是存在正整数对 \((x,y)\), ...
- Codeforces 1207 G. Indie Album
Codeforces 1207 G. Indie Album 解题思路 离线下来用SAM或者AC自动机就是一个单点加子树求和,套个树状数组就好了,因为这个题广义SAM不能存在 \(len[u] = l ...
- codeforces 659 G. Fence Divercity 组合数学 dp
http://codeforces.com/problemset/problem/659/G 思路: f(i,0/1,0/1) 表示到了第i个,要被切的块开始了没有,结束了没有的状态的方案数 递推看代 ...
- Codeforces 803 G. Periodic RMQ Problem
题目链接:http://codeforces.com/problemset/problem/803/G 大致就是线段树动态开节点. 然后考虑到如果一个点还没有出现过,那么这个点显然未被修改,就将这个点 ...
- Codeforces 954 E. Water Taps
http://codeforces.com/problemset/problem/954/E 式子变成Σ xi*(ti-T)=0 sum0表示>=T的ai*ti之和 sum1表示<T的ai ...
- Codeforces 746 G. New Roads
题目链接:http://codeforces.com/contest/746/problem/G mamaya,不知道YY了一个什么做法就这样过去了啊 2333 首先我显然可以随便构造出一棵树满足他所 ...
随机推荐
- Docker for windows 入门二(Kitematic的使用)
Kitematic下载地址:https://download.docker.com/kitematic/Kitematic-Windows.zip 下载Kitematic,解压后运行,可以登录连接Do ...
- Using svn in CLI with Batch
del %~n0.txt@echo offsetlocal EnableDelayedExpansionfor /f "delims=" %%i in ('DIR /A:D /B' ...
- [转帖]什么是安全散列算法SHA256?
什么是安全散列算法SHA256? http://8btc.com/article-136-1.html 发布者: 无主之地 发表于: 2013-10-1 01:31 78152 2 分享 安全散列 ...
- Angular $interval
<!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...
- 【Java】 枚举类
如果要定义一个枚举类: public enum Size { SAMLL, MEDIUM, LARGE, EXTRA, EXTRA_LARGE}; 实际上,这个声明定义的类型是一个类,它刚好有4个实例 ...
- js screen
windows.screen對象包含包含對象屏幕的信息: screen.availheight;屏幕高度 screen.availwidth;屏幕寬度
- Essential Phone PH1原生系统常见问题以及解答
*首先声明原生系统使用Google才是最好的体验. **如果下述问题有更好的解决方法,欢迎各种方式私信我. Q:为什么我的wifi显示无法连接到网络?为什么wifi有x号?如何去掉? A:因为原生系统 ...
- entity framework 缓存干扰的数据不一致问题
本来EF的设计上DbContext就应该是一个短暂存在的对象,所以它默认才开了缓存和自动检测对象修改之类的功能,而且不是线程安全的.理想的生命周期应该是处理一次请求使用一个context,全局使用会带 ...
- Layui_1.0.9_分页实例_Java
一.实体 package com.ebd.application.modules.taskManage.pojo; import com.ebd.application.common.Base.Bas ...
- BZOJ2069 POI2004ZAW(最短路)
显然这样的路径一定是选择了与1相邻的不同的两点分别作为起点和终点(除1本身).如果能将每一组起点终点都计算到就可以得出最优解了.暴力显然不行.注意到我们每次求出的是单源最短路径,考虑如何充分利用信息. ...