我的创作纪念日 && CF1620D Exact Change 题解

突然发现自己在 CSDN 已经创作了 128 128 128 天,时间流逝得好快。

在这个特殊的日子里,发篇题解纪念一下。

link

题面翻译

  • n n n 个物品,第 i i i 个物品价格为 a i a_i ai
  • 有三种货币,面值分别为 1 , 2 , 3 1,2,3 1,2,3
  • 求最小需要多少张货币,才能表出所有的 a i a_i ai
  • 多组测试数据。
  • 1 ≤ t ≤ 1000 , 1 ≤ n ≤ 100 , 1 ≤ a i ≤ 1 0 9 1 \leq t \leq 1000,1 \leq n \leq 100,1 \leq a_i \leq 10^9 1t1000,1n100,1ai109

题目描述

One day, early in the morning, you decided to buy yourself a bag of chips in the nearby store. The store has chips of n n n different flavors. A bag of the i i i -th flavor costs a i a_i ai burles.

The store may run out of some flavors, so you’ll decide which one to buy after arriving there. But there are two major flaws in this plan:

  1. you have only coins of 1 1 1 , 2 2 2 and 3 3 3 burles;
  2. since it’s morning, the store will ask you to pay in exact change, i. e. if you choose the i i i -th flavor, you’ll have to pay exactly a i a_i ai burles.

Coins are heavy, so you’d like to take the least possible number of coins in total. That’s why you are wondering: what is the minimum total number of coins you should take with you, so you can buy a bag of chips of any flavor in exact change?

输入格式

The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000 ) — the number of test cases.

The first line of each test case contains the single integer n n n ( 1 ≤ n ≤ 100 1 \le n \le 100 1n100 ) — the number of flavors in the store.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109 ) — the cost of one bag of each flavor.

输出格式

For each test case, print one integer — the minimum number of coins you need to buy one bag of any flavor you’ll choose in exact change.

样例 #1

样例输入 #1

4
1
1337
3
10 8 10
5
1 2 3 4 5
3
7 77 777

样例输出 #1

446
4
3
260

提示

In the first test case, you should, for example, take with you $ 445 $ coins of value 3 3 3 and 1 1 1 coin of value 2 2 2 . So, 1337 = 445 ⋅ 3 + 1 ⋅ 2 1337 = 445 \cdot 3 + 1 \cdot 2 1337=4453+12 .

In the second test case, you should, for example, take $ 2 $ coins of value 3 3 3 and 2 2 2 coins of value 2 2 2 . So you can pay either exactly 8 = 2 ⋅ 3 + 1 ⋅ 2 8 = 2 \cdot 3 + 1 \cdot 2 8=23+12 or 10 = 2 ⋅ 3 + 2 ⋅ 2 10 = 2 \cdot 3 + 2 \cdot 2 10=23+22 .

In the third test case, it’s enough to take 1 1 1 coin of value 3 3 3 and 2 2 2 coins of value 1 1 1 .

思路

注意到货币的面值只能是 1 , 2 , 3 1,2,3 1,2,3,就从这一点特殊性质入手。

可以发现,面值为 1 1 1 的纸币不会超过 2 2 2 张,因为 2 2 2 1 1 1 元纸币相当于 1 1 1 2 2 2 元纸币,显然后者更优。

同理,面值为 2 2 2 的纸币不会超过 3 3 3 张,因为 3 3 3 2 2 2 元纸币相当于 2 2 2 3 3 3 元纸币,显然后者更优。

然后暴力枚举即可~

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=2e5+10,inf=1e9;
ll T,n,a[N],res;
ll solve(ll t,ll t1,ll t2){
	ll ct=inf; 
	for(int i=0;i<=t1;i++)//面值为 1 的纸币数量
		for(int j=0;j<=t2;j++){//面值为 2 的纸币数量
			ll tmp=t-i-j*2;//剩余钱数
			if(tmp<0) continue;
			if(!(tmp%3)) ct=min(ct,tmp/3);//面值为 3 的纸币数量
		}
	return ct;
}
ll work(ll t1,ll t2){
	ll ans=-1;//面值为 3 的纸币数量最大值
	for(int i=1;i<=n;i++) ans=max(ans,solve(a[i],t1,t2));
	return ans+t1+t2;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--){
		cin>>n;res=inf;
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int i=0;i<=1;i++)
			for(int j=0;j<=2;j++) res=min(res,work(i,j));
		cout<<res<<"\n";
	}
	return 0;
}
  • 11
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值