Description
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
* Lines 2..N+1: Line i+1 contains two space-separated integers: C_i and Y_i.
Output
Sample
Sample Input Sample Output
题意:
有一个奶酪工厂,给出这个工厂每天加工每个奶酪需要的价格,以及每天的需求量,另外,奶酪也可以存放在仓库里,给出每个奶酪存放一天需要的价格,问,这些生产任务全部完成,最少的花费是多少?
思路:
第一天的奶酪只能当天生产,这是无法贪心选取的,所以第一天只能取当天的值,第二天就可以选择第二天和第一天生产哪个更优了,同理第三天选择第一天第二天和第三天哪一个更优。第二天的价格是第二天的价格的和第一天生产的价格加上存储费用的较小的值。这样,每天都维护一个最小值即可,维护的时候只要和前一周比较就行了。比如第三周和第二周比较,因为第二周更新的时候取值就是第一周和第二周的最小值,第三周和第二周比较就是第三周与前两周的最小值比较,一次只需要和前一周比较。
代码:
#include<stdio.h>
#include<stack>
#include<algorithm>
#include<queue>
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
int main()
{
int n,m;
int cost[],cnt[];
scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
scanf("%d%d",cost+i,cnt+i);
long long ans=;
for(int i=; i<n; i++)
cost[i]=min(cost[i-]+m,cost[i]);
for(int i=; i<n; i++)
ans+=cost[i]*cnt[i];
printf("%lld\n",ans);
return ;
}