2019 杭电多校 9 1006

题目链接:HDU 6685

比赛链接:2019 Multi-University Training Contest 9

Problem Description

Rikka hates coins, and she used to never carry any coins with her. These days, Rikka is doing her summer internship abroad. Without mobile payment, Rikka has to face strange prices of commodities, and as a result of always using paper currency, she has to face mountainous coins on here table.

In the local currency system, there are \(4\) kinds of coins: \(10\) cents, \(20\) cents, \(50\) cents and \(1\) dollar. Up to now, Rikka has gained at least \(10^{100}\) coins for each kind.

Now, Rikka is going to have dinner in the canteen, and she decides to pay the bill only with coins. There are \(n\) different combos in the canteen and the price of the \(i\)th is \(w_i\) cents. Rikka would like to choose one combo as dinner but she has not decided to choose which one yet. Therefore, she wants to take some coins so that whichever she chooses, she can always pay the bill without receiving any change.

Since Rikka hates coins, she wants to carry as few coins as possible with her. As it is generally known that Rikka is not good at math, she wants you to help her make the decision.

Input

The first line of the input contains a single integer \(T(1\le T\le 500)\), the number of test cases.

For each test case, the first line contains a single integer \(n(1\le n\le 100)\), the number of combos sold in the canteen.

The second line contains \(n\) positive integers \(w_1,…,w_n(1\le w_i\le 10^9)\), which represents the prices.

Output

For each test case, output a single line with a single integer which represents the minimum number of coins. If there is no valid solution, output \(−1\).

Hint

In the first test case, one optimal solution is to bring one coin of \(10\) cents and two coins of \(20\) cents.

In the second test case, one optimal solution is to bring \(5\) coins of one dollar.

Sample Input

3
5
10 20 30 40 50
5
100 200 300 400 500
1
1

Sample Output

3
5
-1

Solution

题意

给出 \(n\) 种物品的价格,现在要从无限枚 \(10\)元,\(20\)元,\(50\)元,\(100\)元的硬币中选出最少的硬币,满足能购买任何一种物品都不用找零。

题解

显然如果个位不为零时没有可行方案。

接下来考虑可行方案的求解。

\(10\) 分的硬币多只会用一个,如果用了两个,直接替换成一个 \(10\) 分一个 \(20\) 分一定不亏。

\(20\) 分的硬币多只会用三个,如果用了四个,直接替换成一个 \(10\) 分两个 \(20\) 分一个 \(50\) 分一定不亏。

\(50\) 分的硬币多只会用一个,如果用了两个,直接替换成一个 \(50\) 分和一个一元一定不亏。

因此,直接暴力枚举 \(10\), \(20\), \(50\) 的硬币的数量即可,整百的部分用一元硬币填充。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int maxn = 100 + 5; int w[maxn]; bool judge(int n, int a, int b, int c) {
for(int i = 0; i <= a; ++i) {
for(int j = 0; j <= b; ++j) {
for(int k = 0; k <= c; ++k) {
if(i * 50 + j * 20 + k * 10 == n) {
return true;
}
}
}
}
return false;
} int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
int flag = 0;
for(int i = 0; i < n; ++i) {
cin >> w[i];
if(w[i] % 10) {
flag = 1;
}
}
if(flag) {
cout << -1 << endl;
continue;
}
int ans = inf;
for(int j = 0; j <= 1; ++j) {
for(int k = 0; k <= 3; ++k) {
for(int l = 0; l <= 1; ++l) {
int flag = 1;
int cnt = 0;
for(int i = 0; i < n; ++i) {
if(w[i] < 100) {
if(judge(w[i], j, k, l)) {
continue;
} else {
flag = 0;
break;
}
} else {
if(judge(w[i] % 100 + 100, j, k, l)) {
cnt = max(cnt, (w[i] - 100) / 100);
} else if(judge(w[i] % 100, j, k, l)) {
cnt = max(cnt, w[i] / 100);
} else {
flag = 0;
break;
}
}
}
if(flag) {
ans = min(ans, cnt + j + k + l);
}
}
}
}
cout << ans << endl;
}
return 0;
}

