There is a production of yogurt company, continuous n weeks, the weekly need to ship Numi units, has been known every week the production unit yogurt price ci, and, yogurt can be produced in advance, but the storage cost is one unit per week s cost, ask the least cost.
For the yogurt to be shipped, either this week production, or advance production.
When do you use the production method?
If the goods in the first week of production in advance, assuming the production in J Week, then the cost is (I-J) *s+c[j]
If C[i]> (i-j) *s+c[j], then update c[i]= (I-J) *s+c[j]
Update to O (n^2)?
It can be proved that the optimal way of production is to either produce this week or produce it in the previous week (the previous week's C is already updated)
O (n) update the array C.
1#include <cstdio>2#include <cstring>3 4 using namespacestd;5 6 Const intmaxn=1e4+5;7 #defineLL Long Long8 9 intC[MAXN];Ten intNUM[MAXN]; One A intMain () - { - intn,s; the while(~SCANF ("%d%d",&n,&s)) - { - for(intI=1; i<=n;i++) -scanf"%d%d",&c[i],&num[i]); + - for(intI=2; i<=n;i++) + { A if(s+c[i-1]<C[i]) atc[i]=s+c[i-1]; - } -LL ans=0; - for(intI=1; i<=n;i++) - { -ans+= (LL) c[i]*Num[i]; in } -printf"%lld\n", ans); to } + return 0; -}
View Code
POJ 2393 Greedy Simple question