You are on page 1of 70

# 2

si
fi
vi

(.vi=1 , i=1..n)

f1f2 fn
p(j) = i < j i
j

1: j

1, 2, ...,p(j)

p(j)+1, p(j)+2, , j-1

2:

1, 2,
, -1

OPT(j) =
1, ...,

## Input: n, s1,,sn , f1,,fn , v1,,vn

Sort jobs by finish times so that f1 f2 ... fn.
Compute p(1), p(2), , p(n)
Compute-Opt(j) {
if (j = 0)
return 0
else
return max(vj + Compute-Opt(p(j)), Compute-Opt(j-1))
}

2n/2

(2n)

Bottom-Up

## Input: n, s1,,sn , f1,,fn , v1,,vn

Sort jobs by finish times so that f1 f2 ... fn.
Compute p(1), p(2), , p(n)
Iterative-Compute-Opt {
OPT[0] = 0
for j = 1 to n
OPT[j] = max(vj + OPT[p(j)], OPT[j-1])
}
Output OPT[n]

Bottom-Up
OPT[j] e
1...j

(n)
(n log n)

,
?
j e
{1, 2,
, j} vj + OPT(p(j)) OPT(j - 1)
j

{1,

2, , p(j)}
{1, 2, , j-1}

Run M-Compute-Opt(n)
Run Find-Solution(n)
Find-Solution(j) {
if (j = 0)
output nothing
else if (vj + OPT[p(j)] > OPT[j-1])
print j
Find-Solution(p(j))
else
Find-Solution(j-1)

< n O(n)

n
j sj, a
fj

()

( )
,

## Sort requests in increasing order of start times

(s1,f1),,(sn,fn)

For i=1 to n
j1
While (request i not scheduled)
lastj finish time of the last request
currently scheduled on resource j
if silastj then schedule request i on
resource j
jj+1
End While
End For

d =

d
, j,
d-1
,

sj
d
sj
d

## Sort requests in increasing order of start times

(s1,f1),,(sn,fn) (n log n)

For i=1 to n
j1
While (request i not scheduled)
lastj finish time of the last request
currently scheduled on resource j
if silastj then schedule request i on
resource j
jj+1
End While
End For O (n * d), (n2)

## Sort requests in increasing order of start times (s1,f1),,(sn,fn) (n log n)

d 1
Schedule request 1 on resource 1
last1 f1
Insert 1 into priority queue Q with key = last1
For i=2 to n
j findmin(Q)
if si lastj then
schedule request i on resource j
lastj fi
Increasekey(j,Q) to lastj
else
d d+1
schedule request i on resource d
lastd fi
Insert d into priority queue Q with key = lastd
End For (n log n)

,
i

ti

s

[si, fi] ti=fi-si
i e
di < fi i Li=fi-di,
Li=0
L=maxiLi

( si fi i)
L

() tj

dj

dj tj

## Sort deadlines in increasing order

(d1 d2 dn)
f s
for i 1 to n to
si f
fi si+ti
f fi
end for

O ( )

O

O
o A

1:

2:

3:

4:

5:

4:

: O

4 .
1. O , i j j
i d(j) < d(i).
2. i j .
i j, O

3. O

C(n, 2) ,

S
i j di < dj,
i

( )
,

dj > di j O
i i
j O

Li Li i O
O
i j

ki,j Lk=Lk
fj=fi Lj= fj-dj =fi-dj< fj-dj=Lj

()

n(n-1)/2

:

:

,

S = {1,2,3, , n}, n
{d1, d2, d3, , dn}, n
(),
1<=di<=n i di.
{w1, w2, w3, , wn},
wi
di, di
.

: S
.

,
( )
1:
-
.
E
L, L E
E , L
2: 1

.

.
, i j
k k+1 dj < di,
i j
.

k+1 < dj.

.

, a
.

.

.
I .

## t=1, 2,, n Nt(A)

t.
:
:

2. t=1, 2,, n Nt(A) < t
3.
,

1.

2

=

Algorithm RT-Schedule (S, I, w)
A <- 0
Sort S into nonincreasing order by weight w
for each x S, taken in nonincreasing order
by weight wx
do if A U {x} I
then A <- A U {x}
return A

CDP

Example:

di

wi

70

60

50

40

30

20

10

- 1, 2, 3 4
- 5 6
- 7

: <2, 4, 1, 3, 7, 5, 6>

## ECE 291 -- Spring

2000

(N)
(M)

()

N = 3 M = 2 K = 2,

(), =

## 2 (3.0) 1 (0.2) 3 (0.1)

3 (1.0) 2 (0.5) 1 (0.2)

()

k-
O(N*M + N*logN)

()

,

,

O(N*M)

()

N=4M=4K=3
4

## (5.0) 2 (4.0) 3 (2.0) 1 (1.0)

2 (2.0) 3 (1.0) 1 (0.5) 4 (0.3)
4 (6.0) 3 (5.0) 2 (2.0) 1 (0.0)
1 (4.0) 2 (3.0) 4 (0.6) 3 (0.3)

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cassert>
#define MAXN 101
using namespace std;
double best[MAXN];
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
int s;
double o;
scanf("%d%lf", &s, &o);
best[s-1] = max(best[s-1], o);
}
}
sort(best, best+n, greater<double>());
double sol = 0.0;
for(int i = 0; i < k; ++i)
sol += best[i];
printf("%.1lf\n", sol);
return 0;
}

M
N
. i-
j ,
A(i,j) .

(
).

.

=2, N=4
A[1]={1, 6, 7, 8}
A[2]={2, 5, 6, 7}

1 = 8
3l 1 1l 2 = 9
2l 2 2l 2 = 11
4l

=2, N=3
A[1]={1, 2, 3}
A[2]={1, 5, 7}

A[1]={1, 1, 1}
A[2]={1, 2.5, 2.3}

2 1l 1 = 6
3l 2 = 7
2l

k,
N-k
-1

B(X,Y)
X Y

B(P,Q)
P<X, Q=1,N, B(X,Y)

X
( 0 Y ),

X-1

B(1,Y) = A(1,Y)
B(X,0) = 0
B(X,Y) = {B(X-1,Y-k) + A(X,k)}