总时间限制: 1000ms 内存限制: 65536kB
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.
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.
4 5
88 200
89 400
97 300
91 500
126900
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 单元的牛奶需要花费 C i C_i Ci 的费用,并且第 i 个礼拜配送 Y i Y_i Yi 单元的牛奶。可能第 i 个星期生产的牛奶无法配送完,需要进行储存,并且每单元牛奶储存每储存一个星期需要花费 S 的费用。现在给定每个星期生产牛奶的费用 C i C_i Ci 与 配送的牛奶单元数 Y i Y_i Yi,需要注意的是配送的 Y i Y_i Yi 可能是前面未配送完的。你需要做的就是找到一种安排,使得费用最少。
先考虑第 i 个礼拜配送的牛奶数 Y i Y_i Yi,那么这 Y i Y_i Yi 单元的牛奶只有两种可能:
那么对于上述两种情况,在第 i 个礼拜需要的费用为:
prev
就是其生产的费用那么,对于每个礼拜都选取上述两种情况中的较小值,就能得到总的最小值,这也就是 贪心。
prev
的操作:
prev
更新为 C_i
S
,因此 prev
更新为 prev+S
(这里 +S 是具有连续性的,也就是牛奶被转存多个星期也会被记录到,但是之前的会被当做生产费用计算)#include <cstdio>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
long long c, y, ans;
int N, S;
int main(){
scanf("%d %d", &N, &S);
long long prev = 1e10;
for(int i = 0; i < N; i++){
scanf("%lld %lld", &c, &y);
prev = min(S + prev, c);
ans += prev * y;
}
printf("%lld", ans);
return 0;
}