BZOJ1367:[Baltic2004]sequence
浅谈左偏树:https://www.cnblogs.com/AKMer/p/10246635.html
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=1367
显然,如果给出的数组是递增的,那么答案就是\(0\)。
如果给出的数组是递减的,根据贪心的思想答案就是\(\sum\limits_{i=1}^{n}|x-t_i|\),\(x\)是\(t\)数组的中位数。
但是给出的数组是无序的。
我们可以把这个数组划成一段段的,每一段都选一个\(x\)去当做中位数。简单的来讲,最优的方案是\(z_i=t_i\)的,但是为了保证\(z\)数组的递增性我们必须要把比前一位小的\(z\)和前一位合并到一段里去,形成新的一段,然后这一段的\(z\)就是这一段\(t\)的中位数。
但是这样出现了重复的\(z\),不满足严格递增的性质。但是我们只需要把\(t_i=t_i-i\)就行了。这样就默认是严格递增的了。
时间复杂度:\(O(nlogn)\)
空间复杂度:\(O(n)\)
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
ll ans;
int n,tot,cnt;
int l[maxn],r[maxn];
int fa[maxn],dist[maxn],son[maxn][2];
int a[maxn],rt[maxn],val[maxn],siz[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
int newnode(int v) {
val[++tot]=v;
siz[tot]=1;return tot;
}
int merge(int a,int b) {
if(!a||!b)return a+b;
if(val[a]<val[b])swap(a,b);
son[a][1]=merge(son[a][1],b);
if(dist[son[a][1]]>dist[son[a][0]])
swap(son[a][1],son[a][0]);
dist[a]=dist[son[a][1]]+1;
siz[a]=siz[son[a][0]]+1+siz[son[a][1]];
return a;
}
int pop(int u) {
int res=merge(son[u][0],son[u][1]);
son[u][0]=son[u][1]=0;
return res;
}
int main() {
n=read(),dist[0]=-1;
for(int i=1;i<=n;i++)
a[i]=read()-i;
for(int i=1;i<=n;i++) {
rt[++cnt]=newnode(a[i]);l[cnt]=r[cnt]=i;
while(cnt>1&&val[rt[cnt]]<val[rt[cnt-1]]) {
rt[cnt-1]=merge(rt[cnt-1],rt[cnt]);r[--cnt]=i;
while(siz[rt[cnt]]>(r[cnt]-l[cnt]+2)/2)rt[cnt]=pop(rt[cnt]);
}
}
for(int i=1;i<=cnt;i++)
for(int j=l[i];j<=r[i];j++)
ans+=abs(val[rt[i]]-a[j]);
printf("%lld\n",ans);
return 0;
}
BZOJ1367:[Baltic2004]sequence的更多相关文章
- 【BZOJ1367】[Baltic2004]sequence 左偏树
[BZOJ1367][Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sampl ...
- 【bzoj1367】[Baltic2004]sequence
2016-05-31 17:31:26 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1367 题解:http://www.cnblogs.co ...
- 【bzoj1367】[Baltic2004]sequence 可并堆
题目描述 输入 输出 一个整数R 样例输入 7 9 4 8 20 14 15 18 样例输出 13 题解 可并堆,黄源河<左偏树的特点及其应用>Page 13例题原题 #include & ...
- 【BZOJ 1367】 1367: [Baltic2004]sequence (可并堆-左偏树)
1367: [Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Ou ...
- BZOJ 1367: [Baltic2004]sequence [可并堆 中位数]
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1111 Solved: 439[Submit][ ...
- BZOJ 1367 [Baltic2004]sequence 解题报告
BZOJ 1367 [Baltic2004]sequence Description 给定一个序列\(t_1,t_2,\dots,t_N\),求一个递增序列\(z_1<z_2<\dots& ...
- bzoj 1367: [Baltic2004]sequence
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MB Description Input Output 一个整数R Sa ...
- 1367: [Baltic2004]sequence
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MB Submit: 1090 Solved: 432 [Submit ...
- 【九度OJ】题目1442:A sequence of numbers 解题报告
[九度OJ]题目1442:A sequence of numbers 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1442 ...
随机推荐
- 【BZOJ4928】第二题 树hash+倍增
[BZOJ4928]第二题 Description 对于一棵有根树,定义一个点u的k-子树为u的子树中距离u不超过k的部分. 注意,假如u的子树中不存在距离u为k的点,则u的k-子树是不存在的. 定义 ...
- 九度OJ 1202:排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:19711 解决:6508 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100). ...
- Unable to determine IP address from host name
- Javaweb基础--->过滤器filter(转发)
一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...
- python+selenium多窗口之间切换
#!/usr/bin/env python # coding:utf8 # author:Z time:2018/9/19 import time from selenium import webdr ...
- model特性
1.scope http://blog.csdn.net/lissdy/article/details/51107883 2.ActiveConcern http://www.tuicool.com/ ...
- 基于事件驱动的前端通信框架(封装socket.io)
socket.io的使用可以很轻松的实现websockets,兼容所有浏览器,提供实时的用户体验,并且为程序员提供客户端与服务端一致的编程体验.但是在使用socket.io的过程中,由于业务需求需要同 ...
- IOS UI Frame 相对位置调整 与优化方法 Height Width X Y 调整
不使用xib ,纯代码开发的过程中,动态UI 需要改对象的大小位置 反复使用CGRectMake 去 setFrame 非常低效耗时,而且 牵一发动全身. 以下整理出几个方法,方便动态布局 1.s ...
- 搭建SSM项目时报错(org.springframework.jdbc.CannotGetJdbcConnectionException)
严重: Servlet.service() for servlet [SpringMVC] in context with path [/ssm] threw exception [Request p ...
- LeetCode:二叉树的前、中、后序遍历
描述: ------------------------------------------------------- 前序遍历: Given a binary tree, return the pr ...