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)

【代码】:

  1. //左偏树,最短的高级数据结构了
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<iostream>
  5. using namespace std;
  6. const int N=1e6+;
  7. int n,tot,count,root[N],l[N],r[N],z[N],num[N],cnt[N];
  8. struct node{
  9. int l,r,dis,w;
  10. }heap[N];
  11. int merge(int a,int b){
  12. if(!a||!b) return a+b;
  13. if(heap[a].w<heap[b].w) swap(a,b);
  14. heap[a].r=merge(heap[a].r,b);
  15. if(heap[heap[a].l].dis<heap[heap[a].r].dis)
  16. swap(heap[a].l,heap[a].r);
  17. heap[a].dis=heap[heap[a].r].dis+;
  18. return a;
  19. }
  20. int pop(int a){
  21. return merge(heap[a].l,heap[a].r);
  22. }
  23. int main(){
  24. scanf("%d",&n);
  25. for(int i=;i<=n;i++) scanf("%d",&z[i]),z[i]-=i;
  26. for(int i=;i<=n;i++){
  27. ++tot;
  28. l[tot]=r[tot]=i;
  29. num[tot]=cnt[tot]=;
  30. root[tot]=++count;
  31. heap[count].w=z[i];
  32.  
  33. while(tot>&&heap[root[tot]].w<heap[root[tot-]].w){
  34. --tot;
  35. root[tot]=merge(root[tot],root[tot+]);
  36. num[tot]+=num[tot+];
  37. cnt[tot]+=cnt[tot+];
  38. r[tot]=r[tot+];
  39. for(;cnt[tot]*>num[tot]+;cnt[tot]--)
  40. root[tot]=pop(root[tot]);
  41. }
  42. }
  43. long long ans=;
  44. for(int i=;i<=tot;i++)
  45. for(int j=l[i],w=heap[root[i]].w;j<=r[i];j++)
  46. ans+=abs(z[j]-w);
  47. cout<<ans;
  48. return ;
  49. }

 

 

1367: [Baltic2004]sequence的更多相关文章

  1. 【BZOJ 1367】 1367: [Baltic2004]sequence (可并堆-左偏树)

    1367: [Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Ou ...

  2. BZOJ 1367: [Baltic2004]sequence [可并堆 中位数]

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1111  Solved: 439[Submit][ ...

  3. BZOJ 1367 [Baltic2004]sequence 解题报告

    BZOJ 1367 [Baltic2004]sequence Description 给定一个序列\(t_1,t_2,\dots,t_N\),求一个递增序列\(z_1<z_2<\dots& ...

  4. bzoj 1367: [Baltic2004]sequence

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MB Description Input Output 一个整数R Sa ...

  5. 【BZOJ】1367: [Baltic2004]sequence

    题意 给\(n(n \le 10^6)\)个数的序列\(a\),求一个递增序列\(b\)使得\(\sum_{i=1}^{n} |a_i-b_i|\)最小. 分析 神题啊不会. 具体证明看黄源河论文&l ...

  6. BZOJ 1367 [Baltic2004]sequence (可并堆)

    题面:BZOJ传送门 题目大意:给你一个序列$a$,让你构造一个递增序列$b$,使得$\sum |a_{i}-b_{i}|$最小,$a_{i},b_{i}$均为整数 神仙题.. 我们先考虑b不递减的情 ...

  7. 【bzoj1367】[Baltic2004]sequence

    2016-05-31 17:31:26 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1367 题解:http://www.cnblogs.co ...

  8. BZOJ 1367([Baltic2004]sequence-左偏树+中位数贪心)

    1367: [Baltic2004]sequence Time Limit: 20 Sec   Memory Limit: 64 MB Submit: 521   Solved: 159 [ Subm ...

  9. 【BZOJ1367】[Baltic2004]sequence 左偏树

    [BZOJ1367][Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sampl ...

随机推荐

  1. 最短Hamilton路径-状压dp解法

    最短Hamilton路径 时间限制: 2 Sec  内存限制: 128 MB 题目描述 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamil ...

  2. 【CF721C】Journey(拓扑排序,最短路,DP)

    题意:给一个无环的图,问用不超过T的时间从1到n最多可以经过多少个点.要求输出一条路径. 思路:因为无环,可以用DP做.不过因为时间最短的原因要拓扑排序后再DP,目测由底向上的更新也是可以的. ; . ...

  3. Objective-C日期相关工具方法

    //date根据formatter转换成string +(NSString*)dateToString:(NSString *)formatter date:(NSDate *)date { NSDa ...

  4. 天梯赛 - L2-002 链表去重

    GG思密达,第二个测试点的三分怎么也拿不上,我还是比较熟悉指针,用指针来写~,写完去上概率论 题目链接:https://www.patest.cn/contests/gplt/L2-002 #incl ...

  5. 牛客网 Wannafly挑战赛9 C.列一列-sscanf()函数

      C.列一列   时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言524288K64bit IO Format: %lld 链接:https://www.now ...

  6. Careercup | Chapter 4

    二叉查换树,左孩子小于等于根,右孩子大于根. 完全二叉树,除最后一层外,每一层上的节点数均达到最大值:在最后一层上只缺少右边的若干结点. complete binary tree 满二叉树,完美二叉树 ...

  7. win7快捷键和ubuntu快捷键

    http://www.cnblogs.com/xfiver/archive/2010/12/08/1899905.html http://www.pc841.com/article/20121203- ...

  8. android 扩大view的响应区域

    1.Android提供TouchDelegate帮助实现扩大一个很小的view的点击区域 例如:https://developer.android.com/training/gestures/view ...

  9. C# 生成二维码(带Logo)

    C# 生成二维码(带Logo) 第一种方式 我们需要引用 ThoughtWorks.QRCode.dll  生成带logo二维码(framework4.0以上) 下载地址:https://pan.ba ...

  10. python--文本处理1

    1.字符和字符值之间的转换 内建函数:ord(),chr() >>> print ord("a") 97 >>> print chr(97) a ...