隐藏页面特效

1367: [Baltic2004]sequence

1367: [Baltic2004]sequence

Time Limit: 20 Sec  Memory Limit: 64 MB
Submit: 1090  Solved: 432
[Submit][Status][Discuss]

Description

Input

Output

一个整数R

Sample Input

7
9
4
8
20
14
15
18

Sample Output

13

HINT

所求的Z序列为6,7,8,13,14,15,18.
R=13

Source

 

【题解】:

详见:《左偏树的特点及其应用》(P18-20)

【代码】:

//左偏树,最短的高级数据结构了 #include<cstdio> #include<cstdlib> #include<iostream> using namespace std; const int N=1e6+10; int n,tot,count,root[N],l[N],r[N],z[N],num[N],cnt[N]; struct node{ int l,r,dis,w; }heap[N]; int merge(int a,int b){ if(!a||!b) return a+b; if(heap[a].w<heap[b].w) swap(a,b); heap[a].r=merge(heap[a].r,b); if(heap[heap[a].l].dis<heap[heap[a].r].dis) swap(heap[a].l,heap[a].r); heap[a].dis=heap[heap[a].r].dis+1; return a; } int pop(int a){ return merge(heap[a].l,heap[a].r); } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&z[i]),z[i]-=i; for(int i=1;i<=n;i++){ ++tot; l[tot]=r[tot]=i; num[tot]=cnt[tot]=1; root[tot]=++count; heap[count].w=z[i]; while(tot>1&&heap[root[tot]].w<heap[root[tot-1]].w){ --tot; root[tot]=merge(root[tot],root[tot+1]); num[tot]+=num[tot+1]; cnt[tot]+=cnt[tot+1]; r[tot]=r[tot+1]; for(;cnt[tot]*2>num[tot]+1;cnt[tot]--) root[tot]=pop(root[tot]); } } long long ans=0; for(int i=1;i<=tot;i++) for(int j=l[i],w=heap[root[i]].w;j<=r[i];j++) ans+=abs(z[j]-w); cout<<ans; return 0; }

 

 

 

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/6275229.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(223)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示