poj3257

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.
View Code

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

首尾必须相连是这题关键

 

转载于:https://www.cnblogs.com/phile/p/4473291.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值