简体   繁体   中英

Dynamic programming - task scheduling

This is a question nobody was able to answer correctly to at a "flash test" in my class in my college:

We are given:

int N < 1000 slots of time

int[] C: -10 000 <= C[i] <= 10 000 payment corresponding to each of the slots

int T: number of slots we have to use

The problem is stated in the following way:

A lazy worker has N slots of time in a total workday.

For each slot of time he gets a certain payment (C[i] - which, unrealistically, can also be negative).

He wants to choose intervals of exactly T slots so that he will get the biggest payment. We have to choose the intervals he will work in. For example [1, 4] - from the first slot to the 4th.

The problem is that every time he takes a break, when he comes back to work, the first slot he works will not be paid, because he is getting used to work again, just like a lazy person. Therefore, we can also choose empty intervals like [5, 5], this could come in handy if we have negative payments. These will get the payment 0, regardless of what payment is associated to the slot beforehand.

To make it clearer, I'm going to give a simple example:

Let's say we have N=5 slots, and we want to choose T=4. C={3, 9, 1, 1, 7}

The best solution is the intervals [1, 2]; [4, 5] with a total payment of 9+7=16

We cover a total of T=4 slots, the solution is valid.

My first attempt at the problem, there may be something faster though. Let f(i, j, t) denote the best way to fit t spots inside the subarray N[i, j] (inclusive of i and j), such that N[i] is covered and N[j] is covered. Now note that f(i, j, t) = max { f(i, j-1, t-1) + C[j], max_{1 < k < j - i} f(i, jk, t-1) }. The base cases are f(i, j, t) = 0 if (j - i + 1 < t), f(i, i, 1) = 0, f(i, j, 1) = inf, and f(i, j, 2) = C[i] + C[j]. The complexity I believe is O(N^3 * T), in that the table f has size N^2 * T, and inside f we need to take max of (k < j - i) to get the additional factor of N in our complexity. Thus O(N^3 * T). Hopefully I haven't overlooked something in the problem.

In order to find the solution from the table f, just do max_{i,j} f(i, j, T).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM