贪心算法HURUST题目

题目描述:

Yogurt factory

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

-----------------------
这道题是我在HURBST做的题目,链接是https://vjudge.net/contest/262212#problem/D 中文理解:有一个酸奶酪生产商,每一个星期都有其需求量和价格量,奶酪可以保存到下个星期使用,不过有保存费用,此题目要求你找出最小费用的解; 个人看法:这道题目是贪心的基础题目,它满足了贪心算法的基于当前是最优解,找出下一个最优解,保证这一步是最优解。
 #include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<iterator>
#include<string>
#include<string.h>
#include<ctype.h>
#include<map>
#include<stack>
#include<queue>
#include<iostream>
#include<time.h> using namespace std; #define rep(i ,a, b) for(int i = a; i <= b; i++)
#define per(i, a, b) for(int i = a; i <= b; i--)
typedef long long ll;//数据量大,用longlong来进行运算较合适。不然会数据溢出。 struct milk
{
ll c_i, y_i, id;
}a[]; ll findmin(ll n, ll s)
{
ll ans=a[].c_i*a[].y_i, k = ;
for(ll i = ; i < n; i++)
{
ll temp = a[k].c_i+s*(i- a[k].id), cur = a[i].c_i;//记录前一周牛奶的价格和这个一周的价格,进行比较然后,选取较小的作为当前的最小解;
if(temp < cur)
{
ans += temp*a[i].y_i;
}
else
{
ans += cur*a[i].y_i;
k = i;
}
}
return ans;
} int main()//这是贪心问题。
{
ll n, s;
while(scanf("%lld%lld", &n, &s) != EOF)
{
for(ll i =; i < n; i++)
{
scanf("%lld%lld", &a[i].c_i, &a[i].y_i);
a[i].id = i;
}
printf("%lld\n", findmin(n, s));
}
return ;
}

 
上一篇:【问题记录】eclipse启动web项目时,spring会初始化两次


下一篇:【转载】GET和POST两种基本请求方法的区别