Yogurt factory
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9380 Accepted: 4768Description
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 500Sample 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.
題意
每一週都有單位牛奶的生產費用,和需要向顧客送去的牛奶數量。並且,除了每週生產銷售的牛奶外,也可以選擇多生產牛奶,用作以後幾周的銷量,不過這期間需要支付保存的單位費用,問:最少花費。
分析
一個決策的問題。每一週的牛奶都可以決策在之前的周就提前生產出來。
無後效性。上一週的決策結果,不影響下一週的決策。
最優子結構。每一週都可以做出相同的決策過程,儘管結果可能不一樣。
很明顯是個dp的題。然而爲何,大多數的博客都是在寫貪心呢,爲什麼直接比較上一週就可以了。
如果你也這樣的疑問就,繼續看吧。
很明顯,狀態轉移方程爲:
dp[i] = min{dp[i], dp[i-1]+s, dp[i-2]+2*s,…….dp[0] + i * s};
其中dp[i] 表示第i天的最小花費, 初始化爲第i周生產的花費。s爲保存的花費每一週的。
一直比較,選取前幾週中,當週的花費 加上保存至今的 保存花費, 最少的總花費爲這一週的花費。
是不是很好理解呢?
將方程化簡一下:
dp[i] = min{dp[i], min{dp[i-1] , dp[i-2] +s …, dp[0] + (i-1)*s} +s } 相當於後面的部分提出了個 s;
而 dp[i-1] = min{dp[i-1], dp[i-1] +s, …… dp[0] + (i-1)*s} //有最初的狀態轉移方程遞推出來的。
所以呢, dp[i] = min{dp[i], dp[i-1]+s}
這就得到了很多人寫的式子了,因爲借用了之前的數據結果,所以看起來很是簡單。
而這樣的思想,其實在完全揹包用滾動數組實現的時候就有所體現了。
這裏寫出了,是爲了說明看似貪心的想法下,是dp思想的體現。
AC代碼:
/*===============================================================
* Copyright (C) 2016 All rights reserved.
*
* Filename: I.cpp
* Author: gsh
* Created_time:2016/8/19 星期五 14:51:15
* Problem_tags:
* Problem_keyword:
================================================================*/
#include <iostream>
#include <cstdio>
#include <cmath>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int maxn = 1e4 + 100;
int c[maxn], y[maxn];
int main()
{
#ifdef LOCAL
freopen("I.in","r",stdin);
freopen("I.out","w",stdout);
#endif
int n, s;
while(cin >> n >> s)
{
for(int i = 1; i <= n; i++)
cin >> c[i] >> y[i];
c[0] = 999999;
for(int i = 1; i <= n; i++)
c[i] = min(c[i], c[i-1]+s);
ll ans = 0;
for(int i = 0; i <= n; i++)
ans += c[i]*y[i];
cout << ans << endl;
}
return 0;
}
Go big or go home!!!