Educational Codeforces Round 119 (Rated for Div. 2) D. Exact Change

题目链接:https://codeforces.com/contest/1620/problem/D

题目大意
在一个商店中贩卖 n 种商品,但可能会缺货,且购买物品不找零(必须付正好的钱)。
一个有三种货币,面值分别为 1,2,3。
求最少带多少个货币去购物,可以保证最少买到一个物品。

思路
分析三种货币,显然是尽可能使用面值为 3 的货币是好的,所以其他面值的货币使用的个数不会大于 2。
所以,枚举全部的组合即可。

本题比较特殊的情况为:需要支付 4 块钱时,使用 2 + 2 还是 3 + 1。但是,枚举全部组合以后,这个问题不攻自破。

AC代码

#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std; 

const int maxn = 2e5 + 5;

int a[105];
vector<int> b;
int flag, ans;

void check(int n){
	ans = 0;
	for(int i = 1; i <= n; i++){
		int mn = 1e9;
		for(auto x : b){
			if(a[i] - x >= 0 && (a[i] - x) % 3 == 0){
				mn = min(mn, (a[i] - x) / 3);
			}
		}
		if(mn == 1e9){
			flag = 0;
			return;
		}
		ans = max(ans, mn);
	}
}

int main(int argc, char** argv) {
	
	int ncase;
	cin >> ncase;
	
	while(ncase--){
		int n;
		cin >> n;
		for(int i = 1; i <= n; i++) cin >> a[i];

		int res = 1e9; 
		b = {0,1,2,3,4,5,6}; int cnt = 4; // 1 1 2 2 
		flag = 1; check(n);
		if(flag) res = min(res, ans+cnt);
		
		b = {0,1,2,3,4,5}; cnt = 3;	// 1 2 2 
		flag = 1; check(n);
		if(flag) res = min(res, ans+cnt);
		
		b = {0,2,4}; cnt = 2; // 2 2 
		flag = 1; check(n);
		if(flag) res = min(res, ans+cnt);
		
		b = {0,1,2,3}; cnt = 2; // 1 2
		flag = 1; check(n);
		if(flag) res = min(res, ans+cnt);
		
		b = {0,1}; cnt = 1; // 1
		flag = 1; check(n);
		if(flag) res = min(res, ans+cnt);
		
		b = {0,2}; cnt = 1; // 2
		flag = 1; check(n);
		if(flag) res = min(res, ans+cnt);
		
		b = {0}; cnt = 0; // 0
		flag = 1; check(n);
		if(flag) res = min(res, ans+cnt);
		
		cout << res << endl;
	}
		
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值