UVa11400 Lighting System Design

参考了这个 点击打开链接才会的题
刚开始写动归思维转不过来,中间的推理和证明没什么感觉。

#include <iostream>
#include <algorithm>
using namespace std;

const int MAXN = 1000 + 5;

struct info {
	int V;
	int K;
	int C;
	int L;
};

info plan[MAXN];
int quantity[MAXN] = { 0 };//灯泡数量累计
int total_cost[MAXN] = { 0 };

//从小到大排序意味着每次替换都必须购买voltage
bool compare(info& a, info& b) {
	return a.V <= b.V;
}

void quantity_ini(int n) {
	quantity[0] = plan[0].L;
	for (int i = 1; i < n; i++)
		quantity[i] = quantity[i - 1] + plan[i].L;
}

int redesign(info a[], int n) {
	quantity_ini(n);
//	bool adopted[MAXN] = { 0 };//标记已经选用的voltage
	for (int i = 0; i < n; i++) {
		total_cost[i] = a[i].K + a[i].C*quantity[i];
		for (int j = 1; j < i; j++) {//全i(即j = 0)的情况已经考虑过了
			int temp = total_cost[j] + (quantity[i] - quantity[j])*a[i].C + a[i].K;
			if (total_cost[i] > temp)
				total_cost[i] = temp;
		}
	}
	return 0;
}

int main() {
	int n;
	while (cin >> n && n != 0) {
		for (int i = 0; i < n; i++)
			cin >> plan[i].V >> plan[i].K >> plan[i].C >> plan[i].L;
		sort(plan, plan + n, compare);
		redesign(plan, n);
		cout << total_cost[n-1] << endl;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值