POJ 2393 Greedy Simple question

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.