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

题目连接: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. 20175315Mycp

    MyCP(课下作业,必做) 要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bin ...

  2. 20175315 《Java程序设计》第6周学习总结

    20175215 <Java程序设计>第6周学习总结 教材学习内容总结 第七章主要讲的是内部类,匿名类,异常类等等. 内部类:Java支持在一个类中定义另一个类,称作内部类,包含内部类的类 ...

  3. 论文学习笔记--无缺陷样本产品表面缺陷检测 A Surface Defect Detection Method Based on Positive Samples

    文章下载地址:A Surface Defect Detection Method Based on Positive Samples 第一部分  论文中文翻译 摘要:基于机器视觉的表面缺陷检测和分类可 ...

  4. 使用Setup factory打包WPF

    软件环境 Win10 .NET452 WPF Setup Factory 工具直接百度下啦,关键词:Setup Factory 95 With Sn 打包过程主要参考了以下文章: https://ww ...

  5. jmeter创建时间函数

    固定格式的年月日 ${__time(yyyyMMdd,)} 20151214 //返回年月日 ${__time(HHmmss,)} 092816 //返回时分秒 ${__time(yyyyMMdd-H ...

  6. UltraEdit注册机 及使用方法详解

    转载自:http://www.iyaxi.com/ultraedit-key/ UltraEdit是一款强大的文字编辑器,很多编程的.搞设计的等等都能用到它,具体功能请自行百度.今天为大家带来UE软件 ...

  7. 前端开发【第1篇:HTML】

    HTML初识 1.什么是HTML HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都 ...

  8. pg数据库查询表大小

    查询单个表 select pg_size_pretty(pg_relation_size('table_name')); 按size大小排序列出所有表 SELECT table_schema || ' ...

  9. OrchardCore 如何实现模块化( Modular )和 Multi-Tenancy

    一.概述 通常我们会在 Startup 类通过 void ConfigureServices(IServiceCollection services) 配置应用的服务.常见的形如 AddXXX 的方法 ...

  10. Codeforces 439E Devu and Birthday Celebration 容斥

    Devu and Birthday Celebration 我们发现不合法的整除因子在 m 的因子里面, 然后枚举m的因子暴力容斥, 或者用莫比乌斯系数容斥. #include<bits/std ...