poj2393

2016-07-15  本文已影响107人  科学旅行者

题目:

Yogurt factory
Time Limit: 1000MS* Memory Limit:* 65536K*
Total Submissions:* 9212* Accepted:* 4691
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

这是一道贪心题,每周都可以生产许多牛奶,每周的牛奶都有生产成本,每周都需要上交牛奶,可以是本周的也可以是前面的,剩余的牛奶可以存储起来,问最小成本。

这道题可以将本周的成本与上周的生产成本和存储成本之和作比较,找出最小成本。

参考代码:

#include <iostream>
using namespace std;
struct yogurt {
    int price;
    int units;
}you[10005];
int min(int a,int b) {
    if (a > b) {
        return b;
    }
    else {
        return a;
    }
}
long long result(yogurt you[],int week,int s) {
    long long res = 0;
    int minc = 9999;
    for (int i = 0;i < week;++i) {
        you[i].price = min(minc+s,you[i].price);
        res += you[i].price * you[i].units;
        minc = you[i].price;
    }
    return res;
}
int main() {
    int week,s;
    while (cin >> week >> s) {
        for (int i = 0;i < week;++i) {
            cin >> you[i].price >> you[i].units;
        }
        long long res = result(you,week,s);
        cout << res << endl;
    }
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读