Greedy Change
2 seconds
256 megabytes
standard input
standard output
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 sum6, 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.
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.
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.
5
25 10 5 2 1
-1
3
4 3 1
6
分析:据说是论文结论题。
A Polynomial-time Algorithm for the Change-Making Problem;
由结论,这个数比a[i]大一点点;
所以先贪心a[i]-1,然后枚举j(j>i),把a[j]的数目+1,然后再贪心,看是不是数目变大了;
如果变大了,则取一个最小的答案;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=4e2+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,ans,a[maxn],b[maxn],c[maxn];
bool flag;
int solve(int p,int *q)
{
int ret=;
for(int i=;i<=n;i++)
{
q[i]=p/a[i];
p%=a[i];
ret+=q[i];
}
return ret;
}
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n)scanf("%d",&a[i]);
rep(i,,n)
{
solve(a[i]-,b);
rep(j,i+,n)
{
int now=,num=;
rep(k,,j-)now+=a[k]*b[k],num+=b[k];
now+=a[j]*(b[j]+),num+=b[k]+;
if(solve(now,c)>num)
{
if(!flag)
{
flag=true;
ans=now;
}
else ans=min(ans,now);
}
}
}
if(flag)printf("%d\n",ans);
else puts("-1");
//system("Pause");
return ;
}
Greedy Change的更多相关文章
- [Codeforces 10E] Greedy Change
Brief Introduction: 给你一些种类的硬币,用最少的硬币数表示X 求最小的使贪心算法错误的X Algorithm: 一道论文题,<A Polynomial-time Algori ...
- [cf10E]Greedy Change
对于$w$的表示方案,可以用序列描述,即$x_{i}$表示第$i$种货币的数量 贪心策略得到的方案即是(对应序列)字典序最大的方案,并定义最优策略得到的方案为在最小化货币总数的基础上,(对应序列)字典 ...
- 贪婪算法(Greedy Algorithm)
Greedy Algorithm <数据结构与算法--C语言描述> 图论涉及的三个贪婪算法 Dijkstra 算法 Prim 算法 Kruskal 算法 Greedy 经典问题:coin ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
- “知乎杯”2018 CCF 大学生计算机系统与程序设计竞赛 贪心算法(greedy)
--> 贪心算法 1)题解 • 分别用V0.V1和V>=2表示度为0.1以及至少为2的顶点集合 • 对于每个顶点,维护三个属性: • degree ...
- 【LeetCode】贪心 greedy(共38题)
[44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...
- 代码的坏味道(10)——发散式变化(Divergent Change)
坏味道--发散式变化(Divergent Change) 发散式变化(Divergent Change) 类似于 霰弹式修改(Shotgun Surgery) ,但实际上完全不同.发散式变化(Dive ...
- USACO . Greedy Gift Givers
Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
随机推荐
- Java 集合 HashMap & HashSet 拾遗
Java 集合 HashMap & HashSet 拾遗 @author ixenos 摘要:HashMap内部结构分析 Java HashMap采用的是冲突链表方式 从上图容易看出,如果选择 ...
- 二维小波包重构wprec2\wprcoef
clc,clear all,close all; load woman; t=wpdec2(X,2,'haar');%小波包2层分解 r_X=wprec2(t);%重构小波包 r_X10=wprcoe ...
- cmusphinx格式问题
在windows下.lm和.dict同时为ANSI编码,输出正确,否则输出乱码或不输出结果.
- Openjudge-计算概论(A)-整数奇偶排序
描述: 输入10个整数,彼此以空格分隔重新排序以后输出(也按空格分隔),要求:1.先输出其中的奇数,并按从大到小排列:2.然后输出其中的偶数,并按从小到大排列.输入任意排序的10个整数(0-100), ...
- Office Web Apps安装部署(一)
来源于:http://www.cnblogs.com/poissonnotes/p/3238238.html 系统要求为Windows Server 2012, 注意:安装Office Web App ...
- visible绑定(The "visible" binding)
对visible进行绑定可以控制元素的显示和隐藏. 示例: <div data-bind="visible: shouldShowMessage"> You will ...
- eclipse和tomcat整合之后每次发布server.xml被修改(转)
eclipse每次发布,server.xml和context.xml总是被还原 直接找到eclispse工程下的server工程,把里面的相应的server.xml和context.xml修改了即可, ...
- VS2013程序打包部署详细图解
目录(?)[+] 新建项目 FILE –> New –> Project,如下图所示: 注意:如果 InstallShield Limited Edition Project 显 ...
- CSS传统布局之布局模型
刚开始准备这篇文章的时候,查到的有很多包含“布局模型”的中文博客或是资料,但是google上并未找到类似字眼,google到的是“flex layout module”“grid layout mod ...
- db2安装要设置tcp、ip
1.注册表变量DB2COMM是否已经设置了值,是什么级别的?db2set -all | grep -i "DB2COMM" (in unix like os)db2set -all ...