-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP1208.cpp
101 lines (76 loc) · 2.9 KB
/
P1208.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
// 表示一位农民
struct Peasant
{
int price; // 单价
int amount; // 一天最多能提供的牛奶量
bool operator<(const Peasant &obj)
{
return price < obj.price;
}
};
int main()
{
int n, m;
scanf("%d %d", &n, &m);
vector<Peasant> providers(m);
for (int i = 0; i < m; i++)
scanf("%d %d", &providers[i].price, &providers[i].amount);
sort(providers.begin(), providers.end()); // 按牛奶单价升序排序,让定价低的农民先提供牛奶
int cost = 0; // 总费用
for (int i = 0; i < m; i++)
{
if (n >= providers[i].amount) // 如果剩余的需求量比当前农户提供的量大
{
n -= providers[i].amount; // 这位农户提供出一天能卖出的所有量
cost += providers[i].amount * providers[i].price;
}
else // 剩余需求量不足当前农户提供的量
{
cost += n * providers[i].price; // 从这位农户这买n份牛奶即可。
break;
}
}
printf("%d", cost);
return 0;
}
/*
这题是比较基础的贪心算法题,每次决策的时候只【找当前定价最低】且牛奶尚有余量的农户。
- SomeBottle 2023.3.12
*/
/*
# [USACO1.3]混合牛奶 Mixing Milk
## 题目描述
由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要。帮助 Marry 乳业找到最优的牛奶采购方案。
Marry 乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格可能相同。此外,就像每头奶牛每天只能挤出固定数量的奶,每位奶农每天能提供的牛奶数量是一定的。每天 Marry 乳业可以从奶农手中采购到小于或者等于奶农最大产量的整数数量的牛奶。
给出 Marry 乳业每天对牛奶的需求量,还有每位奶农提供的牛奶单价和产量。计算采购足够数量的牛奶所需的最小花费。
注:每天所有奶农的总产量大于 Marry 乳业的需求量。
## 输入格式
第一行二个整数 $n,m$,表示需要牛奶的总量,和提供牛奶的农民个数。
接下来 $m$ 行,每行两个整数 $p_i,a_i$,表示第 $i$ 个农民牛奶的单价,和农民 $i$ 一天最多能卖出的牛奶量。
## 输出格式
单独的一行包含单独的一个整数,表示 Marry 的牛奶制造公司拿到所需的牛奶所要的最小费用。
## 样例 #1
### 样例输入 #1
```
100 5
5 20
9 40
3 10
8 80
6 30
```
### 样例输出 #1
```
630
```
## 提示
【数据范围】
对于 $100\%$ 的数据:
$0 \le n,a_i \le 2 \times 10^6$,$0\le m \le 5000$,$0 \le p_i \le 1000$
题目翻译来自 NOCOW。
USACO Training Section 1.3
*/