UVA 11400 Lighting System Design (DP)

题意:给出一些灯泡,每个灯泡有四个参数,电压,所需电源价格,当前灯泡价格,需要购买的数量。还有一些条件,为了省钱,低电压的灯泡可以替换成高电压的灯泡,每种灯泡都只能对应唯一的电源,不同的灯泡之间是不能互相使用电源的。

解法:题目还是相对简单的,首先我们用前缀和得到前n种灯泡的数量,且显然,如果一种灯泡要转为更高电压的灯泡,肯定是全部一起转的。我们不断枚举区间,代表这个区间的灯泡都用第i种灯泡。然后dp[i] = min(dp[i], dp[j - 1] + (cnt[i] - cnt[j - 1]) * c[i] + k[i])

代码如下:

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
int dp[maxn], cnt[maxn];

struct NODE {
	int v, k, c, l;
	bool operator < (const NODE &t) const {
		if(v < t.v)
			return 1;
		return 0;
	}
}light[maxn];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif
	int n;
	while(scanf("%d", &n) != EOF) {
		if(n == 0)
			break;
		for(int i = 1; i <= n; i++)
			scanf("%d%d%d%d", &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++)
			cnt[i] = cnt[i - 1] + light[i].l;
		for(int i = 1; i <= n; i++) {
			dp[i] = INF;
			for(int j = 1; j <= i; j++) {
				dp[i] = min(dp[i], dp[j - 1] + (cnt[i] - cnt[j - 1]) * light[i].c + light[i].k );
			}
		}
		printf("%d\n", dp[n]);
	} 
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值