UVA-607-DP

https://vjudge.net/problem/UVA-607  

题意:你是一个教授,给你N个topic,每个topic都得讲到,而且还不允许跳着讲,每节课的时长为L,不允许拖堂,但是可以有剩余时间用于讨论,

但是不能剩余太多的时间,要不然学生会不开心。不开心的公式如下。

 t为剩余时间,C为不开心度。求N个tpoic讲完,花费的课程总数最少时,学生不开心度的最小值是多少。

1:花费课程总数最少,可以贪心求出。

2:学生不开心度最小。

设dp[i]表示前i个topic的最小不开心度。

那么dp[i]=min(dp[j-1]+cost(topic[j]+......+topic[i])) 其中 (topic[j]+......+topic[i])<=L

#include "pch.h"
#include <string>
#include<iostream>
#include <sstream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
    using std::cout;
    using std::endl;
    using std::cin;
    using std::map;
    using std::vector;
    using std::string;
    using std::sort;
    using std::priority_queue;
    using std::greater;
    using std::vector;
    using std::swap;
    using std::stack;
    using std::bitset;
    using std::stringstream;



    constexpr int N = 1005;



    int n;

    int topic[N];
    int lec[N];
    int dp[N];
    int L, C;

    int cost(int l, int c, int sum)
    {
        int  t = l - sum;
        if (t == 0)
        {
            return 0;
        }
        else if (1 <= t && t <= 10)
        {
            return -c;
        }
        return (t - 10)*(t - 10);
    }


    void solve()
    {
        int t = 0;
        while (cin >> n)
        {
            if (n == 0)
            {
                break;
            }
            cin >> L >> C;
            for (int i = 1; i <= n; i++)
            {
                cin >> topic[i];
            }
            lec[0] = 0;
            dp[0] = 0;
            for (int i = 1; i <= n; i++)
            {
                lec[i] = lec[i - 1] + 1;
                dp[i] = dp[i - 1] + cost(L, C, topic[i]);
                int sum = topic[i];
                for (int j = i - 1; j >= 0; j--)
                {
                    int val = dp[j] + cost(L, C, sum);
                    //贪心使用最少的课程
                    if (lec[i] > lec[j] + 1)
                    {
                        lec[i] = lec[j] + 1;
                        dp[i] = val;
                    }
                    //dp,相同课程数目,最小的不开心度
                    else if (lec[i] == lec[j] + 1 && dp[i] > val)
                    {
                        dp[i] = val;
                    }
                    sum = sum + topic[j];
                    if (sum > L)
                        break;
                }
            }
            if (t != 0)
            {
                cout << endl;
            }
            ++t;
            cout << "Case "<<t<<":" << endl;
            cout << "Minimum number of lectures: " << lec[n] << endl;
            cout << "Total dissatisfaction index: " << dp[n] << endl;
        }

    }

};


int main()
{

#ifndef ONLINE_JUDGE
    freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
    cc::solve();

    return 0;
}

 输入:

6
30 15
10
10
10
10
10
10

10
120 10
80
80
10
50
30
20
40
30
120
100
0

输出:

Case 1:
Minimum number of lectures: 2
Total dissatisfaction index: 0

Case 2: Minimum number of lectures: 6 Total dissatisfaction index: 2700
原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/11531181.html