贪心 POJ 2393

来源:互联网 发布:手机怎么在淘宝上购物 编辑:程序博客网 时间:2024/06/07 02:40

题意:
你每周可以生产牛奶,每周生产的价格为Ci,每周需要上交的牛奶量Yi,你可以选择本周生产牛奶,也可选择提前几周生产出存储在仓库中(仓库无限大,而且保质期不考虑),每一周存仓库牛奶需要花费S元,让你求出所有周的需求量上交的最少花费。
题解:
决定你是否提前生产就是看单价,实际上对于每一次的比较只需要比较当前值和以前的最优解,如果比最优解小,那么此时他变成当前最优解。以此类推

#include <iostream>#include <algorithm>#include <stdlib.h>#include <math.h>#include <queue>using namespace std;typedef long long ll;int main(){    int n,s;    cin>>n>>s;    ll cost[10000];    ll need[10000];    ll ans = 0;    for(int i = 0;i<n;i++){        cin>>cost[i]>>need[i];    }    int best = 0;    for(int i =1;i<n;i++){        if(cost[i]>=(i-best)*s+cost[best]){            cost[i] = (i-best)*s+cost[best];        }        else{            best = i;        }    }    for(int i = 0 ;i<n;i++){        ans += need[i]*cost[i];    }    cout<<ans<<endl;}
0 0
原创粉丝点击