单调栈

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. php单例模式的实现

    <?php /** * 设计模式之单例模式 * $_instance必须声明为静态的私有变量 * 构造函数和析构函数必须声明为私有,防止外部程序new * 类从而失去单例模式的意义 * getI ...

  2. debian 和ubuntu 安装ifconfig 命令

    # apt update # apt install net-tools

  3. Mac解决:xcode-select: error: command line tools are already installed, use "Software Update" to install updates

    1.因为node项目终端报错: No receipt for 'com.apple.pkg.CLTools_Executables' found at '/'. No receipt for 'com ...

  4. 北京智和信通IT运维管理系统二次开发服务提供商

    随着云计算.大数据.物联网.移动互联网.人工智能.5G等高新技术的快速发展,数据中心及网络基础设施呈现出井喷式的增长模式,对设备商来说,多.快.好.省的实现定制化网络管理开发,可极大的扩充设备适用范围 ...

  5. codewars--js--Simple string expansion+ repeat(),includes()方法

    问题描述: Consider the following expansion: solve("3(ab)") = "ababab" -- "ab&qu ...

  6. 盒模型大小取决于它的padding,margin,border数值

    盒模型规定了元素框处理元素内容width与height值.内边距padding.边框border 和 外边距margin 的数值大小.边框内的空白是内边距padding,边框外的空白是外边距margi ...

  7. 剑指offer-面试题66-构建乘积数组-发散思维

    /* 题目: 链接:https://www.nowcoder.com/questionTerminal/94a4d381a68b47b7a8bed86f2975db46 来源:牛客网 给定一个数组A[ ...

  8. proptypes介绍

    开始 prop-types的主要作用:对props中数据类型进行检测及限制 引用方法:import PropTypes from 'prop-types' 用法: // 基本用法 用来检测数据类型 c ...

  9. PHP0025:PHP 博客项目开发2

  10. 00.JS前言

    前言: 学习一门编程语言的基本步骤(01)了解背景知识 1.了解背景知识   1)什么是 JavaScript 语言?     JavaScript 是一种轻量级的脚本语言.所谓“脚本语言”(scri ...