POJ 2393 Yogurt factory 贪心

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

Sample Input
4 5
88 200
89 400
97 300
91 500

Sample Output
126900

题意:

  有一个奶酪工厂,给出这个工厂每天加工每个奶酪需要的价格,以及每天的需求量,另外,奶酪也可以存放在仓库里,给出每个奶酪存放一天需要的价格,问,这些生产任务全部完成,最少的花费是多少?

思路:

  第一天的奶酪只能当天生产,这是无法贪心选取的,所以第一天只能取当天的值,第二天就可以选择第二天和第一天生产哪个更优了,同理第三天选择第一天第二天和第三天哪一个更优。第二天的价格是第二天的价格的和第一天生产的价格加上存储费用的较小的值。这样,每天都维护一个最小值即可,维护的时候只要和前一周比较就行了。比如第三周和第二周比较,因为第二周更新的时候取值就是第一周和第二周的最小值,第三周和第二周比较就是第三周与前两周的最小值比较,一次只需要和前一周比较。

代码:

#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[10010],cnt[10010];
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
        scanf("%d%d",cost+i,cnt+i);
    long long  ans=0;
    for(int i=1; i<n; i++)
        cost[i]=min(cost[i-1]+m,cost[i]);
    for(int i=0; i<n; i++)
        ans+=cost[i]*cnt[i];
    printf("%lld\n",ans);
    return 0;
}

 

  

posted @ 2017-07-21 17:41  爱国呐  阅读(588)  评论(0编辑  收藏  举报