UVA-607-DP

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

Meaning of the questions: You are a professor, give you a topic N, each topic had talked about, but also does not allow dancing speaking, each lesson long as L, Tuotang not allowed, but there may be time remaining for discussion ,

But not much time remaining, or else students will not be happy. Unhappy formula is as follows.

 

 t is time remaining, C is unhappy degrees. Find the N tpoic finished, the total number of course it takes a minimum, the minimum degree students are not happy is.

1: spend a minimum of the total number of courses can be obtained greedy.

2: Students unhappy degrees minimum.

Set DP [i] denotes the i-th topic before unhappy minimum degree.

Then dp [i] = min (dp [j-1] + cost (topic [j] + ...... + topic [i])) where (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 (intI = . 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);
                     // greedy with a minimum of course 
                    IF (LEC [I]> LEC [J] + . 1 ) 
                    {
                        LEC [I] = LEC [J] + . 1 ; 
                        DP [I] = Val; 
                    } 
                    // DP, the same number of courses, the minimum degree of unhappy 
                    the 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;
}

 Input:

6
30 15
10
10
10
10
10
10

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

Output:

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

Case 2: Minimum number of lectures: 6 Total dissatisfaction index: 2700

 

Guess you like

Origin www.cnblogs.com/shuiyonglewodezzzzz/p/11531181.html