BZOJ 5059 前鬼后鬼的守护
2024-09-07 03:24:08
题解:
解法一:用函数斜率什么的,不会,留坑
解法二:
某一个序列都变成一个值那么中位数最优
加入一个元素,与前面那一段区间的中位数比较
x>=mid什么事也不做
x<mid合并两端区间
不停向前合并
用大根可并堆维护中位数
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=600009; int n;
long long ans=0; int fa[maxn]={0},ch[maxn][2]={0},ky[maxn]={0},dis[maxn]={0},siz[maxn]={0};
int Getf(int x){
while(fa[x])x=fa[x];
return x;
}
int Mer(int x,int y){
if((x==0)||(y==0))return x^y;
if(ky[x]<ky[y])swap(x,y);
ch[x][1]=Mer(ch[x][1],y);
fa[ch[x][1]]=1;
siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
if(dis[ch[x][1]]>dis[ch[x][0]])swap(ch[x][0],ch[x][1]);
dis[x]=dis[ch[x][1]]+1;
return x;
} struct Sec{
int l,r,root;
Sec(){}
Sec(int ll,int rr,int x){
l=ll;r=rr;root=x;
}
void poppoint(){
int tm=(r-l+2)/2;
while(siz[root]>tm){
fa[ch[root][0]]=0;
fa[ch[root][1]]=0;
root=Mer(ch[root][0],ch[root][1]);
}
}
}Sta[maxn];
int top=0; int ab(int x){
if(x<0)return -x;
else return x;
} int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&ky[i]);
siz[i]=1;
}
for(int i=1;i<=n;++i){
Sta[++top]=Sec(i,i,i);
while(top>=2){
if(ky[Sta[top].root]>=ky[Sta[top-1].root])break;
Sta[top-1].root=Mer(Sta[top-1].root,Sta[top].root);
--top;
Sta[top].r=i;
Sta[top].poppoint();
}
}
for(int j=1;j<=top;++j){
// cout<<Sta[j].l<<' '<<Sta[j].r<<' '<<ky[Sta[j].root]<<endl;
for(int i=Sta[j].l;i<=Sta[j].r;++i){
ans+=ab(ky[Sta[j].root]-ky[i]);
}
}
cout<<ans<<endl;
return 0;
}
最新文章
- EXCEL中多级分类汇总空白字段填充
- ( [原创] 4s摄像头出现的问题及解决办法集锦。
- [jQuery EasyUI系列] 创建增删改查应用
- Velocity(4)——大小写
- [SAP ABAP开发技术总结]动态修改选择屏幕
- SharePoint 2013 开发——SharePoint Designer 2013工作流
- webpack+react+jquery和jquery插件
- dubbo源码分析一:整体分析
- sql server 判断相同值的数据
- IsoAlgo3d - A PCF 3D Viewer for Desktop, Tablet and Smart phone
- [cacti]nginx+php+cacti+mysql+php-fpm 安装小记
- 学习CSS3之实心圆
- kubernetes 编排详解 挂载
- 美团面试-canvas实现放射图
- CVE-2017-11882 POC 全版本通杀
- ocos2d-x 3.0坐标系详解--透彻篇 ---- convertToWorldSpace:把基于当前节点的本地坐标系下的坐标转换到世界坐标系中。
- Kattis之旅——Prime Path
- javascript AOP(面向切面编程)
- vector list map set等容器某些函数的使用区别
- mfc c++字符串类与 流输出
热门文章
- Java安全中的“大坑”,跨平台真“浮云”
- 有关《查找两个List中的不同元素》的问题解答与编程实践
- 通过css 居中div的几种常用方法
- 使用loadrunner监控apcahe资源
- 八 Hibernate延迟加载&;抓取策略(优化)
- firewalld学习-zone的使用和配置
- SChema 多个属性的设置学习
- Windows 与 Linux 、esxi下面查看内存容量和数量
- python 聚类分析 k均值算法
- [Java] Eclipse 设置相同变量背景色高亮显示