来源第 504 场周赛 Q3难度中等分数2215
题目描述
给你一个二维整数数组 items,其中 items[i] = [factori, pricei] 表示下标为 i 的物品。同时给你一个整数 budget。
每种物品都有无限个可供购买。你可以购买任意数量的任意物品,但购买物品的总花费最多为 budget。
购买物品后,你可以根据以下规则获得免费的物品:
- 购买的每一份物品
i 最多 可以让你获得 一份 免费的其他物品 j。 - 免费物品必须满足
i != j 且 factori 可以整除 factorj。 - 对于每个有序对
(i, j),无论你购买了多少个物品 i,你从物品 i 的购买中 最多只能一次 免费获得物品 j。 - 如果免费物品
j 是通过购买不同种类的物品获得的,那么同一种物品 j 可以被免费获得多次。
返回你在购买物品花费最多为 budget 的前提下,能够获得的 物品最大总数 ,包括购买的物品和免费的物品。
示例 1:
输入: items = [[1,6],[2,4],[3,5]], budget = 19
输出: 5
解释:
- 你可以购买 2 个物品 0 和 1 个物品 1,总花费为
2 * 6 + 4 = 16,不超过 budget = 19。 - 购买的其中 1 个物品 0 可以免费获得 1 个物品 1,因为
factor0 = 1 可以整除 factor1 = 2。 - 购买的另一个物品 0 可以免费获得 1 个物品 2,因为
factor0 = 1 可以整除 factor2 = 3。 - 你最终拥有 3 个购买的物品和 2 个免费物品,总共 5 个物品。
示例 2:
输入: items = [[2,8],[1,10],[6,6],[4,12],[5,20],[5,17]], budget = 35
输出: 7
解释:
- 你可以购买 2 个物品 0、1 个物品 1 以及 1 个物品 2,总花费为
2 * 8 + 10 + 6 = 32,不超过 budget = 35。 - 购买的其中 1 个物品 0 可以免费获得 1 个物品 2,因为
factor0 = 2 可以整除 factor2 = 6。 - 购买的另一个物品 0 可以免费获得 1 个物品 3,因为
factor0 = 2 可以整除 factor3 = 4。 - 购买的 1 个物品 1 可以免费获得 1 个物品 2,因为
factor1 = 1 可以整除 factor2 = 6。 - 购买物品 2 没有获得免费物品,因为
factor2 = 6 不能整除任何其他物品的 factor。 - 你最终拥有 4 个购买的物品和 3 个免费物品,总共 7 个物品。
提示:
1 <= items.length <= 105 items[i] = [factori, pricei] 1 <= factori <= items.length 1 <= pricei <= 109 1 <= budget <= 109
解法
方法一:贪心
思考
\(n\) 达到 \(10^5\),预算达到 \(10^9\),按预算做背包无法通过。
买下一件物品,最多再白拿一件因子被它整除、且不是它自己的物品,每种有序对只能用一次。因此一件物品能触发的赠送次数,等于因子是它的倍数的物品种数减一。
一次购买加上一次赠送得到两件物品,只有单价严格小于两件最便宜物品的花费时才更划算。这种搭配越便宜越好,所以按单价从低到高取。
先按倍数把每个因子的赠送额度算出来,再按价格排序。当前额度买不满时,剩下的钱已经不够再买同样贵或更贵的搭配,于是停下,余额全部用来买最便宜的物品。
买下一件因子为 \(f\) 的物品,可以免费获得另一件物品:两者不是同一种,且 \(f\) 整除对方的因子,每种有序对最多使用一次。令 \(\textit{freq}[f]\) 为因子等于 \(f\) 的物品种数, \(\textit{divCount}[f]\) 为因子是 \(f\) 的倍数的物品种数。一件因子为 \(f\) 的物品最多带来 \(\textit{divCount}[f]-1\) 次赠送。 \(\textit{divCount}\) 按倍数枚举得到。
记最低单价为 \(p_{\min}\)。花 \(p\) 买一件并拿到一件赠品,得到两件物品;直接买两件最便宜的物品要花 \(2p_{\min}\)。因此只考虑 \(p<2p_{\min}\) 且赠送次数为正的物品,并按 \(p\) 从小到大购买。设当前物品的赠送额度为 \(g\),还能买 \(t=\min(g,\lfloor\textit{budget}/p\rfloor)\) 件,答案增加 \(2t\),预算减去 \(tp\)。若 \(t<g\),剩余预算已经小于 \(p\),后面的物品不会更便宜,停止。最后把剩余预算全部用来购买单价为 \(p_{\min}\) 的物品。
时间复杂度 \(O(n\log n)\),空间复杂度 \(O(n)\)。其中 \(n\) 是物品种数。
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 | class Solution:
def maximumSaleItems(self, items: list[list[int]], budget: int) -> int:
n = len(items)
freq = [0] * (n + 1)
min_price = float('inf')
for factor, price in items:
freq[factor] += 1
if price < min_price:
min_price = price
div_count = [0] * (n + 1)
for f in range(1, n + 1):
if freq[f]:
for m in range(f, n + 1, f):
div_count[f] += freq[m]
sorted_items = sorted((price, div_count[factor] - 1) for factor, price in items)
ans = 0
rem = budget
limit = 2 * min_price
for price, gain in sorted_items:
if price >= limit:
break
if gain <= 0:
continue
can_buy = min(gain, rem // price)
ans += can_buy * 2
rem -= can_buy * price
if can_buy < gain:
break
ans += rem // min_price
return ans
|
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 | class Solution {
public int maximumSaleItems(int[][] items, int budget) {
int n = items.length;
int[] freq = new int[n + 1];
int minPrice = Integer.MAX_VALUE;
for (int[] item : items) {
freq[item[0]]++;
minPrice = Math.min(minPrice, item[1]);
}
int[] divCount = new int[n + 1];
for (int f = 1; f <= n; f++) {
if (freq[f] == 0) continue;
for (int m = f; m <= n; m += f) {
divCount[f] += freq[m];
}
}
int[][] sortedItems = new int[n][2];
for (int i = 0; i < n; i++) {
sortedItems[i][0] = items[i][1];
sortedItems[i][1] = divCount[items[i][0]] - 1;
}
Arrays.sort(sortedItems, Comparator.comparingInt(a -> a[0]));
long rem = budget;
long ans = 0;
long limit = 2L * minPrice;
for (int[] item : sortedItems) {
int price = item[0];
int gain = item[1];
if (price >= limit) break;
if (gain <= 0) continue;
long canBuy = Math.min((long) gain, rem / price);
ans += canBuy * 2;
rem -= canBuy * price;
if (canBuy < gain) break;
}
ans += rem / minPrice;
return (int) ans;
}
}
|
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 | class Solution {
public:
long long maximumSaleItems(vector<vector<int>>& items, int budget) {
int n = items.size();
vector<int> freq(n + 1, 0);
int min_price = 2e9;
for (const auto& item : items) {
freq[item[0]]++;
min_price = min(min_price, item[1]);
}
vector<int> div_count(n + 1, 0);
for (int f = 1; f <= n; ++f) {
if (freq[f] == 0) continue;
for (int m = f; m <= n; m += f) {
div_count[f] += freq[m];
}
}
vector<pair<int, int>> sorted_items;
sorted_items.reserve(n);
for (const auto& item : items) {
sorted_items.push_back({item[1], div_count[item[0]] - 1});
}
sort(sorted_items.begin(), sorted_items.end());
long long rem = budget;
long long ans = 0;
for (const auto& [price, gain] : sorted_items) {
if (price >= 2LL * min_price) break;
if (gain <= 0) continue;
long long can_buy = min((long long) gain, rem / price);
ans += can_buy * 2;
rem -= can_buy * price;
if (can_buy < gain) break;
}
ans += rem / min_price;
return ans;
}
};
|
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 | func maximumSaleItems(items [][]int, budget int) int {
n := len(items)
freq := make([]int, n+1)
minPrice := int(2e9)
for _, item := range items {
freq[item[0]]++
if item[1] < minPrice {
minPrice = item[1]
}
}
divCount := make([]int, n+1)
for f := 1; f <= n; f++ {
if freq[f] == 0 {
continue
}
for m := f; m <= n; m += f {
divCount[f] += freq[m]
}
}
type pair struct {
price int
gain int
}
sortedItems := make([]pair, n)
for i, item := range items {
sortedItems[i] = pair{price: item[1], gain: divCount[item[0]] - 1}
}
sort.Slice(sortedItems, func(i, j int) bool {
return sortedItems[i].price < sortedItems[j].price
})
rem := int64(budget)
var ans int64 = 0
limit := int64(2) * int64(minPrice)
for _, item := range sortedItems {
if int64(item.price) >= limit {
break
}
if item.gain <= 0 {
continue
}
canBuy := rem / int64(item.price)
if int64(item.gain) < canBuy {
canBuy = int64(item.gain)
}
ans += canBuy * 2
rem -= canBuy * int64(item.price)
if canBuy < int64(item.gain) {
break
}
}
ans += rem / int64(minPrice)
return int(ans)
}
|
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 | function maximumSaleItems(items: number[][], budget: number): number {
const n = items.length;
const freq = new Array<number>(n + 1).fill(0);
let minPrice = Infinity;
for (const item of items) {
freq[item[0]]++;
if (item[1] < minPrice) {
minPrice = item[1];
}
}
const divCount = new Array<number>(n + 1).fill(0);
for (let f = 1; f <= n; f++) {
if (freq[f] === 0) {
continue;
}
for (let m = f; m <= n; m += f) {
divCount[f] += freq[m];
}
}
const sortedItems = items.map(item => [item[1], divCount[item[0]] - 1]);
sortedItems.sort((a, b) => a[0] - b[0]);
let rem = budget;
let ans = 0;
const limit = 2 * minPrice;
for (const [price, gain] of sortedItems) {
if (price >= limit) {
break;
}
if (gain <= 0) {
continue;
}
const canBuy = Math.min(gain, Math.floor(rem / price));
ans += canBuy * 2;
rem -= canBuy * price;
if (canBuy < gain) {
break;
}
}
ans += Math.floor(rem / minPrice);
return ans;
}
|
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 | impl Solution {
pub fn maximum_sale_items(items: Vec<Vec<i32>>, budget: i32) -> i32 {
let n = items.len();
let mut freq = vec![0i64; n + 1];
let mut min_price = i64::MAX;
for item in &items {
let factor = item[0] as usize;
let price = item[1] as i64;
freq[factor] += 1;
min_price = min_price.min(price);
}
let mut div_count = vec![0i64; n + 1];
for f in 1..=n {
if freq[f] == 0 {
continue;
}
for m in (f..=n).step_by(f) {
div_count[f] += freq[m];
}
}
let mut sorted_items: Vec<(i64, i64)> = items
.iter()
.map(|item| (item[1] as i64, div_count[item[0] as usize] - 1))
.collect();
sorted_items.sort_unstable();
let mut ans = 0i64;
let mut rem = budget as i64;
let limit = 2 * min_price;
for (price, gain) in sorted_items {
if price >= limit {
break;
}
if gain <= 0 {
continue;
}
let can_buy = gain.min(rem / price);
ans += can_buy * 2;
rem -= can_buy * price;
if can_buy < gain {
break;
}
}
ans += rem / min_price;
ans as i32
}
}
|
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 | var maximumSaleItems = function (items, budget) {
const n = items.length;
const freq = new Int32Array(n + 1);
let minPrice = Infinity;
for (let i = 0; i < n; i++) {
freq[items[i][0]]++;
if (items[i][1] < minPrice) minPrice = items[i][1];
}
const divCount = new Int32Array(n + 1);
for (let f = 1; f <= n; f++) {
if (freq[f] === 0) continue;
for (let m = f; m <= n; m += f) {
divCount[f] += freq[m];
}
}
const sortedItems = items.map(([factor, price]) => [price, divCount[factor] - 1]);
sortedItems.sort((a, b) => a[0] - b[0]);
let rem = budget;
let ans = 0;
const limit = 2 * minPrice;
for (let i = 0; i < n; i++) {
const [price, gain] = sortedItems[i];
if (price >= limit) break;
if (gain <= 0) continue;
const canBuy = Math.min(gain, Math.floor(rem / price));
ans += canBuy * 2;
rem -= canBuy * price;
if (canBuy < gain) break;
}
ans += Math.floor(rem / minPrice);
return ans;
};
|
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 | public class Solution {
public int MaximumSaleItems(int[][] items, int budget) {
int n = items.Length;
int[] freq = new int[n + 1];
int minPrice = int.MaxValue;
foreach (var item in items) {
freq[item[0]]++;
if (item[1] < minPrice) {
minPrice = item[1];
}
}
int[] divCount = new int[n + 1];
for (int f = 1; f <= n; f++) {
if (freq[f] == 0) {
continue;
}
for (int m = f; m <= n; m += f) {
divCount[f] += freq[m];
}
}
var sortedItems = new (int price, int gain)[n];
for (int i = 0; i < n; i++) {
sortedItems[i] = (items[i][1], divCount[items[i][0]] - 1);
}
Array.Sort(sortedItems, (a, b) => a.price.CompareTo(b.price));
long rem = budget;
long ans = 0;
long limit = 2L * minPrice;
foreach (var (price, gain) in sortedItems) {
if (price >= limit) {
break;
}
if (gain <= 0) {
continue;
}
long canBuy = Math.Min((long)gain, rem / price);
ans += canBuy * 2;
rem -= canBuy * price;
if (canBuy < gain) {
break;
}
}
ans += rem / minPrice;
return (int)ans;
}
}
|
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 | class Solution {
function maximumSaleItems($items, $budget) {
$n = count($items);
$freq = array_fill(0, $n + 1, 0);
$minPrice = PHP_INT_MAX;
foreach ($items as $item) {
$freq[$item[0]]++;
if ($item[1] < $minPrice) {
$minPrice = $item[1];
}
}
$divCount = array_fill(0, $n + 1, 0);
for ($f = 1; $f <= $n; $f++) {
if ($freq[$f] === 0) {
continue;
}
for ($m = $f; $m <= $n; $m += $f) {
$divCount[$f] += $freq[$m];
}
}
$sortedItems = [];
foreach ($items as $item) {
$sortedItems[] = [$item[1], $divCount[$item[0]] - 1];
}
usort($sortedItems, fn($a, $b) => $a[0] <=> $b[0]);
$rem = $budget;
$ans = 0;
$limit = 2 * $minPrice;
foreach ($sortedItems as [$price, $gain]) {
if ($price >= $limit) {
break;
}
if ($gain <= 0) {
continue;
}
$canBuy = min($gain, intdiv($rem, $price));
$ans += $canBuy * 2;
$rem -= $canBuy * $price;
if ($canBuy < $gain) {
break;
}
}
$ans += intdiv($rem, $minPrice);
return $ans;
}
}
|