部分经典算法
2015-06-14 18:45
295 查看
双关键字快排
procedure swap(a,b:longint); var t:longint; begin for i:=1 to 3 do begin t:=x[a,i]; x[a,i]:=x[b,i]; x[b,i]:=t; end; end; procedure sort(l,r: longint); var i,j,x1,x2,y: longint; begin i:=l; j:=r; x1:=x[(l+r) div 2,1]; x2:=x[(l+r) div 2,2]; repeat while (x[i,1]<x1)or((x[i,1]=x1)and(x[i,2]<x2)) do inc(i); while (x1<x[j,1])or((x[j,1]=x1)and(x[j,2]>x2)) do dec(j); if not(i>j) then begin swap(i,j); inc(i); dec(j); end; until i>j; if l<j then sort(l,j); if i<r then sort(i,r); end;
归并排序
逆序对
var x,y:array[0..2000000]of longint; i,j,k:longint; n,len:longint; ans:int64; procedure sort(l,r:longint); var i,j,mid:longint; begin if l>=r then exit; mid:=(l+r)>>1; sort(l,mid); sort(mid+1,r); i:=l; j:=mid+1; len:=l-1; while (i<=mid)or(j<=r) do if (j>r)or((i<=mid)and(x[i]<=x[j])) then begin inc(len); y[len]:=x[i]; inc(i); end else begin inc(len); y[len]:=x[j]; inc(j); inc(ans,mid-i+1); {inc是用来算逆序对的} end; for i:=l to r do x[i]:=y[i]; end; begin readln(N); for i:=1 to n do read(x[i]); ans:=0; sort(1,n); writeln(ans); end.
树状数组
先离散化,从后向前每次先查询,再把自己加入树状数组加入时是把y[x[i]]+1
var x,y:array[0..1000005]of longint; i,j,k:longint; n,maxn:longint; ans:int64; function query(a:longint):int64; var tt:int64; begin tt:=0; while a>0 do begin inc(tt,y[a]); dec(a,a and(-a)); end; exit(tt); end; procedure update(a:longint); begin while a<=maxn do begin inc(y[a]); inc(a,a and(-a)); end; end; begin readln(n); maxn:=0; for i:=1 to n do begin read(x[i]); if x[i]>maxn then maxn:=x[i]; end; ans:=0; for i:=n downto 1 do begin inc(ans,query(x[i]-1)); update(x[i]); end; writeln(ans); end.
堆排序
var n,i:longint; x,heap:array[0..1000000]of longint; procedure shai(z:longint); var tt,a,b,s:longint; begin tt:=z; while (heap[tt*2]<>-maxlongint)or(heap[tt*2+1]<>-maxlongint) do begin a:=tt*2; b:=a+1; if (heap[a]>heap[tt])and(heap[a]>=heap[b]) then begin s:=heap[a]; heap[a]:=heap[tt]; heap[tt]:=s; tt:=a; end else if (heap[b]>heap[tt])and(heap[b]>=heap[a]) then begin s:=heap[b]; heap[b]:=heap[tt]; heap[tt]:=s; tt:=b; end else break; end; end; procedure prepare; begin readln(n); for i:=1 to 5*n do heap[i]:=-maxlongint; for i:=1 to n do read(heap[i]); end; begin prepare; heap[0]:=n; for i:=(n div 2) downto 1 do shai(i); while heap[0]<>1 do begin inc(x[0]); x[x[0]]:=heap[1]; dec(heap[0]); heap[1]:=0; shai(1); end; x :=heap[1]; for i:=n downto 2 do write(x[i],' '); writeln(x[1]); end.
相关文章推荐
- hadoop 异常记录 ERROR: org.apache.hadoop.hbase.MasterNotRunningException: Retried 7 times
- [Objective-C] 006_Protocol(协议)
- java web项目中的jsp页面发送ajax请求404错误
- 算法练习:数组配对
- 浏览器缓存机制
- 浏览器缓存机制
- Linux企业运维高效技巧心得及分享
- JSTL标签的具体介绍—内容多多
- 初步理解Python Web WSGI
- keil中对struct结构体静态赋值导致代码急剧膨胀的原因(转)
- android json解析
- Error “can't use subversion command line client : svn” Probably the path to Subversion executable is wrong
- 团队项目
- hiho一下 第五十周
- hiho一下 第五十周(欧拉路径)50
- 关于VS2015支持编译Linux程序的问题
- 第三个spirit(第十四天)
- Oracle安装
- 再论由内而外造就自己
- Android之GridView