Lighting System Design UVA

题目:题目链接

思路:简单的动态规划问题,先把灯泡按照电压从小到大排序。设s[i]为前i种灯泡的总数量(即L值之和),d[i]为灯 泡1~i的最小开销,则d[i] = min{d[j] + (s[i]-s[j])*c[i] + k[i])},表示前j个先用最优方案 买,然后第j+1~i个都用第i号的电源。答案为d[n]。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <cmath>

#define INF 0x3f3f3f3f

#define FRER() freopen("in.txt", "r", stdin);
#define FREW() freopen("out.txt", "w", stdout);

using namespace std;

struct lamp {
    int v, k, c, l;
    bool operator < (const lamp& temp) const {
        return v < temp.v;
    }
};

const int maxn = 1000 + 5;

lamp light[maxn];
int n, dp[maxn], s[maxn];

int main()
{
    // FRER();
    // FREW();
    while(cin >> n, n) {
        for(int i = 1; i <= n; ++i) 
            cin >> light[i].v >> light[i].k >> light[i].c >> light[i].l;
        sort(light + 1, light + n + 1);
        for(int i = 1; i <= n; ++i) 
            s[i] = s[i - 1] + light[i].l;

        for(int i = 1; i <= n; ++i) {
            dp[i] = s[i] * light[i].c + light[i].k;
            for(int j = 1; j < i; ++j) 
                dp[i] = min(dp[i], dp[j] + (s[i] - s[j])*light[i].c + light[i].k);
        }
        cout << dp[n] << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/fan-jiaming/p/10059767.html