[BZOJ1642] [Usaco2007 Nov]Milking Time 挤奶时间

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=1642

题目大意

给定m个挤奶时间段,及产量,每次挤完要休息固定r分钟,n分钟内最大产量

题解

dp[i]表示前i个任务最大产量
dp[i]=w[i] 赋初值
dp[i]=max(dp[i],dp[j]+w[i]) {第j个任务结束时间+r<=第i个任务开始时间}
为了这个转移,我们要按照开始时间排序

var
 x:array[0..1000,1..3]of longint;
 dp:array[0..1000]of longint;
 i,j,k:longint;
 n,m,r,ans:longint;
procedure sort(l,r:longint);
var i,j,k,a,b:longint;
begin
 i:=l; j:=r; a:=x[(l+r) div 2,1];
 repeat
  while x[i,1]<a do inc(i);
  while a<x[j,1] do dec(j);
  if not(i>j) then
   begin
    for k:=1 to 3 do
     begin b:=x[i,k]; x[i,k]:=x[j,k]; x[j,k]:=b; end;
    inc(i); dec(j);
   end;
 until i>j;
 if l<j then sort(l,j);
 if i<r then sort(i,r);
end;

function max(a,b:longint):longint;
begin
 if a>b then exit(a) else exit(b);
end;

begin
 readln(n,m,r);
 for i:=1 to m do
  readln(x[i,1],x[i,2],x[i,3]);
 sort(1,m); {x[i,1]}
 ans:=0;
 for i:=1 to m do
  begin
   dp[i]:=x[i,3];
   for j:=1 to i-1 do
    if x[j,2]+r<=x[i,1]
    then dp[i]:=max(dp[i],dp[j]+x[i,3]);
   ans:=max(ans,dp[i]);
  end;
 writeln(ans);
end.

你可能感兴趣的:([BZOJ1642] [Usaco2007 Nov]Milking Time 挤奶时间)