这个题目有一个限制条件就是第i个topic必须在第i+1个topic讲完,所以首先使用贪心策略求出最少需要的lecture
状态: dp[i][j] 表示前i个topic使用j个lecture讲完
状态转移: dp[i][j] = min(dp[k][j-1] + cost(k+1, i))
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 1001
#define INF 0x3f3f3f3f
int dp[MAXN][MAXN], sum[MAXN][MAXN], t[MAXN], class_time, base_cost, n;
inline int cost(int time)
{
if( !time ) {
return 0;
}
if( time >= 1 && time <= 10 ) {
return -1*base_cost;
}
return (time-10)*(time-10);
}
int dfs(const int &idx, const int &day)
{
if( -1 != dp[idx][day] ) {
return dp[idx][day];
}
if( 1 == day ) {
return dp[idx][day] = (sum[1][idx] > class_time)? INF : cost(class_time-sum[1][idx]);
}
int rst(INF);
for(int k = idx-1; sum[k+1][idx] <= class_time; k --) {
rst = min(rst, dfs(k, day-1)+cost(class_time-sum[k+1][idx]));
}
return dp[idx][day] = rst;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int residual, need_cnt, cas(1);
while( scanf("%d", &n) && n ) {
scanf("%d %d", &class_time, &base_cost);
residual = class_time; need_cnt = 1;
for(int i = 1; i <= n; i ++) {
scanf("%d", &t[i]);
if( residual >= t[i] ) {
residual -= t[i];
}
else {
residual = class_time-t[i]; need_cnt ++;
}
}
memset(sum, 0, sizeof(sum));
for(int i = 1; i <= n; i ++) {
for(int j = i, tmp = 0; j <= n; j ++) {
tmp += t[j]; sum[i][j] = tmp;
}
}
if( 1 != cas ) {
printf("\n");
}
memset(dp, -1, sizeof(dp)); printf("Case %d:\n", cas ++);
printf("Minimum number of lectures: %d\n", need_cnt);
printf("Total dissatisfaction index: %d\n", dfs(n, need_cnt));
}
return 0;
}
uva_607 - Scheduling Lectures( 贪心+DP )
最新推荐文章于 2015-03-16 19:58:50 发布