【NOI2009】 二叉查找树
2012-05-16 10:15
253 查看
treapmod
program treapmod; uses math; const maxn=100; maxnum=1000000000; type ty=record s,q,f:longint; end; var n,k,i:longint; a:array[0..maxn] of ty; s:array[0..maxn] of longint; f:array[0..maxn,0..maxn,0..maxn] of longint; //================== procedure sort(l,r,k:longint); var i,j,m:longint; t:ty; begin i:=l; j:=r; if k=1 then m:=a[(l+r) >> 1].q else m:=a[(l+r) >> 1].s; repeat if k=1 then begin while a[i].q<m do inc(i); while a[j].q>m do dec(j); end else begin while a[i].s<m do inc(i); while a[j].s>m do dec(j); end; if i<=j then begin t:=a[i]; a[i]:=a[j]; a[j]:=t; inc(i); dec(j); end; until i>j; if i<r then sort(i,r,k); if j>l then sort(l,j,k); end; //================== function find(i,j,w:longint):longint; var x:longint; begin if i>j then exit(0); if f[i,j,w]>-1 then exit(f[i,j,w]); f[i,j,w]:=maxnum; for x:=i to j do begin if a[x].q>=w then f[i,j,w]:=min(f[i,j,w],find(i,x-1,a[x].q)+find(x+1,j,a[x].q)+s[j]-s[i-1]); f[i,j,w]:=min(f[i,j,w],find(i,x-1,w)+find(x+1,j,w)+s[j]-s[i-1]+k); end; exit(f[i,j,w]); end; //================== procedure main; begin fillchar(f,sizeof(f),255); writeln(find(1,n,0)); end; //================== begin assign(input,'treapmod.in'); reset(input); assign(output,'treapmod.out'); rewrite(output); read(n,k); for i:=1 to n do read(a[i].s); for i:=1 to n do read(a[i].q); for i:=1 to n do read(a[i].f); sort(1,n,1); for i:=1 to n do a[i].q:=i; sort(1,n,2); for i:=1 to n do s[i]:=s[i-1]+a[i].f; main; close(input); close(output); end.
相关文章推荐
- c#数据结构———二叉查找树
- C++实现的变种二分查找法(折半查找)--二叉查找树
- 二叉查找树
- 二叉查找树
- 算法学习--二叉查找树
- 【数据结构与算法基础】二叉查找树 / Binary Search Tree
- java 实现的二叉查找树
- 程序员面试100题之十三:求二叉查找树的镜像
- C++算法(一)之二叉查找树
- java实现二叉查找树
- 二叉树和二叉查找树
- 二叉查找树
- 二叉查找树 - C语言实现(摘自数据结构与算法分析 C语言描述)
- 找出二叉查找树中第n大的值
- 二叉查找树(二)
- 【NOI2009】 诗人小G
- 找出二叉查找树中第n大的值
- 算法导论 第12章 二叉查找树
- 统计输入中所有单词出现的次数(使用二叉查找树实现:递归和非递归)
- 二叉查找树及平衡二叉查找树