HDU 6685 Rikka with Coin (枚举 思维)的更多相关文章

  1. HDU 6088 - Rikka with Rock-paper-scissors | 2017 Multi-University Training Contest 5

    思路和任意模数FFT模板都来自 这里 看了一晚上那篇<再探快速傅里叶变换>还是懵得不行,可能水平还没到- - 只能先存个模板了,这题单模数NTT跑了5.9s,没敢写三模数NTT,可能姿势太 ...

  2. HDU 6085 - Rikka with Candies | 2017 Multi-University Training Contest 5

    看了标程的压位,才知道压位也能很容易写- - /* HDU 6085 - Rikka with Candies [ 压位 ] | 2017 Multi-University Training Cont ...

  3. HDU 5831 Rikka with Parenthesis II(六花与括号II)

    31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  4. 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

    // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...

  5. HDU 6091 - Rikka with Match | 2017 Multi-University Training Contest 5

    思路来自 某FXXL 不过复杂度咋算的.. /* HDU 6091 - Rikka with Match [ 树形DP ] | 2017 Multi-University Training Conte ...

  6. HDU 6093 - Rikka with Number | 2017 Multi-University Training Contest 5

    JAVA+大数搞了一遍- - 不是很麻烦- - /* HDU 6093 - Rikka with Number [ 进制转换,康托展开,大数 ] | 2017 Multi-University Tra ...

  7. B - Rikka with Graph HDU - 5631 (并查集+思维)

    As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some mat ...

  8. HDU 6090 17多校5 Rikka with Graph(思维简单题)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  9. HDU 6095 17多校5 Rikka with Competition(思维简单题)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

随机推荐

  1. line-height继承

    父元素设置ling-height;子元素继承父元素的line-height 1.ling-height:固定像素 body{ font-size:20px; line-height: 24px; } ...

  2. java并发编程笔记(三)——线程安全性

    java并发编程笔记(三)--线程安全性 线程安全性: ​ 当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些进程将如何交替执行,并且在主调代码中不需要任何额外的同步或协同,这个类都能表现 ...

  3. PHP面试 MySQL的SQL语句编写

    MySQL的SQL语句编写 面试题一 有A表(id,sex,par,c1,c2),B(id,age,c1,c2)两张表,其中A.id与B.id关联,现在要求写出一条SQL语句,将B中age>50 ...

  4. Python selenium web UI之Chrome 与 Chromedriver对应版本映射表及下载地址和配置(windows, Mac OS)

    浏览器及驱动下载 进行web UI 自动化时,需要安装浏览器驱动webdriver,Chrome浏览器需要安装chromedriver.exe 驱动,Firefox需安装 geckodriver.ex ...

  5. js预编译的四部曲

    众所周知javascript是解释性语言,主要特点为解释一行执行一行. 而在js运行时会进行三件事:1语法分析  2.预编译  3.解释执行 语法分析会在代码执行前对代码进行通篇检查,以排除一些低级错 ...

  6. activiti7组任务测试

    package com.zcc.activiti05; import org.activiti.engine.*;import org.activiti.engine.repository.Deplo ...

  7. 如何在webpack开发中利用vue框架使用ES6中提供的新语法

    在webpack中开发,会遇到一大推问题,特别是babel6升级到babel7,要跟新一大推插件,而对于安装babel的功能就是在webpack开发中,vue中能够是用ES6的新特性: 例如ES6中的 ...

  8. 响应式web开发的一些文章

    CSS Device Adaptation:关注 W3C 建议的 CSS 设备适配标准. “在 CSS 中使用 LESS 实现更多的功能”(作者:Uche Ogbuji,developerWorks, ...

  9. 神器,阿里巴巴Java代码检查插件

    背景 不久,又一气呵成发布了Eclipse/Intellij Idea下的代码检测插件PC3,可谓是国内代码优秀的检测插件.此插件检测的标准是根据<<阿里巴巴Java开发手册(终极版)&g ...

  10. __name__ 与 __main__解读

    在python脚本中我们经常看到如下的代码: # hello.py def hello(): print("hello world!") def test(): hello() i ...