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(第一种情况,第二种情况) ...
随机推荐
- SpringBoot之ApplicationRunner接口和@Order注解
我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了ApplicationRunner接口来帮助我们实现这种需求.该接 ...
- 将STM32F407片外SRAM作运行内存
本例演示用的软硬件: 片内外设驱动库:STM32CubeF41.24.1的HAL库1.7.6,2019年4月12日 IDE:MDK-ARM 5.28.0.0,2019年5月 开发板:片外SRAM挂在F ...
- JAVA ReentrantLock的使用
源码如下 对比synchronized,synchronized使用时会显示的指定一个对象(方法为调用对象,代码块会需要对象作为参数),来获取一个对象的独占锁 而ReentrantLock可能就是使用 ...
- 大延时情况tcp和udp测试
环境搭建 使能Ubuntu的IPv6转发功能 root@yanhc-Aspire-4738G:/home/yanhc# cat /proc/sys/net/ipv4/ip_forward root@y ...
- onboard procossor and cross-compile
星载处理器 ERC32-TSC695F The European Space Agency’s ERC32 is a microprocessor implementing a SPARC V7 pr ...
- Mysql 初始化 及 密码管理
安装好mysql后,第一次初始化数据库 前言:启动mysql数据库最好不要使用root用户,而是使用mysql用户启动 官方解释: (永远不要使用root帐号启动MySQL Server.这样做很危险 ...
- [jQuery]jQuery和DOM对象互换(四)
DOM 和 jQuery 相互转换 DOM 转jQuery $(DOM对象) # (1)直接获取 $('video'); # (2)转换 $(DOM对象) var myVideo = document ...
- Elasticsearch之集群
ElasticSearch集群 ES集群是一个 P2P类型(使用 gossip 协议)的分布式系统,除了集群状态管理以外,其他所有的请求都可以发送到集群内任意一台节点上,这个节点可以自己找到需要转发给 ...
- 与WinRT组件进行操作
1,原理: WinRT是一个新的类库,应用程序可以用它访问操作系统的功能. 在内部,WinRT以组件的形式实现.COM Component Object Model- WinRT使用.net元数据来描 ...
- 正规表达式与有限自动机和LEX
正规式与有限自动机的等价性 一个正规式r与一个有限自动机M等价, L(r)=L(M) FA ->正规式,对任何FA M,都存在一个正规式r,使得L(r)=L(M). 正规式 -> FA, ...