zoukankan      html  css  js  c++  java
  • BZOJ 1680 [Usaco2005 Mar]Yogurt factory:贪心【只用考虑上一个】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1680

    题意:

      在接下来的n周内,第i周生产一吨酸奶的成本为c[i],订单为y[i]吨酸奶。

      酸奶可以提前生产,可以存放无限长的时间,存放一周的花费为s。

      问你在完成所有订单的前提下,最小的花费为多少。

    题解:

      贪心。

      p[i]代表第i周的最小成本。

      对于p[i],只用考虑p[i-1],因为已经保证了p[i-1]是i-1之前所有周的最优答案。

      所以转移为:p[i] = min(c[i], p[i-1]+s)。

      最小花费 = ∑ p[i]*y[i]。

    AC Code:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #define MAX_N 10005
     5 #define INF 10000000
     6 
     7 using namespace std;
     8 
     9 int n,s;
    10 int c[MAX_N];
    11 int y[MAX_N];
    12 int p[MAX_N];
    13 long long ans=0;
    14 
    15 void read()
    16 {
    17     cin>>n>>s;
    18     for(int i=1;i<=n;i++)
    19     {
    20         cin>>c[i]>>y[i];
    21     }
    22 }
    23 
    24 void solve()
    25 {
    26     p[0]=INF;
    27     for(int i=1;i<=n;i++)
    28     {
    29         p[i]=min(c[i],p[i-1]+s);
    30         ans+=p[i]*y[i];
    31     }
    32 }
    33 
    34 void print()
    35 {
    36     cout<<ans<<endl;
    37 }
    38 
    39 int main()
    40 {
    41     read();
    42     solve();
    43     print();
    44 }
  • 相关阅读:
    函数模板——隐式实例化、显式实例化、显式具体化
    SQLAlchemy
    pymysql的使用
    mysql 安装
    Django---Cerley使用
    支付宝支付功能
    Django--log配置
    Vue--基础
    Python学习手册
    针对特定网站scrapy爬虫的性能优化
  • 原文地址:https://www.cnblogs.com/Leohh/p/7634254.html
Copyright © 2011-2022 走看看