参考 http://www.cnblogs.com/Kiraa/p/5510757.html

http://www.cnblogs.com/zhaopAC/p/5159950.html

根据题目说明中的这句话

reduce the total system cost by eliminating some of the voltage sources and replacing the
lamps of that category with higher rating lamps.

可以判断某种电压的灯泡如果要换就全换,否则就不会eliminate某种电压。

这条题目的难点在于最优解符合一定的模式。

设d[i]为子问题灯泡1...i的最小开销,当前考虑灯泡种类i,d[i-1]已经是最优解。

编号1...(i-1)代表每种灯泡,按照电压从低到高排序,在最优解中,可能某个编号代表的灯泡已经被替换,但数量不变。

设每种灯泡的参数分别存放在V[],K[],C[],L[]这些数组中。

设 1<=j<=(i-1),j这种灯泡可以被替换成i这种灯泡,那么需要满足以下这些条件

1) V[j] < V[i]

2) K[j] + C[j]*L[j] < C[i]*L[j]

如果在编号 (j+1)...(i-1)这些种类的灯泡中有个编号x != j

因为1...(i-1)这些灯泡已经构成的最优解,j没有被x替换,那么需要满足以下这些条件

3) V[j] < V[x]

4) K[j] + C[j]*L[j] >= C[x]*L[j]

根据公式 2) 和 4)可知

C[x] <= C[i]

所以 K[x] + C[x]*L[x] < C[i]*L[x]

所以x这种灯泡也可以被替换成i

替换的模式是,如果某种灯泡可以被替换成i,那这种灯泡右边的灯泡都可以被替换成i

设s[i]为前i种灯泡的总数量,

d[i] = min{d[j] + (s[i]-s[j])*C[i] + K[i]} // 0 <= j <= (i-1)

1...j不替换,费用是d[j],(j+1)到(i-1)被替换成i,费用是(s[i]-s[j])*C[i] + K[i]

j = 0时,d[0] = 0,s[0] = 0,d[i] = s[i] * C[i] + K[i],相当于全部替换成i

#define _CRT_SECURE_NO_WARNINGS 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <numeric> using namespace std; struct Lamp {
int V; // 1 <= V <= 132000, voltage rating
int K; // 1 <= K <= 1000, cost of voltage source of this rating
int C; // 1 <= C <= 10, cost of a lamp of this rating
int L; // 1 <= L <= 100, number of lamps required
}; int n; // 1 <= n <= 1000
const int maxn = ;
Lamp a[maxn + ];
int s[maxn + ];
int d[maxn + ]; bool comp(const Lamp &a, const Lamp &b)
{
return a.V < b.V;
} int main()
{ while (scanf("%d", &n) && n) {
for (int i = ; i <= n; i++){
scanf("%d%d%d%d", &a[i].V, &a[i].K, &a[i].C, &a[i].L);
} sort(a + , a + n + , comp); // sort based on voltage s[] = ; // s[i] = sum of numbers of 1..i types of lamps
for (int i = ; i <= n; i++) {
s[i] = s[i - ] + a[i].L;
} d[] = ; // d[i] = minium cost with 1..i types of lamps
for (int i = ; i <= n; i++){
d[i] = a[i].K + a[i].C * s[i]; // replace all lamps with type i
for (int j = ; j < i; j++) {
d[i] = min(d[i], d[j] + (s[i] - s[j])*a[i].C + a[i].K); // replace j+1...i with type i
}
} printf("%d\n", d[n]);
} return ;
}

