dp,先将材料按以终点为关键字升序排
设f[i,j]为过山车到建到位置i在用了j元钱所得到的最大价值,然后
1 var x,y,v,w:array[0..10010] of longint; 2 f:array[0..1010,0..1010] of longint; 3 l,n,k,m,j,i,ans:longint; 4 5 function max(a,b:longint):longint; 6 begin 7 if a>b then exit(a) else exit(b); 8 end; 9 10 procedure swap(var a,b:longint); 11 var c:longint; 12 begin 13 c:=a; 14 a:=b; 15 b:=c; 16 end; 17 18 procedure sort(l,r: longint); 19 var i,j,p: longint; 20 begin 21 i:=l; 22 j:=r; 23 p:=y[(l+r) div 2]; 24 repeat 25 while y[i]<p do inc(i); 26 while p<y[j] do dec(j); 27 if not(i>j) then 28 begin 29 swap(x[i],x[j]); 30 swap(y[i],y[j]); 31 swap(v[i],v[j]); 32 swap(w[i],w[j]); 33 inc(i); 34 j:=j-1; 35 end; 36 until i>j; 37 if l<j then sort(l,j); 38 if i<r then sort(i,r); 39 end; 40 41 begin 42 readln(l,n,m); 43 for i:=1 to n do 44 begin 45 readln(x[i],j,v[i],w[i]); 46 y[i]:=x[i]+j; 47 if y[i]>l then y[i]:=l; 48 end; 49 sort(1,n); 50 j:=1; 51 fillchar(f,sizeof(f),0); 52 f[0,0]:=1; 53 for i:=1 to l do 54 begin 55 while y[j]<=i do 56 begin 57 if y[j]=i then 58 begin 59 for k:=0 to m do 60 if (f[x[j],k]>0) and (k+w[j]<=m) then 61 f[i,k+w[j]]:=max(f[i,k+w[j]],f[x[j],k]+v[j]); 62 end; 63 inc(j); 64 end; 65 end; 66 ans:=0; 67 for j:=1 to m do 68 ans:=max(ans,f[l,j]); 69 writeln(ans-1); 70 end.
容易分析复杂度为O(nm)
首尾必须相连是这题关键