poj3257

dp,先将材料按以终点为关键字升序排

设f[i,j]为过山车到建到位置i在用了j元钱所得到的最大价值,然后

 var x,y,v,w:array[..] of longint;
f:array[..,..] of longint;
l,n,k,m,j,i,ans:longint; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure sort(l,r: longint);
var i,j,p: longint;
begin
i:=l;
j:=r;
p:=y[(l+r) div ];
repeat
while y[i]<p do inc(i);
while p<y[j] do dec(j);
if not(i>j) then
begin
swap(x[i],x[j]);
swap(y[i],y[j]);
swap(v[i],v[j]);
swap(w[i],w[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; begin
readln(l,n,m);
for i:= to n do
begin
readln(x[i],j,v[i],w[i]);
y[i]:=x[i]+j;
if y[i]>l then y[i]:=l;
end;
sort(,n);
j:=;
fillchar(f,sizeof(f),);
f[,]:=;
for i:= to l do
begin
while y[j]<=i do
begin
if y[j]=i then
begin
for k:= to m do
if (f[x[j],k]>) and (k+w[j]<=m) then
f[i,k+w[j]]:=max(f[i,k+w[j]],f[x[j],k]+v[j]);
end;
inc(j);
end;
end;
ans:=;
for j:= to m do
ans:=max(ans,f[l,j]);
writeln(ans-);
end.

容易分析复杂度为O(nm)

首尾必须相连是这题关键

上一篇:Linux 设备驱动--- 阻塞型字符设备驱动 --- O_NONBLOCK --- 非阻塞标志【转】


下一篇:nginx.conf 中php-ftp配置