博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF10E Greedy Change 判断硬币系统是否能用贪心策略
阅读量:6155 次
发布时间:2019-06-21

本文共 2488 字,大约阅读时间需要 8 分钟。

Billy investigates the question of applying greedy algorithm to different spheres of life. At the moment he is studying the application of greedy algorithm to the problem about change. There is an amount of n coins of different face values, and the coins of each value are not limited in number. The task is to collect the sum x with the minimum amount of coins. Greedy algorithm with each its step takes the coin of the highest face value, not exceeding x. Obviously, if among the coins' face values exists the face value 1, any sum x can be collected with the help of greedy algorithm. However, greedy algorithm does not always give the optimal representation of the sum, i.e. the representation with the minimum amount of coins. For example, if there are face values {1, 3, 4} and it is asked to collect the sum 6, greedy algorithm will represent the sum as 4 + 1 + 1, while the optimal representation is 3 + 3, containing one coin less. By the given set of face values find out if there exist such a sum x that greedy algorithm will collect in a non-optimal way. If such a sum exists, find out the smallest of these sums.
Input

The first line contains an integer n (1 ≤ n ≤ 400) — the amount of the coins' face values. The second line contains n integers ai (1 ≤ ai ≤ 109), describing the face values. It is guaranteed that a1 > a2 > ... > an and an = 1.

Output

If greedy algorithm collects any sum in an optimal way, output -1. Otherwise output the smallest sum that greedy algorithm collects in a non-optimal way.

Sample test(s)
input
5 25 10 5 2 1
output
-1
input
3 4 3 1
output
6

 

参考论文《A polynomial-time algorithm for the change-making problem》

设找零钱的最小表示为M(x),贪心表示为G(x),最小不满足M(x)=G(x)的值为w。

如题中input2,M(6)={0,2,0}, G(6)={1,0,2}。

设M(w)第一个非0元素在位置i,最后一个非0元素在位置j

有这么一个结论:

M(w)和G(c[i-1]-1)从1到j-1位都相等,M[j]=G[j]+1。

于是可以通过枚举i,j求出w的值。

1 #include  2 #include 
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #define ll long long13 #define pii pair
14 #define mp(x,y) make_pair(x,y)15 using namespace std;16 const int inf = 0x7fffffff;17 const double eps = 1e-12;18 const int mod = 1e9+7;19 const int maxn = 300005;20 using namespace std;21 int c[401];22 int main() {23 int n;24 cin>>n;25 for (int i=0; i
>c[i];27 int ans=-1;28 for (int i=1; i
View Code

 

 

转载于:https://www.cnblogs.com/mandora/p/3791325.html

你可能感兴趣的文章
HTML 邮件链接,超链接发邮件
查看>>
HDU 5524:Subtrees
查看>>
手机端userAgent
查看>>
pip安装Mysql-python报错EnvironmentError: mysql_config not found
查看>>
http协议组成(请求状态码)
查看>>
怎样成为一个高手观后感
查看>>
[转]VC预处理指令与宏定义的妙用
查看>>
MySql操作
查看>>
python 解析 XML文件
查看>>
MySQL 文件导入出错
查看>>
java相关
查看>>
由一个异常开始思考springmvc参数解析
查看>>
向上扩展型SSD 将可满足向外扩展需求
查看>>
虚机不能启动的特例思考
查看>>
SQL Server编程系列(1):SMO介绍
查看>>
在VMware网络测试“专用VLAN”功能
查看>>
使用Formik轻松开发更高质量的React表单(三)<Formik />解析
查看>>
也问腾讯:你把用户放在什么位置?
查看>>
CSS Sprites 样式生成工具(bg2css)
查看>>
[转]如何重构代码--重构计划
查看>>