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

Input

Output
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+;
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+;
return a;
}
int pop(int a){
return merge(heap[a].l,heap[a].r);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&z[i]),z[i]-=i;
for(int i=;i<=n;i++){
++tot;
l[tot]=r[tot]=i;
num[tot]=cnt[tot]=;
root[tot]=++count;
heap[count].w=z[i]; while(tot>&&heap[root[tot]].w<heap[root[tot-]].w){
--tot;
root[tot]=merge(root[tot],root[tot+]);
num[tot]+=num[tot+];
cnt[tot]+=cnt[tot+];
r[tot]=r[tot+];
for(;cnt[tot]*>num[tot]+;cnt[tot]--)
root[tot]=pop(root[tot]);
}
}
long long ans=;
for(int i=;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 ;
}
1367: [Baltic2004]sequence的更多相关文章
- 【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 ...
- 【BZOJ】1367: [Baltic2004]sequence
题意 给\(n(n \le 10^6)\)个数的序列\(a\),求一个递增序列\(b\)使得\(\sum_{i=1}^{n} |a_i-b_i|\)最小. 分析 神题啊不会. 具体证明看黄源河论文&l ...
- BZOJ 1367 [Baltic2004]sequence (可并堆)
题面:BZOJ传送门 题目大意:给你一个序列$a$,让你构造一个递增序列$b$,使得$\sum |a_{i}-b_{i}|$最小,$a_{i},b_{i}$均为整数 神仙题.. 我们先考虑b不递减的情 ...
- 【bzoj1367】[Baltic2004]sequence
2016-05-31 17:31:26 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1367 题解:http://www.cnblogs.co ...
- BZOJ 1367([Baltic2004]sequence-左偏树+中位数贪心)
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MB Submit: 521 Solved: 159 [ Subm ...
- 【BZOJ1367】[Baltic2004]sequence 左偏树
[BZOJ1367][Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sampl ...
随机推荐
- Java--消除重复数字后的最大值
描述: 一个长整型数字,消除重复的数字后,得到最大的一个数字. 如12341 ,消除重复的1,可得到1234或2341,取最大值2341. 42234,消除4 得到4223 或者 2234 ,再消除2 ...
- Codevs 1010 过河卒== 洛谷 1002
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 如图,A 点有一个过河卒,需要走到目标 B 点.卒行走规则:可以向下.或者向右.同 ...
- JSTL <C:if></C:if> 和<C:ForEach></C:ForEach> 入门级~
一.<C:If>标签:条件判断语句 <c:if test="${objList.nodetype == 1}">上级节点</c:if> te ...
- LeetCode OJ——Word Break
http://oj.leetcode.com/problems/word-break/ 动态规划题目,重点是建立出模型来: fun(start,end) = fun(start,i)*fun(i+1, ...
- Windows Phone 8 与 windows 8 开发技术概览
目前来说Windows phone 8的开发者 大家都是走战斗在在技术朋友,相信大家在做Windows Phone 8开发的同时也在关注Windows 8,我相信很多开发者一定是在 Windows 8 ...
- [笔记][FPGA]如何使用SignalTap观察wire与reg值
0. 简介 在FPGA程序调试时,我们除了仿真还经常的会用到SignalTap进行板级调试,其可以真实有效的反应某些变量的变化,方便我们理解内在跳转,方便Debug的运行.SignalTap需要制定时 ...
- CompletionService 与 ExecutorService 获取任务执行结果时的区别
CompletionService 与 ExecutorService 之间的区别 在讨论二者之间的区别之前,先交待一下背景. 看了ElasticSearch Transport模块的源码,里面充满了 ...
- JAVA算法总结_时间复杂度_Demo
JAVA面试中经常问到排序算法问题,本人结合网络上一些资源整理了编写一下常用的Demo,并附带运行结果,希望能帮助到大家. /** * @Title: 冒泡排序 * @Description: 将数组 ...
- 邁向IT專家成功之路的三十則鐵律 鐵律二十七 IT人夢想之道-實踐
有句話說:「人因夢想而偉大」.身為IT的您不知道夢想為何?是希望能夠環遊世界.開一間咖啡廳.買一部法拉利跑車.買一部重機.中大樂透頭彩.娶心目中的女神當老婆,還是只要明天還能活著就好了.無論您的夢想為 ...
- 实践认识--ANN
1. 常用激活函数 激活函数的选择是构建神经网络过程中的重要环节,下面简要介绍常用的激活函数. (1) 线性函数 ( Liner Function ) (2) 斜面函数 ( Ramp Functio ...