【CF10E】Greedy Change

题目

题意翻译
给定n种货币,每种货币数量无限。 现在要求以最少的货币数目表示一个数S。 一种方法当然是DP求一个最优解了, 当然正 常人的做法是贪心:每次取最大的不超过当前待表示数的货币。 现在,你的任务是证明正常人的表示法不一定最优:找到最小的S,使得正常人的表示法 比理论最优解差,或说明这样的S不存在。

输入格式

第一行包含一个整数n(1≤n≤400)。第二行包含n个整数ai(1 ≤ai≤109)为每个硬币面值。保证a1>a2>an>并且an=1

输出格式

如果贪心能以最优的方式求出所以和,输出-1。否则以非最优方式输出贪婪算法收集的最小和。

Translated by @liyifeng

题目描述
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 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 x with the minimum amount of coins. Greedy algorithm with each its step takes the coin of the highest face value, not exceeding x x . Obviously, if among the coins’ face values exists the face value 1, any sum x 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} 1,3,4 and it is asked to collect the sum 6 6 , greedy algorithm will represent the sum as 4+1+1 4+1+1 , while the optimal representation is 3+3 3+3 , containing one coin less. By the given set of face values find out if there exist such a sum x 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 n ( 1<=n<=400 1<=n<=400 ) — the amount of the coins’ face values. The second line contains n n integers a_{i} a
i
​ ( 1<=a_{i}<=10^{9} 1<=a
i
​ <=10
9
), describing the face values. It is guaranteed that a_{1}>a_{2}>…>a_{n} a
1
​ >a
2
​ >…>a
n
​ and a_{n}=1 a
n
​ =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.

输入输出样例
输入样例#1:
5
25 10 5 2 1
输出样例#1:
-1
输入样例#2:
3
4 3 1
输出样例#2:
6

思路

设找零钱的最小表示为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的值。

代码

#include<bits/stdc++.h>
using namespace std;

template <typename T> void chmin(T &x,const T &y)
{
    if(x>y)x=y;
}
template <typename T> void chmax(T &x,const T &y)
{
    if(x<y)x=y;
}
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define per(i,r,l) for(int i=r;i>=l;--i)
#define pb push_back
#define mp make_pair
#define gc (c=getchar())
int read()
{
    char c;
    while(gc<'-');
    if(c=='-')
    {
        int x=gc-'0';
        while(gc>='0')x=x*10+c-'0';
        return -x;
    }
    int x=c-'0';
    while(gc>='0')x=x*10+c-'0';
    return x;
}
#undef gc

const int N=400+5,inf=2e9;
int n,c[N];
int ans=inf;
int G(int x)
{
    int ans=0;
    rep(i,1,n)
    {
        int d=x/c[i];
        ans+=d;
        x-=c[i]*d;
    }
    return ans;
}

int main()
{
    n=read();
    rep(i,1,n)c[i]=read();
    rep(i,2,n)
    {
        int x=c[i-1]-1;
        int x0=x;
        int m=0;
        rep(j,i,n)
        {
            int d=x/c[j];
            m+=d;
            x-=c[j]*d;
            if(m+1<G(x0-x+c[j]))
                chmin(ans,x0-x+c[j]);
        }
    }
    if(ans==inf)puts("-1");
    else cout<<ans<<endl;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值