当前位置: 首页 > 工具软件 > Yogurt > 使用案例 >

Yogurt factory(POJ-2393)(贪心)

伏德义
2023-12-01

Yogurt factory
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15204 Accepted: 7601
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) 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 500
    Sample 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.
    Source
    USACO 2005 March Gold
    题目大意:
    奶牛们收购了一家世界著名的酸奶工厂Yucky Yogurt. 在接下来的 N (1 <= N <= 10,000) 周,牛奶和人工的价格每周会波动,以致于第i周需要花公司 C_i (1 <= C_i <= 5,000) 美分来生产一个单位的酸奶. Yucky factory被奶牛们照顾得很好,所以每周可以生产很多单位的酸奶

Yucky Yogurt 拥有一个仓库,可以以S (1 <= S <= 100)美分每单位每周的价格储存没用的酸奶。神奇的是,酸奶不会变质。而且仓库十分巨大,可以容纳很多牛奶

Yucky Yogurt每周要交货 Y_i (0 <= Y_i <= 10,000) 单位的酸奶给它的客户。请你帮助奶牛们减少整个 N-week 期间的支出. i周生产的牛奶和之前储存的牛奶都可以用来交i周的货
Input

  • 第一行:N and S.

  • 第 2…N+1行:第 i+1 行包括 : C_i 和 Y_i.
    Output

  • 满足客户需求的最小花费,保证不超过64位整数
    Sample Input
    4 5
    88 200
    89 400
    97 300
    91 500
    Sample Output
    126900
    Hint
    输出提示:
    第一周生产200单位,全部售出。第二周生产700单位,售出400,储存300.第三周使用储存的300单位。第四周,生产500单位并全部售出
    注释:
    yucky意为难以下咽的

这个题主要在于确定每月的最小C_i的值,在这个地方我是纠结了半天,以为是个难题,后来发现这道题很水。
由于是多组输入,而且这道题测试数据很多,所以开数组时一定要定义为全局变量,要不然会超时,我在这个地方WA了很多次…

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=10011;
int c[maxn],y[maxn];//开数组时一定要定义为全局变量,要不然会超时
int main()
{
	int n,s;
	while(cin>>n>>s)
	{
		for(int i=1;i<=n;i++)
		{
			cin>>c[i]>>y[i];//输入每月的c_i值和y_i值
		}
		long long ans=0;
		for(int i=2;i<=n;i++)
		{
			c[i]=min(c[i-1]+s,c[i]);//关键点:(贪心思想)确定每月的最小c_i值,后面的就容易了
		}
		for(int i=1;i<=n;i++)
		{
			ans+=c[i]*y[i];//计算每月成本并加和
		}
		cout<<ans<<endl;
	}
}
 类似资料: