poj2393

題目:

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

  • 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 Input
    4 5
    88 200
    89 400
    97 300
    91 500
    Sample Output
    126900

這是一道貪心題,每周都可以生產(chǎn)許多牛奶,每周的牛奶都有生產(chǎn)成本,每周都需要上交牛奶,可以是本周的也可以是前面的,剩余的牛奶可以存儲(chǔ)起來(lái),問最小成本。

這道題可以將本周的成本與上周的生產(chǎn)成本和存儲(chǔ)成本之和作比較,找出最小成本。

參考代碼:

#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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容