[ 可并堆 贪心 ] [ Baltic2004 ] BZOJ1367
来源:互联网 发布:python怎么创建工程 编辑:程序博客网 时间:2024/05/18 04:40
论文例题。
但论文中
只要将
#include<bits/stdc++.h>using namespace std;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline void Read(int& x){ char c=nc(); for(;c<'0'||c>'9';c=nc()); for(x=0;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-48,c=nc());}const int N=1000010;typedef long long ll;int n,m,p,a[N];int Rt[N],ls[N],rs[N],v[N],d[N],sz[N],s[N];int top;ll Ans;int Merge(int x,int y){ if(!x||!y)return x+y; if(v[x]<v[y])swap(x,y); rs[x]=Merge(rs[x],y); if(d[rs[x]]>d[ls[x]])swap(ls[x],rs[x]); d[x]=d[rs[x]]+1; return x;}inline void Pop(int& x){ x=Merge(ls[x],rs[x]);}inline int Abs(int x){ return x<0?-x:x;}int main(){ Read(n); for(int i=1;i<=n;i++)Read(a[i]),a[i]-=i; for(int i=1;i<=n;i++){ v[i]=a[i]; Rt[++top]=i; sz[top]=s[top]=1; while(top>1&&v[Rt[top]]<v[Rt[top-1]]){ top--; Rt[top]=Merge(Rt[top],Rt[top+1]); sz[top]+=sz[top+1];s[top]+=s[top+1]; while(sz[top]>(s[top]+1)>>1)Pop(Rt[top]),sz[top]--; } } int j=1; for(int i=1;i<=top;i++){ int x=v[Rt[i]]; for(int k=0;k<s[i];k++){ Ans+=Abs(x-a[j++]); } } cout<<Ans<<endl; return 0;}
阅读全文
0 0
- [ 可并堆 贪心 ] [ Baltic2004 ] BZOJ1367
- bzoj1367 [Baltic2004]sequence(可并堆+中位数)
- 可并堆--左偏树(Bzoj1367&&Bzoj1455)
- BZOJ 1367 [Baltic2004]sequence 可并堆
- 【BZOJ 1367】 [Baltic2004]sequence 可并堆
- BZOJ[1367][Baltic2004]Sequence 可并堆
- [可并堆 中位数] BZOJ 1367 [Baltic2004]sequence
- bzoj 1367: [Baltic2004]sequence(中位数+可并堆)
- BZOJ 1367 [Baltic2004]sequence【脑洞+可并堆
- bzoj1367 [Baltic2004]sequence [左偏树]
- bzoj1367: [Baltic2004]sequence
- bzoj1367【Baltic2004】sequence
- bzoj1367: [Baltic2004]sequence
- BZOJ1367 [Baltic2004]sequence
- bzoj1367 [Baltic2004]sequence
- [BZOJ1367] [Baltic2004]sequence
- BZOJ1367——[Baltic2004]sequence
- BZOJ 2809 [Apio2012]dispatching【可并堆(贪心
- 在bibtex下引用arXiv的文献格式是什么?
- spring AOP 之 注解 配置实现(附 Java 代码实例)
- Git和TortoiseGit安装
- SYD8801蓝牙profile(UUID)的使用说明
- 将word文档转换为图片格式的PDF
- [ 可并堆 贪心 ] [ Baltic2004 ] BZOJ1367
- 哈希学习
- 5-1 继承与派生
- C#判断语句与循环语句
- C# winform 动态添加控件
- Sensor——摇一摇(传感器)
- Hi3518开发板开发总结
- 解决nginx中proxy_pass到tomcat的session丢失问题:补充
- 一些网页工具的地址