求教一经典算法……
pascal吧
全部回复
仅看楼主
level 1
taobingxue 楼主
x轴上有若干条线段,求线段覆盖的总长度。(当然线段会有重叠)
2008年06月09日 12点06分 1
level 1
taobingxue 楼主
弱智算法是有一个布尔数组,这个简单不赘述
2008年06月09日 12点06分 2
level 1
taobingxue 楼主
似乎此题还有一所谓较常用算法,我看了解析,编了以下程序
2008年06月09日 12点06分 3
level 1
taobingxue 楼主
const tw:array [1..11] of integer=(1,2,4,8,16,32,64,128,256,512,1024);var a:array [1..500,1..2] of longint; y,z:array [1..500] of longint; n,i,j,ss,en:integer; t:boolean; fr,la:longint; tree:array [1..2000] of boolean;procedure paixu(r,c:longint); var i,j,k:longint; begin if r>=c then exit; i:=r; j:=c; k:=z[r]; repeat while (z[j]>=k)and(i
y[ss] then begin inc(ss);y[ss]:=z[i];end; end;end;procedure ins(p,l,r,a,b:longint);var m:integer;begin if tree[p] then begin if (l=a) and (r=b) then tree[p]:=false else begin m:=(l+r) div 2; if b<=m then ins(p*2,l,m,a,b) else begin if a>=m then ins(p*2+1,m,r,a,b) else begin ins(p*2,l,m,a,m);ins(p*2+1,m,r,m,b);end; end; end; end;end;function co(p,l,r:integer):longint;begin if tree[p]=false then co:=y[r]-y[l] else begin if r-l=1 then co:=0 else co:=co(p*2,l,(l+r) div 2)+co(p*2+1,(l+r) div 2,r); end;end;begin readln(n); for i:=1 to n do begin read(a[i,1],a[i,2]);z[i*2-1]:=a[i,1];z[i*2]:=a[i,2];end; paixu(1,n*2); fang; fillchar(tree,sizeof(tree),true); for en:=1 to 11 do if tw[en]+1>=ss then break; for i:=1 to n do begin t:=true; for j:=1 to ss do begin if t then begin if a[i,1]=y[j] then begin fr:=j;t:=false;end; end else if a[i,2]=y[j] then la:=j; end; ins(1,1,tw[en]+1,fr,la); end; writeln(co(1,1,tw[en]+1));end.
2008年06月09日 12点06分 4
level 1
taobingxue 楼主
但似乎相对于白痴算法而言这样做速度快不了多少,而且要多打n多东西……
2008年06月09日 12点06分 5
level 1
taobingxue 楼主
哪位来给小妹解解疑
2008年06月09日 12点06分 6
level 6
经典线段树。。。
2008年06月11日 07点06分 7
level 6
不好意baidu上搜的线段树程序都是C的。。。我记得貌似有2~3个大牛专门写过选段数。。懒得找了。。实在不行等我们恢复活动了我再把资料给你传过去吧
2008年06月11日 07点06分 8
1