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. 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.
Output
For each test case, print the minimum possible cost to design the system.
The question is from here.
My Solution
首先这个题目是要保持总灯泡数不变的,一定是换一个上去才可以,是在这个情况下money-saving,而不管energy-saving。所以把功率小的一些灯泡换成大灯lamp以节约voltage source的钱。
把灯泡的规格按电压从小到大排列,d[ i ] 表示前i种计划中的灯泡的最低总消费。
d[ i ] = min{d[ j ] + (s[ i ] - s[ j ])*c[ i ] + k[ i ] } 这里i > j;
#include <iostream> #include <cstdio> //#define LOCAL using namespace std; int d[1002],v[1002],vt[1002],k[1002],kt[1002],c[1002],ct[1002],l[1002],lt[1002], s[1002]; void merge_sort(int x, int y) { if(y-x > 1){ int m = x + (y-x)/2; int p = x, q = m, i = x; merge_sort(x,m); merge_sort(m,y); while(p < m || q < y){ if(q >= y || (p < m && v[p] <= v[q])) { vt[i] = v[p]; kt[i] = k[p]; ct[i] = c[p]; lt[i] = l[p]; i++;p++; //最开始的时候这个 ++ 是写在下标里面的常规写法,但这里4个数组按v[]排序,所以只能拿出来单独 ++ 了 } else{ vt[i] = v[q]; kt[i] = k[q]; ct[i] = c[q]; lt[i] = l[q]; i++;q++; } } for(int i = x; i < y; i++) { v[i] = vt[i]; k[i] = kt[i]; c[i] = ct[i]; l[i] = lt[i]; } } } int main() { #ifdef LOCAL freopen("a.txt","r",stdin); #endif // LOCAL int n,sumt; while(scanf("%d",&n)&&n != 0){ sumt = 0; for(int i = 1; i <= n; i++){ scanf("%d%d%d%d",&v[i],&k[i],&c[i],&l[i]); } merge_sort(1,n+1); for(int i = 1; i <= n; i++){ sumt += l[i]; s[i] = sumt; } d[0] = 0;s[0] = 0; for(int i = 1; i <= n; i++){ d[i] = d[i-1] + (s[i] - s[i-1])*c[i] + k[i]; for(int j = 0; j < i; j++){ d[i] = min(d[i], d[j] + (s[i]-s[j])*c[i] + k[i]); } } printf("%d\n", d[n]); } return 0; }
另外也可以用sort(),自己写个类,并定义好比较用的谓词。
#include <iostream> #include <algorithm> using namespace std; struct kind{ int v,k,c,l; } a[1002]; bool compare(const kind &x, const kind &y) //!作为sort的谓词使用 { return x.v < y.v; } int main() { int n=0; sort(a,a+n,compare); return 0; }