此题为一般的动态规划,为可能性动态规划。初态只需将f[0]:=true;在所有都不小于a[i]的条件下进行动态规划。
状态转移方程:f[j+c[i]]:=true (if (f[j]=true) and (j+c[i]<=a[i]))
注意动态规划的顺序:一定要倒推,否则会造成重复。
CODE
Program Elevator;//By_Poetshy Const maxn=40000; Var i,j,p,n,max,re :Longint; f :Array[0..maxn]of Boolean; h,c,a :Array[1..400]of Longint; Function Min(i,j:Longint):Longint; begin if i<j then exit(i);exit(j); end; Procedure Qsort(l,r:Longint); var i,j,k,temp:Longint; begin i:=l;j:=r; k:=a[(i+j)>>1]; repeat while a[i]<k do inc(i); while a[j]>k do dec(j); if i<=j then begin temp:=a[i];a[i]:=a[j];a[j]:=temp; temp:=c[i];c[i]:=c[j];c[j]:=temp; temp:=h[i];h[i]:=h[j];h[j]:=temp; inc(i);dec(j); end; until i>j; if l<j then Qsort(l,j); if i<r then Qsort(i,r); end; BEGIN readln(n);f[0]:=true;max:=0; for i:=1 to n do readln(h[i],a[i],c[i]); Qsort(1,n); for i:=1 to n do for p:=1 to c[i] do begin re:=max; for j:=Min(max,a[i]-h[i])downto 0 do if f[j] then begin f[j+h[i]]:=true; if j+h[i]>max then max:=j+h[i]; end; if re=max then break; end; writeln(max); END.