洛谷

Codeforces


我竟然能在有生之年踩标算。


思路

首先考虑暴力:枚举左右端点直接计算。

考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的平方为\(f(l,r)\),使劲推式子:

\[ans_{l,r}=(r-l+1)\times a-sum_r+sum_{l-1}-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 [单调栈]的更多相关文章

  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 1107 - G Vasya and Maximum Profit 线段树

    题目传送门 题解: 枚举 r 的位置. 线段树每个叶子节点存的是对应的位置到当前位置的价值. 每次往右边移动一个r的话,那么改变的信息有2个信息: 1. sum(a-ci) 2.gap(l, r) 对 ...

  4. Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次 ...

  5. Educational Codeforces Round 23 D. Imbalanced Array 单调栈

    D. Imbalanced Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. codeforces 817 D. Imbalanced Array(单调栈+思维)

    题目链接:http://codeforces.com/contest/817/problem/D 题意:给你n个数a[1..n]定义连续子段imbalance值为最大值和最小值的差,要你求这个数组的i ...

  7. codeforces1107G Vasya and Maximum Profit 【模拟】

    题目分析: 前缀和啥的模拟一下就行了. 代码: #include<bits/stdc++.h> using namespace std; ; int n,x,d[maxn],sta[max ...

  8. Codeforces gym 100971 D. Laying Cables 单调栈

    D. Laying Cables time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. Maximum Xor Secondary CodeForces - 281D (单调栈)

    Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...

随机推荐

  1. 比较好用的Copy代码到博客VS扩展工具

    这个工具叫做TrimCopy,可以避免拷贝出来的代码带空格,影响布局.

  2. 多态(upcast)减少分支判断 以及 多态继承设计、具体类型判断。

    Influenced by <java 八荣八耻>,翻了下<java编程思想> 印象中多态产生的条件:1.子类继承父类 2.父类[指针]指向子类 3.父类引用调用重写(@Ove ...

  3. 【51nod 1785】数据流中的算法

    Description 51nod近日上线了用户满意度检测工具,使用高级人工智能算法,通过用户访问时间.鼠标轨迹等特征计算用户对于网站的满意程度.   现有的统计工具只能统计某一个窗口中,用户的满意程 ...

  4. 最小生成树入门(克鲁斯卡尔+普利姆 hdu1233)

    克鲁斯卡尔 #include <set> #include <map> #include <queue> #include <stack> #inclu ...

  5. 小程序开发-Step1

    先申请一个小程序 https://mp.weixin.qq.com/wxopen/waregister?action=step1 根据以上链接步骤一步一步来,认识字就可以完成,没什么特殊的 申请成功之 ...

  6. Can not deserialize instance of xxx out of START_ARRAY token

    Json 反序列化异常 Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableEx ...

  7. mysql 原理~ FTWRDL

    FTWRL 锁与MDL一 简介:今天来聊聊为什么备份会卡住,申请不到全局FTWRL二 FTWRL 1 FTWRL主要包括3个步骤:        1.上全局读锁(lock_global_read_lo ...

  8. mysql 案例 ~ 表空间迁移数据与数据导入

    一  简介:mysql5.6+的表空间传输二 目的:复制数据到另一个表三 步骤   1 create table b like a ->创建一个空表   2 alter table b disc ...

  9. python编程 之 json包

    1,json是什么? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. 我的理解就是:json是一种统一的格式化的文件,比如,一个jso ...

  10. java的引用

    一.值类型与引用类型 1.变量初始化 int num = 10; String str = "hello"; num是int基本类型变量,值就直接保存在变量中.str是String ...