当前位置: 首页 > news >正文

我的创作纪念日 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;
}

相关文章:

  • Python - 处理电子书的库
  • 【代码随想录训练营】【Day 49+】【动态规划-8】| Leetcode 121, 122, 123
  • C#使用OpenXml读取Word、PPT、Excel文档内容
  • linux pip 离线安装
  • 2024-6-10-zero shot,few shot以及无监督学习之间的关系是什么
  • NettyのBufferChannelSelector用法
  • 2024年春季学期《算法分析与设计》练习13
  • opencv中的图像操作
  • 端口占用多:UE4/UE5像素流送云推流时如何优化端口使用?
  • mac无法读取windows分区怎么办 苹果硬盘怎么读取
  • Android SDK版本号与API Level 的对应关系
  • ctfshow-web入门-命令执行(web53-web55)
  • 数据结构:手撕代码——顺序表
  • 【Java】解决Java报错:IllegalArgumentException
  • 【QT】记录一次QT程序发布exe过程
  • 【从零开始安装kubernetes-1.7.3】2.flannel、docker以及Harbor的配置以及作用
  • C++类的相互关联
  • Go 语言编译器的 //go: 详解
  • in typeof instanceof ===这些运算符有什么作用
  • Java到底能干嘛?
  • java中的hashCode
  • Netty源码解析1-Buffer
  • PaddlePaddle-GitHub的正确打开姿势
  • Rancher-k8s加速安装文档
  • vue-router的history模式发布配置
  • 分享自己折腾多时的一套 vue 组件 --we-vue
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 手机端车牌号码键盘的vue组件
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 通信类
  • 微信小程序填坑清单
  • 在Mac OS X上安装 Ruby运行环境
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • 交换综合实验一
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • 智能情侣枕Pillow Talk,倾听彼此的心跳
  • ​低代码平台的核心价值与优势
  • ​一帧图像的Android之旅 :应用的首个绘制请求
  • # 计算机视觉入门
  • #NOIP 2014#Day.2 T3 解方程
  • #中的引用型是什么意识_Java中四种引用有什么区别以及应用场景
  • (1)(1.13) SiK无线电高级配置(五)
  • (1)Android开发优化---------UI优化
  • (PySpark)RDD实验实战——取一个数组的中间值
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • (八十八)VFL语言初步 - 实现布局
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (黑客游戏)HackTheGame1.21 过关攻略
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (十一)手动添加用户和文件的特殊权限
  • (四)Android布局类型(线性布局LinearLayout)
  • (图文详解)小程序AppID申请以及在Hbuilderx中运行