POJ 2393 Yogurt Factory(贪心 ?? dp)
来源:互联网 发布:精功科技收购盘古数据 编辑:程序博客网 时间:2024/05/16 06:13
Yogurt factory
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9380 Accepted: 4768Description
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) cents to produce one unit of yogurt in week i. Yucky’s factory, being well-designed, can produce arbitrarily many units of yogurt each week.Yucky Yogurt owns a warehouse that can store unused yogurt at a constant fee of S (1 <= S <= 100) cents per unit of yogurt per week. Fortuitously, yogurt does not spoil. Yucky Yogurt’s warehouse is enormous, so it can hold arbitrarily many units of yogurt.
Yucky wants to find a way to make weekly deliveries of Y_i (0 <= Y_i <= 10,000) units of yogurt to its clientele (Y_i is the delivery quantity in week i). Help Yucky minimize its costs over the entire N-week period. Yogurt produced in week i, as well as any yogurt already in storage, can be used to meet Yucky’s demand for that week.
Input
* Line 1: Two space-separated integers, N and S.
* Lines 2..N+1: Line i+1 contains two space-separated integers: C_i and Y_i.Output
* Line 1: Line 1 contains a single integer: the minimum total cost to satisfy the yogurt schedule. Note that the total might be too large for a 32-bit integer.Sample Input
4 5
88 200
89 400
97 300
91 500Sample Output
126900
Hint
OUTPUT DETAILS:
In week 1, produce 200 units of yogurt and deliver all of it. In week 2, produce 700 units: deliver 400 units while storing 300 units. In week 3, deliver the 300 units that were stored. In week 4, produce and deliver 500 units.
题意
每一周都有单位牛奶的生产费用,和需要向顾客送去的牛奶数量。并且,除了每周生产销售的牛奶外,也可以选择多生产牛奶,用作以后几周的销量,不过这期间需要支付保存的单位费用,问:最少花费。
分析
一个决策的问题。每一周的牛奶都可以决策在之前的周就提前生产出来。
无后效性。上一周的决策结果,不影响下一周的决策。
最优子结构。每一周都可以做出相同的决策过程,尽管结果可能不一样。
很明显是个dp的题。然而为何,大多数的博客都是在写贪心呢,为什么直接比较上一周就可以了。
如果你也这样的疑问就,继续看吧。
很明显,状态转移方程为:
dp[i] = min{dp[i], dp[i-1]+s, dp[i-2]+2*s,…….dp[0] + i * s};
其中dp[i] 表示第i天的最小花费, 初始化为第i周生产的花费。s为保存的花费每一周的。
一直比较,选取前几周中,当周的花费 加上保存至今的 保存花费, 最少的总花费为这一周的花费。
是不是很好理解呢?
将方程化简一下:
dp[i] = min{dp[i], min{dp[i-1] , dp[i-2] +s …, dp[0] + (i-1)*s} +s } 相当于后面的部分提出了个 s;
而 dp[i-1] = min{dp[i-1], dp[i-1] +s, …… dp[0] + (i-1)*s} //有最初的状态转移方程递推出来的。
所以呢, dp[i] = min{dp[i], dp[i-1]+s}
这就得到了很多人写的式子了,因为借用了之前的数据结果,所以看起来很是简单。
而这样的思想,其实在完全背包用滚动数组实现的时候就有所体现了。
这里写出了,是为了说明看似贪心的想法下,是dp思想的体现。
AC代码:
/*===============================================================* Copyright (C) 2016 All rights reserved.* * Filename: I.cpp* Author: gsh* Created_time:2016/8/19 星期五 14:51:15* Problem_tags:* Problem_keyword:================================================================*/#include <iostream>#include <cstdio>#include <cmath>typedef long long ll;typedef unsigned long long ull;using namespace std;const int maxn = 1e4 + 100;int c[maxn], y[maxn];int main(){#ifdef LOCAL freopen("I.in","r",stdin); freopen("I.out","w",stdout);#endif int n, s; while(cin >> n >> s) { for(int i = 1; i <= n; i++) cin >> c[i] >> y[i]; c[0] = 999999; for(int i = 1; i <= n; i++) c[i] = min(c[i], c[i-1]+s); ll ans = 0; for(int i = 0; i <= n; i++) ans += c[i]*y[i]; cout << ans << endl; } return 0;}
Go big or go home!!!
- POJ 2393 Yogurt factory(贪心 or DP)
- POJ 2393 Yogurt Factory(贪心 ?? dp)
- POJ 2393 Yogurt factory(贪心)
- Poj 2393 Yogurt factory【贪心】
- POJ 2393 Yogurt factory 贪心
- POJ 2393 Yogurt factory(贪心)
- 【POJ】2393-Yogurt factory 贪心
- poj 2393 —— Yogurt factory (贪心)
- POJ 2393-Yogurt factory 贪心算法
- POJ 2393 Yogurt factory(贪心)
- POJ 2393 Yogurt factory(简单贪心)
- POJ 2393 - Yogurt factory(贪心)
- POJ 2393 Yogurt factory(贪心)
- POJ 2393 Yogurt factory 【贪心 水】
- POJ 2393 Yogurt factory(贪心)
- POJ 2393-Yogurt factory(贪心)
- POJ 2393 Yogurt factory (贪心)
- POJ 2393 Yogurt factory (贪心)
- Android studio系统类报红,让我setup jdk的解决办法
- Android 列表
- 如何在Snap包中定义全局的plug
- 推理集 —— 特殊的时间
- 海思3518c普通串口更换485通讯(启用rtsn管脚)-内核代码修改
- POJ 2393 Yogurt Factory(贪心 ?? dp)
- TCP/IP 扫盲教程
- 最近公共祖先
- 牛客网-Java-字符统计
- Spring3自定义环境配置 <beans profile="">
- js实现div的显示与隐藏
- 堆排序JAVA版实现
- java冒泡排序演示
- 不使用临时表,仅使用select实现查询出多行常数