Greedy Change
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

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.

Examples
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;
   由结论,这个数比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的更多相关文章

  1. [Codeforces 10E] Greedy Change

    Brief Introduction: 给你一些种类的硬币,用最少的硬币数表示X 求最小的使贪心算法错误的X Algorithm: 一道论文题,<A Polynomial-time Algori ...

  2. [cf10E]Greedy Change

    对于$w$的表示方案,可以用序列描述,即$x_{i}$表示第$i$种货币的数量 贪心策略得到的方案即是(对应序列)字典序最大的方案,并定义最优策略得到的方案为在最小化货币总数的基础上,(对应序列)字典 ...

  3. 贪婪算法(Greedy Algorithm)

    Greedy Algorithm <数据结构与算法--C语言描述> 图论涉及的三个贪婪算法 Dijkstra 算法 Prim 算法 Kruskal 算法 Greedy 经典问题:coin ...

  4. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

  5. “知乎杯”2018 CCF 大学生计算机系统与程序设计竞赛 贪心算法(greedy)

    --> 贪心算法 1)题解 •        分别用V0.V1和V>=2表示度为0.1以及至少为2的顶点集合 •        对于每个顶点,维护三个属性: •        degree ...

  6. 【LeetCode】贪心 greedy(共38题)

    [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...

  7. 代码的坏味道(10)——发散式变化(Divergent Change)

    坏味道--发散式变化(Divergent Change) 发散式变化(Divergent Change) 类似于 霰弹式修改(Shotgun Surgery) ,但实际上完全不同.发散式变化(Dive ...

  8. USACO . Greedy Gift Givers

    Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...

  9. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

随机推荐

  1. 【Python@Thread】thread模块

    一.关于Python多线程 Python解释器中可以同时运行多个线程,但是再任意时刻只能有一个线程在解释器运行. Python虚拟机的访问是由全局解锁器(GIL)控制的,由GIL保证同时只有一个线程的 ...

  2. webpack-hot-middleware 用于 livereload

    https://github.com/glenjamin/webpack-hot-middleware Webpack hot reloading using only webpack-dev-mid ...

  3. Map获取键值,Map的几种遍历方法

    Map 类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集.接着,Map.Entry类提供了一个 getKey()方法和一个getValue()方法,Map ...

  4. mysql表设计---时间类型

    mysql 时间格式的区别 datetime 日期 +时间timestamp 时间戳 格式都是一样YYYY-MM-DD HH:MM:SS int(12)型存储php的time()时间戳,格式10位14 ...

  5. 8.多线程和Socket通信

    一.多线程 1.进程的概念: 进程就是应用程序的执行实例,有独立的内存空间和系统资源.当一个应用程序没有执行的时候,它就不是一个进程.   2.进行的特征: (1)动态性:动态产生动态消亡. (2)并 ...

  6. CentOS postgresql9.4

    yum install http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-redhat94-9.4-1.noarch.rpm Once t ...

  7. Linux中如何设置java环境变量

    这里介绍Linux下如何设置java环境变量. 工具/原料 Linux java环境变量 方法/步骤 1 查看java的安装路径   查看java执行路径   配置java环境变量   java的安装 ...

  8. Java使用千分位并保留两位小数

    double d = 123456.789; DecimalFormat df = new DecimalFormat("#,##0.00"); System.out.printl ...

  9. The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))

    消息筛选器显示应用程序正在使用中. ((错误来自 HRESULT:0x8001010A (RPC_E_SERVERCALL_RETRYLATER)) 在对Word文档进行合并或者其他操作的时候,如果数 ...

  10. Openjudge-计算概论(A)-短信计费

    描述: 用手机发短信,一条短信资费为0.1元,但限定一条短信的内容在70个字以内(包括70个字).如果你一次所发送的短信超过了70个字,则会按照每70个字一条短信的限制把它分割成多条短信发送.假设已经 ...