[Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]
咸鱼了好久...出来冒个泡_(:з」∠)_
题目连接:1107G - Vasya and Maximum Profit
题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\(n\)的严格单调上升数组\(d_i\),求\(\max\limits_{1 \le l \le r \le n} (a\cdot(r-l+1)-\sum_{i=l}^{r}c_i-gap(l,r))\),其中\(gap(l, r) = \max\limits_{l \le i < r} (d_{i + 1} - d_i)^2\)
题解:首先将所有的\(c_i\)转换为\(a-c_i\),这样就变成了求\(\max\limits_{1 \le l \le r \le n} (\sum_{i=l}^{r}c_i-gap(l,r))\)。如果\(l,r\)确定的话,我们就能通过求前缀和以及区间内最大值来算出该区间对应的答案,但我们还需要进一步的优化。
考虑每一个\(d_{i + 1} - d_i\)能成为\(gap(l,r)\)的范围,即在区间\([L,R]\)中,\(\forall L \le l \le r \le R,gap(l,r)\le d_{i + 1} - d_i\)。这样我们只需要用线段树查询区间\([L,R]\)的最大子段和就能求出当\(gap(l,r) \le d_{i + 1} - d_i\)时的答案。先预处理所有的\(L,R\),再扫一遍就好了。
#include<bits/stdc++.h>
using namespace std;
#define N 300001
#define LL long long
LL n,b,l[N],r[N],a[N],d[N],L,R,M,S,ans;
struct rua{LL l,r,w,s,lw,rw;}t[N<<];
void up(int x,int mid)
{
t[x].s=t[x*].s+t[x*+].s;
t[x].w=max(t[x*].w,t[x*+].w);
t[x].lw=max(t[x*].lw,t[x*].s+t[x*+].lw);
t[x].rw=max(t[x*+].rw,t[x*+].s+t[x*].rw);
t[x].w=max(t[x].w,t[x*].rw+t[x*+].lw);
}
void Build(int l,int r,int x)
{
t[x].l=l,t[x].r=r;
if(l==r){t[x].w=t[x].lw=t[x].rw=t[x].s=a[l];return;}
int mid=l+r>>;
Build(l,mid,x*);
Build(mid+,r,x*+);
up(x,mid);
}
void ask(int ll,int rr,int l,int r,int x)
{
if(ll>r || l>rr)return;
int mid=l+r>>;
if(ll<=l && r<=rr)
{
M=max(M,max(t[x].w,R+t[x].lw));
L=max(L,S+t[x].lw);
R=max(R+t[x].s,t[x].rw);
M=max(M,max(L,R));
S+=t[x].s;
return;
}
ask(ll,rr,l,mid,x*);
ask(ll,rr,mid+,r,x*+);
}
int main()
{
scanf("%I64d%I64d",&n,&b);
for(LL i=;i<=n;i++)
{
scanf("%I64d%I64d",&d[i],&a[i]);
a[i]=b-a[i],ans=max(ans,a[i]);
}
for(LL i=n;i>=;i--)d[i]-=d[i-];
Build(,n,);
d[]=;
l[]=,r[n]=n;
for(LL i=;i<=n;i++)
{
LL _=i;
while(_> && d[i]>=d[_-])
_=l[_-];
l[i]=_;
}
for(LL i=n-;i>=;i--)
{
LL _=i;
while(_<n && d[i]>=d[_+])
_=r[_+];
r[i]=_;
}
for(LL i=;i<=n;i++)
{
S=;
L=R=M=-(1e18);
ask(l[i]-,r[i],,n,);
ans=max(ans,M-d[i]*d[i]);
}
printf("%I64d\n",ans);
return ;
}
[Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]的更多相关文章
- Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈
Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...
- Codeforces 1107G Vasya and Maximum Profit [单调栈]
洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...
- [Educational Round 5][Codeforces 616F. Expensive Strings]
这题调得我心疲力竭...Educational Round 5就过一段时间再发了_(:з」∠)_ 先后找了三份AC代码对拍,结果有两份都会在某些数据上出点问题...这场的数据有点水啊_(:з」∠)_[ ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]
这题拖了快一周_(:з」∠)_就把这货单独拿出来溜溜吧~ 本文归属:Educational Codeforces Round 3 题目链接:609F - Frogs and mosquitoes 题目 ...
- [Educational Round 17][Codeforces 762F. Tree nesting]
题目连接:678F - Lena and Queries 题目大意:给出两个树\(S,T\),问\(S\)中有多少连通子图与\(T\)同构.\(|S|\leq 1000,|T|\leq 12\) 题解 ...
- [Educational Round 13][Codeforces 678F. Lena and Queries]
题目连接:678F - Lena and Queries 题目大意:要求对一个点集实现二维点对的插入,删除,以及询问\(q\):求\(max(x\cdot q+y)\) 题解:对每个点集内的点\(P( ...
- [Educational Round 10][Codeforces 652F. Ants on a Circle]
题目连接:652F - Ants on a Circle 题目大意:\(n\)个蚂蚁在一个大小为\(m\)的圆上,每个蚂蚁有他的初始位置及初始面向,每个单位时间蚂蚁会朝着当前面向移动一个单位长度,在遇 ...
- CodeForces 1107 - G Vasya and Maximum Profit 线段树
题目传送门 题解: 枚举 r 的位置. 线段树每个叶子节点存的是对应的位置到当前位置的价值. 每次往右边移动一个r的话,那么改变的信息有2个信息: 1. sum(a-ci) 2.gap(l, r) 对 ...
随机推荐
- python操作redis命令
Python操作redis from redis import StrictRedis, ConnectionPoolredis_url="redis://:xxxx@112.27.10.1 ...
- Django2.0 和 Django1 中的正则匹配问题
2.0内的path匹配正则时候无效时,导入re_path即可 from django.urls import path,re_path from king_admin import views url ...
- ios访问web页面<div>点击事件不起效果,以及alert()显示url的解决办法
ios访问web页面<div>点击不起效果,在其div上添加style=”cursor:pointer:“ jquery web页面动态append()事件调用方法:$(document) ...
- python+selenium自动测试之WebDriver的常用API(基础篇一)
基于python3.6,selenium3.141,详细资料介绍查看官方API文档,点击这里 一.对浏览器操作 driver = webdriver.Chrome() # 初始化chrome driv ...
- 2018-2019-2 网络对抗技术 20165328 Exp5 MSF基础应用
目录 实验要求: 基础问题回答: 离实战还缺些什么技术或步骤? 实验内容: Exp5-1 主动攻击实践:实现ms08_067(失败) 实现ms17_010_eternalblue(成功) Exp5-2 ...
- idea cpu 卡慢 占用100%
1.修改idea配置文件D:\ideaIU-2017.2\bin\idea.exe.vmoptions 如: -server -Xms800m -Xmx800m -XX:MaxPermSize=512 ...
- 记一次物理机安装centos7.5 出现黑屏的问题
记一次物理机安装centos7.5 出现黑屏的问题 一.问题出现 使用物理机安装centos7.5,选择Install CentOS Linux7之后,并没有出现选择语言的界面,而是只出现了一个鼠标, ...
- Django by example -----1总结
根据django by example 完成了第一个例子,总结如下. 第一: django的orm真的很方便,避免了sql语句的使用,你所需要的,django基本都已经封装好了,一些字段很有意思. 第 ...
- SEH exception with code 0xc0000005 thrown in the test body
在用Visual Studio时遇到这个报错.原因:访问了非法的内存地址. 这个问题不应该被忽略,通常是代码有bug. 解决办法: VS2013: 菜单->Debug->Exception ...
- spring boot 2.0 配置双数据源 MySQL 和 SqlServer
参考:https://www.cnblogs.com/xiaofengfeng/p/9552816.html 安装 org.mybatis.spring.boot:mybatis-spring-boo ...