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 ...
随机推荐
- 最短Hamilton路径-状压dp解法
最短Hamilton路径 时间限制: 2 Sec 内存限制: 128 MB 题目描述 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamil ...
- 【CF721C】Journey(拓扑排序,最短路,DP)
题意:给一个无环的图,问用不超过T的时间从1到n最多可以经过多少个点.要求输出一条路径. 思路:因为无环,可以用DP做.不过因为时间最短的原因要拓扑排序后再DP,目测由底向上的更新也是可以的. ; . ...
- Objective-C日期相关工具方法
//date根据formatter转换成string +(NSString*)dateToString:(NSString *)formatter date:(NSDate *)date { NSDa ...
- 天梯赛 - L2-002 链表去重
GG思密达,第二个测试点的三分怎么也拿不上,我还是比较熟悉指针,用指针来写~,写完去上概率论 题目链接:https://www.patest.cn/contests/gplt/L2-002 #incl ...
- 牛客网 Wannafly挑战赛9 C.列一列-sscanf()函数
C.列一列 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言524288K64bit IO Format: %lld 链接:https://www.now ...
- Careercup | Chapter 4
二叉查换树,左孩子小于等于根,右孩子大于根. 完全二叉树,除最后一层外,每一层上的节点数均达到最大值:在最后一层上只缺少右边的若干结点. complete binary tree 满二叉树,完美二叉树 ...
- win7快捷键和ubuntu快捷键
http://www.cnblogs.com/xfiver/archive/2010/12/08/1899905.html http://www.pc841.com/article/20121203- ...
- android 扩大view的响应区域
1.Android提供TouchDelegate帮助实现扩大一个很小的view的点击区域 例如:https://developer.android.com/training/gestures/view ...
- C# 生成二维码(带Logo)
C# 生成二维码(带Logo) 第一种方式 我们需要引用 ThoughtWorks.QRCode.dll 生成带logo二维码(framework4.0以上) 下载地址:https://pan.ba ...
- python--文本处理1
1.字符和字符值之间的转换 内建函数:ord(),chr() >>> print ord("a") 97 >>> print chr(97) a ...