单调栈

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. kendo ui 好用的小部件--grid

    Kendo Ui Grid控件,继承至Widget. https://demos.telerik.com/kendo-ui/grid/index  快速上手教程  下面的代码来自本教程 做表格时非常方 ...

  2. ArtiPub:一款开源的一文多发平台

    文章来自我的博客:https://blog.ljyngup.com/archives/705.html/ 看到感觉挺有意思的,有空找个空闲的VPS搭建一下. 转自官方Github仓库 ArtiPub ...

  3. node.js+express+mongoose实现用户增删查改案例

    node.js+express+mongodb对用户进行增删查改 一.用到的相关技术 使用 Node.js 的 express 框架搭建web服务 使用 express 中间件 body-parse ...

  4. docker:搭建ELK 开源日志分析系统

    ELK 是由三部分组成的一套日志分析系统, Elasticsearch: 基于json分析搜索引擎,Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片 ...

  5. 浅谈.NET中AppDomain的理解

    一.走进.NET AppDomain 天哪,.NET Framwork的CLR真是巧妙呢!随着越来越多的对.Net底层编程的了解,一些诸如架构,处理过程的复杂难懂的细节完全的让我叹服,所以呢,再次错过 ...

  6. Navicat 导入数据时报Incorrect datetime value: '0000-00-00 00:00:00.000000' 错误

    今天重装了个系统,装好MySQL和Navicat之后导入SQL时报了Incorrect datetime value: '0000-00-00 00:00:00.000000' 错误, 查了资料说是m ...

  7. 注销Apache

    到D:\phpTools\Apache24\bin下运行cmd 输入httpd.exe -k uninstall -n apache24 回车后提示注销完成 接着把Apache的文件删了即可

  8. 使用Java, AppleScript对晓黑板进行自动打卡

    使用Java, AppleScript对晓黑板进行自动打卡 由于我们学校要求每天7点起床打卡,但是实在做不到,遂写了这个脚本. 绪论 由于晓黑板不支持网页版,只能使用App进行打卡,所以我使用网易的安 ...

  9. Centos7.5中Nginx报错:nginx: [error] invalid PID number "" in "/run/nginx.pid" 解决方法

    服务器重启之后,执行 nginx -t 是OK的,然而在执行 nginx -s reload 的时候报错 nginx: [error] invalid PID number "" ...

  10. 搭建fastdfs文件服务器

    一.安装FastDFS环境 1.跟踪服务器(Tracker Server) tracker1:192.168.2.134 tracker2:192.168.2.135 2.存储服务器(Storage ...