codeforceCodeForces - 1107G
单调栈
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的更多相关文章
- [Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]
咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\( ...
- 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]\)时那个奇怪东西的 ...
- Codeforces 1107G(dp)
1.答案要取连续的区间疯狂暗示线段树. 2.外层枚举r,内层枚举l显然过于暴力. 3.考虑内层的优化.dp[i]:以第i位为结尾的答案(长度大于1的).dp[i] = max(第一种情况,第二种情况) ...
随机推荐
- HTML5与HTML4的区别-----文档结构
HTML5在结构和语法上做了大量的简化.当然,也提供了语义化的标签 结构上区别: 1.简化了文档声明语句 HTML5仅规定了一种: <!DOCTYPE html> 2. ...
- javascript json语句 与 js语句的互转
//var data = "weihexin" //var data = ["weihexin", 1] var data = {name:"weih ...
- 票据传递之MS14-068
MS14-068是密钥分发中心(KDC)服务中的Windows漏洞.它允许经过身份验证的用户在其Kerberos票证(TGT)中插入任意PAC(表示所有用户权限的结构).该漏洞位于kdcsvc.dll ...
- space transport protocols
VSAT系统对TCP的改进 https://www.vsat-systems.com/broadband-satellite-internet/index.html TCP/IP over satel ...
- 使用matplotlib画图
一.介绍 官方文档:https://www.matplotlib.org.cn/home.html 安装:pip install matplotlib Matplotlib是一个Python 2D绘图 ...
- Apache httpd.conf配置文件 3(虚拟主机)
### Section 3: Virtual Hosts 第三部分 虚拟主机 注意:在使用虚拟主机前,请先检查 http.conf 的 辅助配置文件httpd-vhosts.conf 是否注释 # ...
- oracle数据库重要的查询语句
查看所有数据文件(dbf文件)的存放位置 SQL> select name from v$datafile; 标红色的为默认表空间文件 SQL> select name from v$da ...
- java架构之路-(dubbo专题)dubbo的基本使用
今天我们来说一下dubbo的基本使用,没有什么高阶的知识,真的超级简单易懂. Dubbo核心功能解释 dubbo 阿里开源的一个SOA服务治理框架,从目前来看把它称作是一个RPC远程调用框架更为贴切. ...
- 秘钥分割-Shamir秘钥分割门限方案
精选: 1.问题的提出 2.需求的抽象: 有一个秘钥S,转换成另一种数据数据形式,分配给12个人(s1,s2,.......,s12),使得任意3个人的数据拼凑在一起就可以反向计算出秘钥S. 3.解决 ...
- .net平台 通过Windows installer 打包和部署实例操作
Windows installer 打包和部署 1.新建项目. 打开VS,新建项目->其他项目类型->安装和部署(Visual Studio Installer). 注:若VS2013 或 ...