咸鱼了好久...出来冒个泡_(:з」∠)_

题目连接: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]的更多相关文章

  1. Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈

    Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...

  2. Codeforces 1107G Vasya and Maximum Profit [单调栈]

    洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...

  3. [Educational Round 5][Codeforces 616F. Expensive Strings]

    这题调得我心疲力竭...Educational Round 5就过一段时间再发了_(:з」∠)_ 先后找了三份AC代码对拍,结果有两份都会在某些数据上出点问题...这场的数据有点水啊_(:з」∠)_[ ...

  4. [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]

    这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...

  5. [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]

    这题拖了快一周_(:з」∠)_就把这货单独拿出来溜溜吧~ 本文归属:Educational Codeforces Round 3 题目链接:609F - Frogs and mosquitoes 题目 ...

  6. [Educational Round 17][Codeforces 762F. Tree nesting]

    题目连接:678F - Lena and Queries 题目大意:给出两个树\(S,T\),问\(S\)中有多少连通子图与\(T\)同构.\(|S|\leq 1000,|T|\leq 12\) 题解 ...

  7. [Educational Round 13][Codeforces 678F. Lena and Queries]

    题目连接:678F - Lena and Queries 题目大意:要求对一个点集实现二维点对的插入,删除,以及询问\(q\):求\(max(x\cdot q+y)\) 题解:对每个点集内的点\(P( ...

  8. [Educational Round 10][Codeforces 652F. Ants on a Circle]

    题目连接:652F - Ants on a Circle 题目大意:\(n\)个蚂蚁在一个大小为\(m\)的圆上,每个蚂蚁有他的初始位置及初始面向,每个单位时间蚂蚁会朝着当前面向移动一个单位长度,在遇 ...

  9. CodeForces 1107 - G Vasya and Maximum Profit 线段树

    题目传送门 题解: 枚举 r 的位置. 线段树每个叶子节点存的是对应的位置到当前位置的价值. 每次往右边移动一个r的话,那么改变的信息有2个信息: 1. sum(a-ci) 2.gap(l, r) 对 ...

随机推荐

  1. Linux中查看端口占用情况

    1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口的使用情况: # lsof -i:8000 2.netstat -tunlp | grep 端口号,用于查看指定的端口号的进 ...

  2. MDX Query - mdx的基本语法和概念

    文档: https://wenku.baidu.com/view/ef14b0e1900ef12d2af90242a8956bec0975a5e7.html?rec_flag=default http ...

  3. L2-010 排座位 (25 分) (最短路)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805066135879680 题目: 布置宴席最微妙的事情,就是给前 ...

  4. Uni-app页面生命周期

    学习任何框架必须将其生命周期了然于胸. uni-app支持如下页面生命周期函数: onLoad 监听页面加载,其参数为上个页面传递的数据,参数类型为object(用于页面传参),示例中可参考 onSh ...

  5. 初识C语言(六)

    数组 程序中需要容器,该容器有点特殊,它在程序中是一块连续的,大小固定并且里面的数据类型一致的内存空间,它的名字叫数组. 声明一个数组: 数据类型 数组名称[长度]; C语言中的数组初始化是有三种形式 ...

  6. JS一直是单线程,异步(定时器,ajax请求等)是由浏览器来实现的!(转)

    原文地址:https://www.cnblogs.com/woodyblog/p/6061671.html 1.自己画的一张示意图 2.DOM一变化,界面就立刻重新渲染,效率必然很低,所以浏览器的机制 ...

  7. Linux下创建共享文件夹

    1,查看ip 地址 ifconifg: 2,查看是否安装samba服务器,rpm -qa | grep samba: 3,如果有该服务器,启动 service smb start,否则进行安装 yum ...

  8. centos/redhat命令行上传下载文件

    前言:客户端上没有安装xftp,winscp等等软件,无法将服务器上需要的文件下载到本地去解析,无法将本地的安装包上传到服务器上去,这个时候命令行就可以带你翱翔一波 配置如下: 服务器上: 1.安装需 ...

  9. iOS webview 获取html中的图片地址

    //js代码,声明1个数组,对img 进行遍历,采用,分割多个url NSString * getAllImages = @"var str = new Array();" &qu ...

  10. kafka单机搭建,并测试api

    所用环境: kafka_2.-.gz centos 6.9 nat动态ip 准备工作: ().将防火墙关闭 service iptables stop 临时关闭 chkconfig iptables ...