7.23 贪心 Yogurt factory (POJ 2393)

题目:

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

翻译:

奶牛们有一个工厂用来生产奶酪,接下来的N周时间里,在第i周生产一份的奶酪需要花费Ci,同时它们也有一个储存室,奶酪放在那永远不会坏,并且可以无限放,每一单元奶酪放在那的价格恒定为每周s。然后奶牛在第i周会交付顾客Yi的奶酪,让你求最小花费。

思路:

只维护一个最小值,因为如果牛奶要保存,必须选取的是当前费用里,最小的哪一个。而它最小,就永远比之前的费用要小。

AC代码:

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

#pragma warning(disable:4996);
#define mem(sx,sy) memset(sx,sy,sizeof(sx))
#define INF 0x3f3f3f3f
typedef long long ll;
const ll MAXN = 1e6 + 5;
#define MAXSIZE 100

int main() {
	int n, s;
	while (cin >> n >> s) {
		int pre = INF;
		ll sum = 0;
		for (int i = 0; i < n; i++) {
			int c, y;
			cin >> c >> y;
			pre = min(pre, c);
			sum += y * pre;
			pre += s;
		}
		cout << sum << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_23502651/article/details/81196056