[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) 对 ...
随机推荐
- hihocoder 1175
拓扑排序 hihocoder 1175 拓扑只适用于 有向无环图中,这个指的是 1.有向的,不是那种双向可走的 2.无环,并不是不存在环,而是一定要有一个没有其他点指向这个点的点, 题目大意:一个有向 ...
- swoole异步群发模板消息
1.用的是TP5.1的框架,swoole分成一个客户端发送接收消息,一个服务器负责处理信息 服务端代码,服务器要先安装swoole拓展,用 php server.php 启动进程监听 <?php ...
- java - day003 - 循环嵌套, 循环命名, while, 数组
1.循环嵌套 break 中断循环或switch(跳出循环).中断后继续往下执行 continue (跳到循环的下一轮继续执行) return (结束方法) 2.循环命名 内层循环控制外层循环.需要给 ...
- 初识C语言 (四)
分支结构 if语句 C语言中的分支结构语句中的if条件语句,简单if语句的基本结构如下: 其语义是:如果表达式的值为真,则执行其后的语句,否则不执行该语句. 其过程可表示为下图 实例: if(resu ...
- JAVA第三次实训作业
---恢复内容开始--- 1. 编写“学生”类及其测试类. “学生”类: 类名:Student 属性:姓名.性别.年龄.学号.5门课程的成绩 方法1:在控制台输出各个属性的值. 方法2:计算平均成绩 ...
- Docker commit 命令保存的镜像文件太大的问题
基础镜像: centos7.5 进入容器后, 先后安装了 jdk1.8, maven3.6.0, git, rocketmq4.3.2 安装完成后使用 docker commit 命令保存为镜像 结果 ...
- menuStrip1动态添加菜单及快捷键
public partial class FormMkTest : Form { public FormMkTest() { InitializeComponent(); } private void ...
- C. mathematican 的二进制
题解: subtask1:爆搜,没状压dp的分 subtask2,3:统计有几个操作就好了,分治ntt优化 subtask4,5: 发现和操作顺序无关,然后我们考虑用分治ntt优化 每个的贡献是$n+ ...
- python调用函数超时设置
1.Windows中sign报错,Linux能很好的使用: https://pypi.python.org/pypi/timeout-decorator 2.Windows可以使用,Linux报错不能 ...
- 开发自己的react-native组件并发布到npm[转]
原文链接:https://www.jianshu.com/p/091a68ea1ca7 写在前面 在做react-native开发的时候,我们经常会找到一些第三方组件,并且通过npm install的 ...