Greedy:Yogurt factory(POJ 2393)

                

                  酸奶工厂

  题目大意:酸奶工厂每个星期都要制造酸奶,成本每单位x,然后每个星期要生产y,然后酸奶厂有个巨大的储存室,可以无限储存酸奶,而且酸奶的品质不会变坏,每天储存要每单位花费S,求最小的成本。

  简直是初中生数学题,贪心法即可,假如当前酸奶成本是X,则如果储存酸奶,则下k个星期的(如果还使用这批酸奶),则成本为x+k*s,对比k个星期的成本就可以确定用哪个了。

  水题

 1 #include <iostream>
 2 #include <functional>
 3 #include <algorithm>
 4 
 5 typedef struct demand
 6 {
 7     int cost;
 8     int units;
 9 }Week;
10 
11 Week work[10000];
12 
13 void Search(const int, const int);
14 
15 int main(void)
16 {
17     int N, S;
18     while (~scanf("%d%d", &N, &S))
19     {
20         for (int i = 0; i < N; i++)
21             scanf("%d%d", &work[i].cost, &work[i].units);
22         Search(N, S);
23     }
24     return 0;
25 }
26 
27 void Search(const int N, const int S)
28 {
29     int stop, now_cost, j;
30     long long ans = 0;
31 
32     for (int pos = 0; pos < N; )
33     {
34         now_cost = work[pos].cost;
35         for (stop = pos + 1, j = 1; stop < N && now_cost + j*S < work[stop].cost; j++, stop++);
36 
37         for (int k = 0; k < j; k++)//i是当前位置
38             ans += (long long)((now_cost + S*k) * work[pos + k].units);
39         pos = stop;
40     }
41     printf("%lld\n", ans);
42 }

posted @ 2015-10-17 11:41  PhiliAI  阅读(252)  评论(0编辑  收藏  举报