The difficulty of this question seems to be that some bulbs can be replaced with bulbs with higher voltage to save money on power supply, so we have to explore the optimal solution.
A good solution is to sort the voltage from small to large, so we can only replace the previous one with the following one.Change some bulbs to bulbs with higher voltageRequirements;
A voltage bulb can be switched either. If it is to be switched, it should be switched. If it is to be switched, it means that the current power supply is not worth it. If it is not worth it, it should be switched out to avoid using the current power supply, otherwise, the lightbulb cost will be increased, and the power cost will not be reduced...
For details about status transfer, refer to the code.
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 1010; struct lamp {int v, k, C, L ;} A [maxn]; int N, V1, K1, C1, L1; int s [maxn]; int d [maxn]; bool CMP (lamp A1, lamp A2) {return a1.v <a2.v;} int main () {While (scanf ("% d", & N )! = EOF) {If (n = 0) break; memset (S, 0, sizeof (s); memset (D, 0, sizeof (d )); memset (A, 0, sizeof (a); For (INT I = 1; I <= N; I ++) {scanf ("% d", & V1, & K1, & C1, & L1); A [I]. V = V1; A [I]. k = k1; A [I]. C = C1; A [I]. L = L1;} Sort (a + 1, A + n + 1, CMP); s [0] = 0; For (INT I = 1; I <= N; I ++) {s [I] = s [I-1] + A [I]. l;} d [0] = 0; For (INT I = 1; I <= N; I ++) {for (Int J = 0; j <I; j ++) {If (j =-0) d [I] = (s [I]) * A [I]. c + A [I]. k; else d [I] = min (d [J] + (s [I]-s [J]) * A [I]. c + A [I]. k, d [I]) ;}} printf ("% d \ n", d [N]);} return 0 ;}