我正在努力解决动态编程问题几天。它是这样的: 约翰的工作日分为N个时隙,每个时隙我都有一个增益G [i],他可以在那个时段工作。如果他决定在时间间隔[i,j]工作,他的总奖励将是R [i,j] = G [i + 1] + ... + G [j],因为第一个时段用于预热。每天他必须正好工作T槽 - 他可以从可用的N个总槽中选择一个T槽的子集。他希望通过选择一组分离间隔[a1,b1],[a2,b2],... [ak,bk]来最大化其利润,其中1 <= a1&lt; = b1&lt; a2&lt; = b2&lt; ...&lt; ak&lt; = bk和Sum [i = 1,k](bi-ai + 1)= T.
示例:N = 7,T = 5,增益矢量为{3,9,1,1,7,5,4}。最优解是选择区间[1,2]和[4,6],总利润为9 + 12 = 21.
答案 0 :(得分:0)
DP解决方案: int f [i] [j] [0..1];
let f[i][j][0] denotes the maximal gain for the first i time slots and using j time slots, and the i-th time slot is not used.
let f[i][j][1] denotes the maximal gain for the first i time slots and using j time slots, and the i-th time slot is used.
obviously,f[i][j][k] can determine f[i+1][j][k] or f[i+1][j+1][k]. details below:
f[i+1][j+1][1]=max(f[i+1][j+1][1],f[i][j][0],f[i][j][1]+G[i+1]);
f[i+1][j][0]=max(f[i+1][j][0],f[i][j][0],f[i][j][1]);