贪心问题 POJ 2393 Yogurt factory

题目:http://poj.org/problem?id=2393

 

题意:N周,每周生成牛奶(任意!),每周成本为c_i(1~5000),每周出货 y_i;出货可以使用该周生产的,也可以用之前的储存的牛奶,每周存储 每单位牛奶需要 S 价格。问,N周最小的成本是多少?

题解:贪心策略,维持每周 的最低单位成本,本周的单位成本min(上周单位成本 + 存储S,本周成本)

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std; 

typedef long long LL;
const int maxn = 10000 + 50;

void solve()
{
    int N, S, C, Y;                     // N: N周,S:存储费用 
    LL ans = 0;
    scanf("%d%d", &N, &S);
    
    int Pre = 5000;
    for (int i = 0; i < N; i++)
    {
        scanf("%d%d", &C, &Y);   // c_i:第i周成本, y_i: 第i周需要出货 
        Pre = min(Pre + S, C); // min(上一次cost + 存储S, 此次成本) 当作此次的成本 
        ans += Pre * Y; 
    }
    cout << ans << endl;
}

int main()
{
    solve();
    return 0;
}

 

posted @ 2018-03-05 23:10  douzujun  阅读(251)  评论(0编辑  收藏  举报