大厅安排-SSL 1212

Description

  有一个演讲大厅需要GEORGE管理,演讲者们事先定好了需要演讲的起始时间和中止时间。GEORGE想让演讲大厅得到最大可能的使用。我们要接受一些预定而拒绝其他的预定,目标自然是使演讲者使用大厅的时间最长。为方便起见,假设在某一时刻一个演讲结束,另一个演讲就可以立即开始。 
  计算演讲大厅最大可能的使用时间。

Input

第一行为一个整数n,n <= 100,表示申请的数目。

Output

一个整数,表示大厅最大可能的使用时间。

Sample Input


12
1 2
3 5
0 4
6 8
7 13
4 6
9 10
9 12
11 14
15 19
14 16
18 20
Sample Output


16

题解:这道题用dp,首先用选择排序,然后a[j]表示大厅当前的使用时间,然后用max记录最大的使用时间。
      if (a[j]>max) and (t[i,1]>=t[j,2]) then max:=a[j];
      a[i]:=max+t[i,2]-t[i,1];

const maxn=5000;
var
  a:array[1..maxn] of longint;
  t:array[0..maxn,1..2] of longint;
  n,i,j,max:longint;
begin
  readln(n);
  for i:=1 to n do readln(t[i,1],t[i,2]);
  for i:=1 to n-1 do
   for j:=i+1 to n do
    if t[i,2]>t[j,2] then
    begin
      t[0]:=t[i];t[i]:=t[j];t[j]:=t[0];
    end;
    for i:=1 to n do
    begin
      max:=0;
      for j:=1 to i-1 do
      if (a[j]>max) and (t[i,1]>=t[j,2]) then max:=a[j];
      a[i]:=max+t[i,2]-t[i,1];
    end;
    max:=0;
    for i:=1 to n do
    if a[i]>max then max:=a[i];
    writeln(max);
end.

你可能感兴趣的:(大厅安排-SSL 1212)