UVa 607 - Scheduling Lectures

題目:假如你是個教授,要安排幾個lecture,有n個topic(長度不一)需要在lecture中講述,

            lecture的長度的固定的(L),問怎麼安排topic使得lecture數量最小,不滿意度最小;

            不滿意度與課程結束剩餘時間t有關:

            ① 1 ≤ t ≤ 10時為 -C(常數);

            ② 超過10分鐘為(t-10)^2;

            ③ 正好完成為0;

            這裡只要求計算出最下的lecture數量和對應不滿意度的最小值。

分析:動態規劃(dp)。和編書的題目類似。

            這裡需要注意:第一個標準是lecture數量最少,在這個前提下計算最小的不滿意度;

            1.計算最小的lecture數量:

               狀態定義:lecture[i]為前i個topic安排的最少lecture數量,topic[i]為第i個topic耗時;

               轉移方程:lecture[i] = min(lecture[j] + 1)   

                                   {其中0 ≤ j ≤ i,topic[j+1] + .. + topic[i] ≤ L}

            2.計算最小的不滿意度(lecture數量相同時):

               狀態定義:dissatis[i]為前i個topic安排的最小不滿意度,topic[i]為第i個topic耗時;

               轉移方程:dissatis[i] = min(dissatis[j] + cost(topic[j+1] + .. + topic[i]))  

                                   {其中0 ≤ j ≤ i,topic[j+1] + .. + topic[i] ≤ L}

說明:數據間有空行,UVa又上不去了╮(╯▽╰)╭。

#include <stdio.h>
#include <stdlib.h>

int topic[1001];
int dissatis[1001];
int lectures[1001];

int cost(int C, int L, int t)
{
    if (L == t) {
        return 0;
    }else if (L-t <= 10) {
        return -C;
    }else {
        return (L-t-10)*(L-t-10);
    }
}

int main()
{
    int n, L, C, cases = 0;
    while (~scanf("%d",&n) && n) {
        scanf("%d%d",&L,&C);
        for (int i = 1; i <= n; ++ i) {
            scanf("%d",&topic[i]);
        }
        
        lectures[0] = 0;
        dissatis[0] = 0;
        for (int i = 1; i <= n; ++ i) {
            lectures[i] = lectures[i-1]+1;
            dissatis[i] = dissatis[i-1]+cost(C, L, topic[i]);
            int sum = topic[i];
            for (int j = i-1; j >= 0; -- j) {
                int value = cost(C, L, sum);
                if (lectures[i] > lectures[j]+1) {
                    lectures[i] = lectures[j]+1;
                    dissatis[i] = dissatis[j]+value;
                }else if (lectures[i] == lectures[j]+1 && 
                            dissatis[i] > dissatis[j]+value) {
                    dissatis[i] = dissatis[j]+value;
                }
                sum += topic[j];
                if (sum > L) {
                    break;
                }
            }
        }
        
        if (cases ++) {
            puts("");
        }
        printf("Case %d:\n",cases);
        printf("Minimum number of lectures: %d\n",lectures[n]);
        printf("Total dissatisfaction index: %d\n",dissatis[n]);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值