题目链接:

https://codeforces.com/problemset/problem/1620/D

题目大意:

商店有 \(n\) 个物品,你有1,2,3三个面值的硬币,问最少需要带多少硬币能买商店中的任何一件物品。

思路:

容易想到,每个物品最优的策略应该是用 \(\lfloor x / 3 \rfloor\) 个 3 块的硬币,若 \(x\) % 3 != 0 则再加上一个 1 或 2 的硬币,而 1 或 2 的硬币的个数的取值都只有 0、1、2 三种情况。
因为数据范围很小,只有 100,考虑用 暴力 跑出所有的可能方案,然后一个一个试。

代码:

#include <bits/stdc++.h>
using namespace std;
int T, n;
void solve(){
	scanf("%d", &n);
	int ans = 1e9;	//最少的硬币数量 
	vector <int> a(n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for (int num1 = 0; num1 <= 2; num1++)
		for (int num2 = 0; num2 <= 2; num2++){
			int num = 0;	//当前方案所需的3面值的硬币数量 
			for (int i = 0; i < n; i++){
				int k = 1e9;	//第 i 件物品花费的最少的3的硬币的数量 
				for (int x = 0; x <= num1; x++)
					for (int y = 0; y <= num2; y++){
						int p = a[i] - x - y * 2;	//花费3的硬币的钱 
						if (p % 3 == 0 && p >= 0)	//要大于0!!! 
							k = min(k, p / 3);
					}
				num = max(k, num);
			}
			ans = min(num + num1 + num2, ans);
		}
	cout << ans << "\n";
}
int main(){
	cin >> T;
	while(T--)
		solve();
	return 0;
}
posted on 2022-01-07 12:52  Hamine  阅读(161)  评论(0编辑  收藏  举报