Codeforces 10E. Greedy Change 科学设计硬币

博客探讨了在不同生活领域应用贪婪算法的问题,特别是关于找零问题。当有不同面值的硬币且不限数量时,任务是用最少的硬币数凑出指定金额x。虽然贪婪算法通常会优先选择最大面值的硬币,但并不总是最优解。例如,面对值为{1, 3, 4}的情况,凑出6元时,贪婪算法会选择4+1+1,而最优解是3+3。文章通过给出硬币面值,判断是否存在使贪婪算法非最优的总金额,并找出这样的最小非最优总金额。" 113895208,9317827,Kali Linux中RsaCtfTool的安装步骤与问题解决,"['Linux', 'CTF工具', '软件安装', '系统管理', '信息安全']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E. 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 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.

Examples
input
5
25 10 5 2 1
output
-1
input
3
4 3 1
output
6

不用dp太难了吧啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊看不懂啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊先记着吧

#include <iostream>  
using namespace std;  
int n,i,j,k,c[411],x,y,z,o,ans;  
int main()  
{  
    ans=1 << 30;  
    cin >> n;  
    for (i=1;i<=n;i++) cin >> c[i];  
    for (i=2;i<=n;i++)  
    for (j=i;j<=n;j++)  
    {  
        x=c[i-1]-1;  
        y=0;  
        for (k=i;k<=j;k++)  
        {  
            z=x/c[k];  
            x-=z*c[k];  
            y+=z;  
        }  
        x=c[i-1]-1-x+c[j];  
        o=x;  
        y++;  
        if (x<ans)  
        {  
           for (k=1;k<=n;k++)  
           {  
               z=x/c[k];  
               x-=z*c[k];  
               y-=z;  
           }  
           if (y<0) ans=o;  
        }  
    }  
    if (ans==1 << 30) cout << -1;  
    else              cout << ans;  
    return 0;  
}  


啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

dp的就是naq17

http://cs.ecs.baylor.edu/~hamerly/icpc/qualifier_2017/naq17_slides.pdf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值