9-6 UVa 11400的更多相关文章

  1. Uva 11400,照明系统设计

    题目链接:https://uva.onlinejudge.org/external/114/11400.pdf 题意:有一个照明系统需要用到n种灯,每种灯的电压为V,电源费用K,每个灯泡费用为C,需要 ...

  2. UVa 11400 - Lighting System Design(线性DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. UVa 11400 Lighting System Design

    题意: 一共有n种灯泡,不同种类的灯泡必须用不同种电源,但同一种灯泡可以用同一种电源.每种灯泡有四个参数: 电压值V.电源费用K.每个灯泡的费用C.所需该种灯泡的数量L 为了省钱,可以用电压高的灯泡来 ...

  4. UVa 11400 Lighting System Design【DP】

    题意:给出n种灯泡,分别给出它们的电压v,电源费用k,每个灯泡的费用c,和所需灯泡的数量l,问最优方案的费用 看的紫书= = 首先是dp[i]为灯泡1到i的最小费用, dp[i]=min(dp[i], ...

  5. 【线性结构上的动态规划】UVa 11400 - Lighting System Design

    Problem F Lighting System Design Input: Standard Input Output: Standard Output You are given the tas ...

  6. uva 11400 Problem F Lighting System Design

    紫皮书题: 题意:让你设计照明系统,给你n种灯泡,每种灯泡有所需电压,电源,每个灯泡的费用,以及每个灯泡所需的数量.每种灯泡所需的电源都是不同的,其中电压大的灯泡可以替换电压小的灯泡,要求求出最小费用 ...

  7. UVA - 11400 Lighting System Design (区间DP)

    这个问题有两个点需要注意: 1. 对于一种灯泡,要么全换,要么全不换. 证明: 设一种灯泡单价为p1,电池价格为k1,共需要L个,若把L1个灯泡换成单价为p2,电池为k2的灯泡,产生的总花费为p1*L ...

  8. Uva 11400 照明系统

    有一个照明系统需要用到n种灯,每种灯的电压为V,电源费用K,每个灯泡费用为C,需要该灯的数量为L.注意到,电压相同的灯泡只需要共享一个对应的电源即可,还有电压低的灯泡可以被电压高的灯泡替代.为了节约成 ...

  9. UVa 11400 照明系统设计

    https://vjudge.net/problem/UVA-11400 题意: 有一个照明系统需要用到n种灯,每种灯的电压为V,电源费用K,每个灯泡费用为C,需要该灯的数量为L.注意到,电压相同的灯 ...

  10. uva 11400 - Lighting System Design(动态规划 最长上升子序列问题变型)

    本题难处好像是在于 能够把一些灯泡换成电压更高的灯泡以节省电源的钱 .所以也才有了对最优方案的探求 好的处理方法是依照电压从小到大排序.仅仅能让前面的换成后面的.也就满足了把一些灯泡换成电压更高的灯泡 ...

随机推荐

  1. 安装 Composer

    参考百度经验:http://jingyan.baidu.com/article/4f34706ed04013e386b56d72.html Composer下载:https://getcomposer ...

  2. 蚁群算法MATLAB解VRP问题

    Excel  exp12_3_2.xls内容: ANT_VRP函数: function [R_best,L_best,L_ave,Shortest_Route,Shortest_Length]=ANT ...

  3. 洛谷 P2955 [USACO09OCT]奇数偶数Even? Odd?【字符串/易错】

    题目描述 Bessie's cruel second grade teacher has assigned a list of N (1 <= N <= 100) positive int ...

  4. Redis-cli 命令不能用

    bash: redis-cli: command not found... 环境: Linux7.X 在运行redis-cli命令的时候提示错误: 解决方案: 1. wget http://downl ...

  5. fc_net.py cs231n

    n如果有错误,欢迎指出,不胜感激 import numpy as np from cs231n.layers import * from cs231n.layer_utils import * cla ...

  6. querySelector与getElementBy系列的区别

    getElementBy系列 document.getElementsByTagName('tag'); document.getElementById('id'); document.getElem ...

  7. span元素和div元素的浮动效果

    首先看一段代码: <style> #right {margin: 10px;float:right;color:red;} #left {float:left;color:blue;} & ...

  8. 模板内置函数(HTML)

    模板内置函数 注意:1.html书写避免多余的空格,否则可能无法被识别 2.模板是用来渲染的不要用来处理逻辑 后台ctime=datetime.datatime.now() {{ctime|date: ...

  9. jmeter测试APP时如何录制脚本

    jmeter录制脚本需要注意的点: (1)手机和电脑需要处于一个局域网内(如手机和电脑所使用一个wifi) (2)设置手机代理的时候手机IP填写本机IP,端口号要和jmeter的相同,一般情况下端口号 ...

  10. javascript如何将时间戳转为24小时制

    var now = new Date(parseInt(1446634507) * 1000);console.log(now.toLocaleString('chinese',{hour12:fal ...