P3299 [SDOI2013]保护出题人
2024-09-08 12:22:12
全世界都会二分可海星……
首先记\(sum[i]\)为\(a[i]\)的前缀和,那么第\(i\)个的答案就是\(max\{\frac{sum[i]-sum[j-1]}{x+(i-j)d}\}\),那么我们可以把式子给看做点\((j*d,sum[j-1])\)和\((x+i*d,sum[i])\)的斜率。发现前面那个是一个定值,于是我们可以维护一个下凸包,因为凸包上的斜率单调增,每一次在这个凸包上二分最大的斜率即可
//minamoto
#include<bits/stdc++.h>
#define fp(i,a,b) for(register int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(register int i=a,I=b-1;i>I;--i)
#define ll long long
#define eps 1e-3
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
ll read(){
ll res,f=1;char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
const int N=1e5+5;
int n,top,l,r,mid,ret;ll d,a[N],sum[N],x[N];double ans;
struct node{ll x,y;}st[N],res;
inline double slope(const node &a,const node &b){return 1.0*(b.y-a.y)/(b.x-a.x);}
int main(){
// freopen("testdata.in","r",stdin);
n=read(),d=read();
fp(i,1,n)a[i]=read(),x[i]=read(),sum[i]=sum[i-1]+a[i];
fp(i,1,n){
res={d*i,sum[i-1]};
while(top&&slope(st[top-1],st[top])>slope(st[top],res))--top;
st[++top]=res,res={x[i]+d*i,sum[i]};
l=1,r=top;
while(l<=r){
mid=(l+r)>>1;
(slope(st[mid],res)>slope(st[mid-1],res))?l=mid+1,ret=mid:r=mid-1;
}ans+=slope(st[ret],res);
}printf("%.0lf\n",ans);return 0;
}
最新文章
- Linux C popen()函数详解
- Adapter 启动时报错——2
- FreeRadius服务器安装以及error while loading shared libraries问题
- RSA数字证书管理
- Redis集群的配置
- markdown 设置字体颜色
- android ViewGroup事件分发机制
- typdef struct 语法
- Activiti的Eclipse插件离线安装指南
- 【HTML】Beginner6:Link
- 持续集成之戏说Check-in Dance
- 类型萃取(type traits)
- hdu 4465 概率称号
- vue中的computed(计算属性)和watch(监听属性)的特点,以及深度监听
- UIdynamic系列认知
- 自动化测试-10.selenium的iframe与Frame
- 自动化部署shell
- shell 检测安装包
- Python+Django(Python Web项目初体验)
- Spark-Cache与Checkpoint
热门文章
- zip相关知识梳理(一)
- UVA - 1623 Enter The Dragon(贪心)
- Spark在Executor上的内存分配
- Error:java: Internal compiler error: java.lang.Exception: java.lang.NoClassDefFoundError解决
- HXY烧情侣(洛谷 2194)
- ORACLE分区表删除分区数据
- 51Nod——T 1631 小鲨鱼在51nod小学
- CH上的Think Bear#1模拟赛
- 深入解析Microsoft Sql server 2008
- CSS filter 模拟黑洞照片效果