Yogurt factory
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice OpenJ_Bailian 2393
Description
The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the next N (1 <= N <= 10,000) weeks, the price of milk and labor will fluctuate weekly such that it will cost the company C_i (1 <= C_i <= 5,000) cents to produce one unit of yogurt in week i. Yucky‘s factory, being well-designed, can produce arbitrarily many units of yogurt each week.
Yucky Yogurt owns a warehouse that can store unused yogurt at a constant fee of
S (1 <= S <= 100) cents per unit of yogurt per week. Fortuitously, yogurt
does not spoil. Yucky Yogurt‘s warehouse is enormous, so it can hold
arbitrarily many units of yogurt.
Yucky wants to find a way to make weekly deliveries of Y_i (0 <= Y_i <=
10,000) units of yogurt to its clientele (Y_i is the delivery quantity in week
i). Help Yucky minimize its costs over the entire N-week period. Yogurt
produced in week i, as well as any yogurt already in storage, can be used to
meet Yucky‘s demand for that week.
Input
* Line 1: Two space-separated integers, N
and S.
* Lines 2..N+1: Line i+1 contains two space-separated integers: C_i and Y_i.
Output
* Line 1: Line 1 contains a single integer:
the minimum total cost to satisfy the yogurt schedule. Note that the total
might be too large for a 32-bit integer.
Sample Input
4 5
88 200
89 400
97 300
91 500
Sample Output
126900
Hint
OUTPUT DETAILS:
In week 1, produce 200 units of yogurt and deliver all of it. In week 2,
produce 700 units: deliver 400 units while storing 300 units. In week 3,
deliver the 300 units that were stored. In week 4, produce and deliver 500
units.
题意:
一工厂要在N周的时间生产一些单位的奶酪去销售。已知从第i(1<=i<=N)周生产一个单位奶酪的成本价格Ci和第i周的奶酪需求量Yi。奶酪可以储存,每单位的奶酪储存一周的花费为S,也就是说本周要销售的奶酪可以提前几周生产出来。问满足所有需求的最小成本是多少。
输入:
第一行N和S。之后N行是每一周的成本价和需求量。
输出:
最小成本。
分析:
使用贪心的思想。首先,第1周所需的奶酪必须要在第1周生产。之后的N-1周,每天需要的奶酪要么全部在当周生产,要么全部提前生产。如果当周生产比较省钱,那么当周的需求就在当周生产,并且假设如果之后的需求需要提前生产那么就在这一周生产,因为储存的价格是累周增加的。
1 #include <cstdio> 2 #define MAX_N 10000 3 #define ll long long 4 ll N,S,ans; 5 struct week{ll p,need;}x[MAX_N + 1]; 6 int main(){ 7 scanf("%I64d%I64d",&N,&S); 8 for(ll i = 0 ; i < N ; i++) 9 scanf("%I64d%I64d",&x[i].p,&x[i].need); 10 ans = x[0].need * x[0].p; 11 ll index = 0; 12 for(ll i = 1 ; i < N ; i++){ 13 ll pro_bef = x[index].p + S * (i - index),pro_now = x[i].p; 14 if(pro_bef < pro_now) ans += pro_bef * x[i].need; 15 else ans += pro_now * x[i].need,index = i; 16 } 17 printf("%lld\n",ans); 18 return 0; 19 }