单调栈

RMQ

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<ctime>
#include<ctype.h>
#include<bitset>
#include<algorithm>
#include<numeric> //accumulate
#define endl "\n"
#define fi first
#define se second
#define FOR(i,s,t) for(int i=(s);i<=(t);++i)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=+;
long long n,a;
int d[maxn],c[maxn];
long long pref[maxn];
int stc[maxn],top;
int l[maxn],r[maxn];
long long f1[maxn][];
long long f2[maxn][];
void rmq_init()
{
for(int i=; i<=n; i++)
f1[i][]=f2[i][]=pref[i];
for(int j=; (<<j)<=n; j++)
{
for(int i=; i+(<<j)-<=n; i++)
{
f1[i][j]=min(f1[i][j-],f1[i+(<<(j-))][j-]);
f2[i][j]=max(f2[i][j-],f2[i+(<<(j-))][j-]);
}
}
}
long long rmq_query(int l,int r,int ty)
{
int k=;
while(<<(k+)<=r-l+)
k++;
if(ty==)
return min(f1[l][k],f1[r-(<<k)+][k]);
else
return max(f2[l][k],f2[r-(<<k)+][k]); }
int main()
{ //cin.tie(0);
//cout.tie(0);
//ios_base::sync_with_stdio(false);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
cin>>n>>a;
for(int i=; i<=n; i++)
{
cin>>d[i]>>c[i];
}
for(int i=n; i; i--)
{
d[i]=d[i]-d[i-];
}
top=;
d[n+]=1e9;
for(int i=; i<=n+; i++)
{
while(top&&d[stc[top]]<d[i])
r[stc[top--]]=i-;
stc[++top]=i;
}
d[]=1e9;
top=;
for(int i=n; i; i--)
{
while(top&&d[stc[top]]<d[i])
l[stc[top--]]=i+;
stc[++top]=i;
}
//for(int i=2;i<=n;i++)
//cout<<l[i]<<' '<<r[i]<<endl;
for(int i=; i<=n; i++)
{
pref[i]=pref[i-]+a-c[i];
}
rmq_init();
//cout<<rmq_query(1,n,0)<<endl;
long long ans=;
for(int i=; i<=n; i++)
{
long long mi;
if(i==) mi=;
else if(l[i]==) mi=min(0ll,rmq_query(,i-,));
else mi=rmq_query(l[i]-,i-,);
long long mx=rmq_query(i,r[i],);
ans=max(ans,mx-mi-1ll*d[i]*d[i]);
}
for(int i=; i<=n; i++)
{
ans=max(ans,1ll*a-c[i]);
}
cout<<ans;
} /*
void read()
{ char c = getchar();
int x = 0;
for (; (c < 48 || c>57); c = getchar());
for (; c > 47 && c < 58; c = getchar())
{
x = (x << 1) + (x << 3) + c - 48;
}
return x;
}
*/

codeforceCodeForces - 1107G的更多相关文章

  1. [Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]

    咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\( ...

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

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

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

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

  4. Codeforces 1107G(dp)

    1.答案要取连续的区间疯狂暗示线段树. 2.外层枚举r,内层枚举l显然过于暴力. 3.考虑内层的优化.dp[i]:以第i位为结尾的答案(长度大于1的).dp[i] = max(第一种情况,第二种情况) ...

随机推荐

  1. postman之存储测试结果

    前言 在Jmeter的随笔中,我跟大家讲过利用Jmeter工具存储测试结果,那么,postman工具要该如何存储测试结果呢?下面一起来学习吧! 一:添加一个登录请求,填入接口参数点击send 二:点击 ...

  2. Mac设置Linux免密登陆

    利用公钥认证登录 1.创建共钥 输入下面的命令,一路回车 ssh-keygen -t rsa 2.复制公钥到ssh服务器 将上一步生成的id_rsa.pub公钥文件复制到目标服务器对应用户下的~/.s ...

  3. 使用chrome控制台调试js代码

    1.打开控制台(空白页签即可) 2.将控制台在独立页打开(点击控制台右上角的三个点图标,然后点击如下图中的图标) 3.创建脚本编辑页面 4.页面区域说明 5.花键+回车之行代码 6.常用命令介绍 6. ...

  4. Cacti 升级

    现在用的 cacti 1.0.3   决定升级一下cacti到最新版本 1.1.1   官方升级指导文件 Upgrading Cacti Backup the old Cacti database. ...

  5. Android 7.0新特性“Nougat”(牛轧糖)。

    1.Unicode 9支持和全新的emoji表情符号 Android Nougat将会支持Unicode 9,并且会新增大约70种emoji表情符号.这些表情符号大多数都是人形的,并且提供不同的肤色, ...

  6. linux中的挂载命令

    一.查询与自动挂载 查询系统中已经挂载的设备,-l会显示卷标名称 mount [-l] oot@izm5e2q95pbpe1hh0kkwoiz tmp]# mount sysfs on /sys ty ...

  7. clr via c# 程序集加载和反射(2)

    查看,clr via c# 程序集加载和反射(1) 8,发现类型的成员: 字段,构造器,方法,属性,事件,嵌套类型都可以作为类型成员.其包含在抽象类MemberInfo中,封装了所有类型都有的一组属性 ...

  8. ASP.NET Core MVC 网站学习笔记

    ASP.NET Core MVC 网站学习笔记 魏刘宏 2020 年 2 月 17 日 最近因为” 新冠” 疫情在家办公,学习了 ASP.NET Core MVC 网站的一些知识,记录如下. 一.新建 ...

  9. 微信小程序如何下载超过大小限制(10M)的视频?(苹果用户仔细看,安卓用户快速看)

    众所周知,微信小程序对下载的文件大小有限制,目前是最大支持10M.我们在用去水印小程序保存视频的时候,如果遇到长视频,视频大小可能就超过限制.遇到这种情况,我们如何才能把视频保存到手机相册呢? 首先, ...

  10. mysql必知必会--用正则表达式 进行搜索

    正则表达式介绍 前两章中的过滤例子允许用匹配.比较和通配操作符寻找数据.对 于基本的过滤(或者甚至是某些不那么基本的过滤),这样就足够了.但 随着过滤条件的复杂性的增加, WHERE 子句本身的复杂性 ...