zoukankan      html  css  js  c++  java
  • 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
    ", ans);
    42 }

  • 相关阅读:
    模型分离(选做)
    密码保护
    实现搜索功能
    完成个人中心—导航标签
    个人中心标签页导航
    评论列表显示及排序,个人中心显示
    完成评论功能
    从首页问答标题到问答详情页
    运行Junit单测时遇到的问题
    spring定时任务执行两次的原因与解决方法
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/4887206.html
Copyright © 2011-2022 走看看