题目:
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.
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.
题意:
给定一个固定给的S作为一个周的存储成本(存储时间可以无限长),一共N个周,每个周有不同的C生产成本和生产量Y,需要我们求出最少的总成本。
理解:
首先考虑第一个周的成本,这肯定是固定的S1=Y1*C1;再考虑第二个周的成本,这时候有了一个矛盾,如果这个周的生产成本Y2*C2大于上个周提前生产并存储的总金额Y2*(C1+S)的话,那么提前一个周生产会有更低的生产成本;对与以后的每两个相邻的周来讲,如果前一个周的Ci+S < Ci+1的话,就可以放到上一个周来生产以降低成本;再考虑如果在这个周的上一个周之前有Ci-n + n*S < Ci的话,就可以一直在那个周的生产一直保存到这个周来销售。因此,定义一个最小单位成本mn,初始为C1,每次计算完这个周的就加上一个S,mn=C1+S,遇到mn < Ci就发生改变,然后继续进行操作。
代码如下:
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int N, S;
long long t = 0, mn = 2147486437;
cin >> N >> S; //输入周数和存储成本
for(int i = 0; i < N; i++)
{
int C, Y;
cin >> C >> Y; //输入生产成本和运输量
if(C > mn + S)
{
C = mn + S; //每次更新总成本
}
t = t + C * Y;
mn = C;
}
cout << t << endl;
}