贪心问题 POJ 2393 Yogurt factory

题目:http://poj.org/problem?id=2393

题意:N周,每周生成牛奶(任意!),每周成本为c_i(1~5000),每周出货 y_i;出货可以使用该周生产的,也可以用之前的储存的牛奶,每周存储 每单位牛奶需要 S 价格。问,N周最小的成本是多少?

题解:贪心策略,维持每周 的最低单位成本,本周的单位成本min(上周单位成本 + 存储S,本周成本)

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std; 

typedef long long LL;
const int maxn = 10000 + 50;

void solve()
{
    int N, S, C, Y;                     // N: N周,S:存储费用
    LL ans = 0;
    scanf("%d%d", &N, &S);

    int Pre = 5000;
    for (int i = 0; i < N; i++)
    {
        scanf("%d%d", &C, &Y);   // c_i:第i周成本, y_i: 第i周需要出货
        Pre = min(Pre + S, C); // min(上一次cost + 存储S, 此次成本) 当作此次的成本
        ans += Pre * Y;
    }
    cout << ans << endl;
}

int main()
{
    solve();
    return 0;
}

原文地址:https://www.cnblogs.com/douzujun/p/8511937.html

时间: 2024-10-13 11:21:53

贪心问题 POJ 2393 Yogurt factory的相关文章

poj 2393 Yogurt Factory(贪心)

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) c

poj 2393 Yogurt factory

http://poj.org/problem?id=2393 Yogurt factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7341   Accepted: 3757 Description The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the next N (1 <= N <= 10

POJ 2393 Yogurt factory【贪心】

POJ 2393 题意: 每周可以生产牛奶,每周生产的价格为Ci,每周需要上交的牛奶量Yi,你可以选择本周生产牛奶,也可选择提前几周生产出存储在仓库中(仓库无限大,而且保质期不考虑),每一周存仓库牛奶需要花费S元,让你求出所有周的需求量上交的最少花费. 分析: 因为第 i 周的奶酪,可以在第 i 周生产,也可以在前几周生产,然后储存.通过把s转化为花费,跟原有花费去比较,取一个最小值,这样从头到尾,每一周都可以取得一个花费的最小值.贪心求解. #include<cstdio> #include

POJ 2393 Yogurt factory(简单贪心)

http://poj.org/problem?id=2393 题意:任务规定,一个酸奶制造厂,在n个星期内,分别要向外提供y[i]unit的酸奶.已知这个制造厂第i周制造每unit酸奶的费用为c[i],储存室储存每1unit酸奶1星期的费用为s.问要完成这个任务的最小费用是多少. . . . . . . . . . . . . . 思路:简单贪心.维护一个目前最优的代价minc = min(c, minc + s),然后求和. #include <iostream> using namespa

poj 2393 Yogurt factory(dp+贪心)

奶牛们建了一家酸奶厂,在N周内每周需要出货Y_i单位酸奶,第i周成本为C_i,储存费为每周S.求总体最低成本. 贪心策略是维持每周的最低单位成本,每周可能用上周剩下的,也可能生产新的.于是该周单位成本可能为上一周的单位成本加上储存费,也可能为该周的单位成本. 1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #in

poj 2393 Yogurt factory(贪心)

传送门 解题思路 对于第\(i\)周来说,最小的花费 \(sum=min(y[i]*(c[j]+s(i-j)))(1<=j<=i)\),所以\(sum=min(y[i]*(c[j]-s[j]+s[i])\),发现\(y[i]\)与\(s[i]\)均为定值,就是让最小化\(c[j]-s[j]\),然后就直接维护个最小值每次更新答案即可. 代码 #include<iostream> #include<cstdio> #include<cstring> #incl

Yogurt factory(POJ 2393 贪心 or DP)

Yogurt factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8205   Accepted: 4197 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

POJ2393 Yogurt factory 【贪心】

Yogurt factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6821   Accepted: 3488 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

bzoj 1680\1740 : [Usaco2005 Mar]Yogurt factory 贪心 双倍经验

1680: [Usaco2005 Mar]Yogurt factory 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 com