Codeforces 1107G Vasya and Maximum Profit [单调栈]
我竟然能在有生之年踩标算。
思路
首先考虑暴力:枚举左右端点直接计算。
考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的平方为\(f(l,r)\),使劲推式子:
ans_{l,r}+l\times a-a-sum_{l-1}=r\times a-sum_r-f(l,r)\\
ans_{l,r}+l\times a-a-sum_{l-1}=F_r-(\max_{i=l+1}^r\{d_{i}-d_{i-1}\})^2
\]
其中\(F_r=r\times a-sum_r\)。
发现左边只和\(l\)有关,所以可以考虑枚举\(l\),用数据结构维护右边的最大值。
然而右边有一个\(\max\)较为麻烦,怎样在移动左端点时快速更新呢?
考虑\(\max\)在左端点一定时单调不降,所以左端点每次往左移一格,只会对连续的一部分\(r\)造成影响,将他们的\(\max\)弄成一样的。
既然一样了,那就可以记录一个\(\max\{F_r\}\),然后把它们并在一起。
用一个单调栈记录\(r\),每次\(l\)往左移一位,就把一堆满足\(\max<d_{l+1}-d_l\)的\(r\)缩在一起,记录它们的\(\max \{F_r\}\)。
栈里每个元素也要存自己右边的\(F_r-(\max_{i=l+1}^r\{d_{i}-d_{i-1}\})^2\)的最大值,用来统计答案。
复杂度显然是\(O(n)\)的。
#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define templ template<typename T>
#define sz 303030
typedef long long ll;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ inline void read(T& t)
{
t=0;char f=0,ch=getchar();double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
t=(f?-t:t);
}
template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
inline void print(register int x)
{
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
void file()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
}
inline void chktime()
{
#ifndef ONLINE_JUDGE
cout<<(clock()-t)/1000.0<<'\n';
#endif
}
#ifdef mod
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
ll inv(ll x){return ksm(x,mod-2);}
#else
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
#endif
// inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;
int n;ll a;
ll sum[sz];
ll d[sz];
ll F[sz];
struct hh
{
ll f;
ll mx;
ll mxr;
}s[sz];
int top;
inline ll sq(ll x){return x*x;}
int main()
{
file();
read(n,a);
ll x;
rep(i,1,n) read(d[i],x),sum[i]=sum[i-1]+x,F[i]=a*i-sum[i];
ll ans=max(a-sum[n]+sum[n-1],0ll);
s[++top]=(hh){F[n],0,F[n]};
s[0].mxr=-1e15;
drep(i,n-1,1)
{
ll mxF=-1e15;
while (top&&s[top].mx<=d[i+1]-d[i]) chkmax(mxF,s[top].f),s[top]=(hh){0,0,0},--top;
if (mxF!=-1e15) {++top;s[top]=(hh){mxF,d[i+1]-d[i],max(mxF-sq(d[i+1]-d[i]),s[top-1].mxr)};}
++top;s[top]=(hh){F[i],0,max(F[i],s[top-1].mxr)};
chkmax(ans,s[top].mxr-a*i+a+sum[i-1]);
}
cout<<ans;
return 0;
}
Codeforces 1107G Vasya and Maximum Profit [单调栈]的更多相关文章
- [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 1107 - G Vasya and Maximum Profit 线段树
题目传送门 题解: 枚举 r 的位置. 线段树每个叶子节点存的是对应的位置到当前位置的价值. 每次往右边移动一个r的话,那么改变的信息有2个信息: 1. sum(a-ci) 2.gap(l, r) 对 ...
- Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次 ...
- Educational Codeforces Round 23 D. Imbalanced Array 单调栈
D. Imbalanced Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 817 D. Imbalanced Array(单调栈+思维)
题目链接:http://codeforces.com/contest/817/problem/D 题意:给你n个数a[1..n]定义连续子段imbalance值为最大值和最小值的差,要你求这个数组的i ...
- codeforces1107G Vasya and Maximum Profit 【模拟】
题目分析: 前缀和啥的模拟一下就行了. 代码: #include<bits/stdc++.h> using namespace std; ; int n,x,d[maxn],sta[max ...
- Codeforces gym 100971 D. Laying Cables 单调栈
D. Laying Cables time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Maximum Xor Secondary CodeForces - 281D (单调栈)
Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...
随机推荐
- NPOI导出Excel2007-xlsx
今天在用npoi导出xls时会报错,经过在网上查找资料,找到一篇博客文章介绍的,原文地址https://www.cnblogs.com/spring_wang/p/3160020.html 1.今天再 ...
- DosBox 的 DOSBOX.CONF 的详细配置说
1.首先下载 DOSbox 0.72 版. 2.下载完毕,开始安装.安装到任意目录均可.安装完毕会在开始菜单生成程序组,DOSBox.conf 文件是 DOSbox 的配置文件,保持默认配置就可 ...
- Python中json一点小知识
import json dic={ "name":"杨林" } ret=json.dumps(dic,ensure_ascii=False) #因为json.d ...
- 微信小程序开发(5) 2048游戏
在这篇微信小程序开发教程中,我们将介绍如何使用微信小程序开发2048小游戏. 本文主要分为两个部分,小程序主体部分及小游戏页面部分 一.小程序主体部分 一个小程序主体部分由三个文件组成,必须放在项目的 ...
- MySQL api
今天看去年年中写的代码,留意到一个关键时刻能提高效率的api:on duplicate key update: 语法: INSERT INTO INSERT INTO g_iot_user_build ...
- Coursera, Deep Learning 1, Neural Networks and Deep Learning - week4, Deep Neural Networks
Deep Neural Network Getting your matrix dimention right 选hyper-pamameter 完全是凭经验 补充阅读: cost 函数的计算公式: ...
- python中的Process
from multiprocessing import Process import time import os # # def acb(n): # print(n) # # # if __name ...
- 标准盒模型、IE盒模型
结论:IE盒模型是陈旧知识点,除了帮助理解css3 box-sizing: border-box(等分宽度布局)外没什么用. 标准(W3C)模型中:CSS中的宽(width) = 内容 (conten ...
- spring4 注入参数
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring重温(二)--Spring JavaConfig
1.从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中.但是,仍然允许使用经典的XML方式来定义bea ...