(动态规划)UVA-11400:Lighting System Design
You are given the task to design a lighting system for a huge conference hall. After doing a lot of calculation and sketching, you have figured out the requirements for an energy-efficient design that can properly illuminate the entire hall. According to your design, you need lamps of n different power ratings. For some strange current regulation method, all the lamps need to be fed with the same amount of current. So, each category of lamp has a corresponding voltage rating. Now, you know the number of lamps and cost of every single unit of lamp for each category. But the problem is, you are to buy equivalent voltage sources for all the lamp categories. You can buy a single voltage source for each category (Each source is capable of supplying to infinite number of lamps of its voltage rating.) and complete the design.
你接到了为会议礼堂设计照明系统的任务,一番精算之后你已设计完毕。在你的设计中,需要n种不同能量级别的灯。而依据某神奇的电流定律,所有灯需要通过等量电流,所以每种灯有一个对应的电压等级。现在你知道每种灯的所需数量和单价,问题是你还得给每种灯买电压源。每种灯有它对应的电压源,一个源可以为无限盏灯供能。
But the accounts section of your company soon figures out that they might be able to reduce the total system cost by eliminating some of the voltage sources and replacing the lamps of that category with higher rating lamps. Certainly you can never replace a lamp by a lower rating lamp as some portion of the hall might not be illuminated then. You are more concerned about money-saving than energy-saving. Find the minimum possible cost to design the system.
然鹅财政部门要削减开支……为了省钱,可以把一些灯泡换成电压更高的灯泡,这样就省了好几种电压源的钱,因为只花了一份电压源的钱(看样例可知电压源贼贵)。算出最省钱方案。
Input
Each case in the input begins with n (1 ≤ n ≤ 1000), denoting the number of categories. Each of the following n lines describes a category. A category is described by 4 integers - V (1 ≤ V ≤ 132000), the voltage rating, K (1 ≤ K ≤ 1000), the cost of a voltage source of this rating, C (1 ≤ C ≤ 10), the cost of a lamp of this rating and L (1 ≤ L ≤ 100), the number of lamps required in this category. The input terminates with a test case where n = 0. This case should not be processed.
每组样例先给出n,代表种类数目。接下来的n行每行将依次给出这种灯的参数——V:电压级别、K:这种电压的电压源价格、C:这种灯泡的单价、L:所需这种灯泡的数量。
输入的n==0时结束输入。
Output
For each test case, print the minimum possible cost to design the system.
对于每组数据,输出最小花费。
Sample Input
3
100 500 10 20
120 600 8 16
220 400 7 18
0
Sample Output
778
样例解释:778 = 400 + (20+16+18)*7
只看表层题面不好解决,要进行一定的思考推演出万变情况中不变的规律,方可写出状态转移。
结论1:每种灯泡要么不换,要么全换。
如果单拿两种灯泡,我们什么情况换?在我们发现应该换的情况下,如何证明全换肯定比部分换省钱?不妨动笔写写。
结论2:现我们将输入按照电压等级从小到大排序后,j < i,如果 j 要换成 i,则从 j 到 i-1 都要换成 i。
假设 j 要换成 i,而 j+1 是独立不换的,j 换成 i 比换成 j+1更好,那么?
如果结论2成立,那么意味着这个升序列中有几个“断层”,它们之间的弱势灯泡要被替换为右侧大功率灯泡。首先,第n个由于功率最大,肯定是最后一个断层。然后,此时假设断层 i 的上一个断层为 j,由于 j 和 i 之间的灯泡都要换成 i,所以dp[i] = dp[j] + 灯泡数量*i的单价 + i的电压源价格。
该暴力的时候也得暴力一下,上一个断层 j 无法直接得出,可以遍历1~i-1看看哪种方案最优得之。
故给出状态转移方程dp[i] = min{ dp[j] + (s[i]-s[j])*C[i] + K[i] }。其中s[i]是灯泡数量前缀和。
注:对于上述结论、思路有疑义或想法的同学欢迎讨论!也欢迎分享其他解法,或者对题目难度进行反馈!
下面是我的代码,欢迎优化、指正:
#include <cstdio>
#include <cstring>
#include <algorithm> struct category
{
int V, K, C, L;
}a[]; int n;
int dp[], s[]; 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); std::sort(a+, a++n, [](const category& x, const category& y){return x.V < y.V;}); memset(dp, , sizeof(dp));
for (int i = ; i <= n; i++)
s[i] = s[i-] + a[i].L; for (int i = ; i <= n; i++)
for (int j = ; j < i; j++)
if (!dp[i] || dp[i] > dp[j] + (s[i]-s[j])*a[i].C + a[i].K)
dp[i] = dp[j] + (s[i]-s[j])*a[i].C + a[i].K; printf("%d\n", dp[n]);
}
}
(动态规划)UVA-11400:Lighting System Design的更多相关文章
- 【线性结构上的动态规划】UVa 11400 - Lighting System Design
Problem F Lighting System Design Input: Standard Input Output: Standard Output You are given the tas ...
- UVa 11400 Lighting System Design(DP 照明设计)
意甲冠军 地方照明系统设计 总共需要n不同类型的灯泡 然后进入 每个灯电压v 相应电压电源的价格k 每一个灯泡的价格c 须要这样的灯泡的数量l 电压低的灯泡能够用电压高的灯泡替换 ...
- uva 11400 - Lighting System Design(动态规划 最长上升子序列问题变型)
本题难处好像是在于 能够把一些灯泡换成电压更高的灯泡以节省电源的钱 .所以也才有了对最优方案的探求 好的处理方法是依照电压从小到大排序.仅仅能让前面的换成后面的.也就满足了把一些灯泡换成电压更高的灯泡 ...
- UVa 11400 - Lighting System Design(线性DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 11400 Lighting System Design
题意: 一共有n种灯泡,不同种类的灯泡必须用不同种电源,但同一种灯泡可以用同一种电源.每种灯泡有四个参数: 电压值V.电源费用K.每个灯泡的费用C.所需该种灯泡的数量L 为了省钱,可以用电压高的灯泡来 ...
- UVa 11400 Lighting System Design【DP】
题意:给出n种灯泡,分别给出它们的电压v,电源费用k,每个灯泡的费用c,和所需灯泡的数量l,问最优方案的费用 看的紫书= = 首先是dp[i]为灯泡1到i的最小费用, dp[i]=min(dp[i], ...
- UVA - 11400 Lighting System Design (区间DP)
这个问题有两个点需要注意: 1. 对于一种灯泡,要么全换,要么全不换. 证明: 设一种灯泡单价为p1,电池价格为k1,共需要L个,若把L1个灯泡换成单价为p2,电池为k2的灯泡,产生的总花费为p1*L ...
- UVA 11400 Lighting System Design 照明系统设计
首先是一个贪心,一种灯泡要么全都换,要么全都不换. 先排序,定义状态d[i]为前面i种灯泡的最小花费,状态转移就是从d[j],j<i,加上 i前面的j+1到i-1种灯泡换成i的花费. 下标排序玩 ...
- UVA 11400"Lighting System Design"
传送门 错误思路 正解 AC代码 参考资料: [1]:https://www.cnblogs.com/Kiraa/p/5510757.html 题意: 现给你一套照明系统,这套照明系统共包含 n 种类 ...
- UVA - 11400 Lighting System Design(照明系统设计)(dp)
题意:共有n种(n<=1000)种灯泡,每种灯泡用4个数值表示.电压V(V<=132000),电源费用K(K<=1000),每个灯泡的费用C(C<=10)和所需灯泡的数量L(1 ...
随机推荐
- HTML初级教程
1:标题h1~h6HTML标签有专门的标签处理你页面上的标题,它们是h1,h2,h3,h4,h5和h6,象封建社会一样,h1就是万能的君主而h6就是最底层的百姓. 注意,h1标签在一个页面只能使用一次 ...
- java中String.valueOf(obj)、(String)obj与obj.toString()有什么区别
方法1:采用 Object.toString()方法 在这种使用方法中,因为java.lang.Object类里已有public方法.toString(),所以对任何严格意义上的java对象都可以调用 ...
- 人生苦短之Python多线程
#encoding=utf-8 import threading import time ''' python多线程并不是真正意义上的多线程,通常我们所说的多线程是多个线程同时执行某功能,而在pyth ...
- php数组合并
php的数合并函数: array_merge($arr1, $arr2, ..., $arr{$n}); 如果数组的键名有重复,后面的会覆盖前面的. 如果键名是数字索引,则会重新排列索引,往后累加. ...
- DDD领域驱动之干货(二)
基于仓储的实现 1.前言:本着第一节写的有些糊涂,主要是自己喜欢实干,不太喜欢用文字表述,就这样吧.下面切入正题. 博客园里面有很多的大佬,我这里就不一一解释概览,有兴趣的朋友可以去看大佬们写的 ...
- 使用 WinSCP(下载) 上文件到 Linux图文教程
问题导读: 1.如何远程链接? 2.如何上传文件? 3.如何对立面的文件进行操作? 4.什么情况下会链接失败? https://yunpan.cn/cYWtNMycjeVPv 访问密码 4f7 ...
- Dom4J 解析xml ,类查询
/** * 从XML文件比对,传入provinceId 返回 provinceShortName * @param provinceid * @return */ public static Stri ...
- Spring 事务管理高级应用难点剖析: 第 3 部分
本文是“Spring 事务管理高级应用难点剖析” 系列文章的第 3 部分,作者将继续深入剖析在实际 Spring 事务管理应用中容易遇见的一些难点,包括在使用 Spring JDBC 时如果直接获取 ...
- RQNOJ魔法石之恋
魔法石之恋 (stone.pas/c/cpp) [问题描述] 在<Harry Potter and the Sorcerer's Stone>中,想得到魔法石,必须要通过许许多多的测试和游 ...
- JVM介绍(一)
JVM是运行在操作系统之上的,它与硬件没有直接的交互 类装载器ClassLoader 负责加载class文件,class文件在文件开头有特定的文件标示,并且ClassLoader只负责class文件的 ...