CF10E Greedy Change

链接

\(\text{明显的恶意评分好吧}\)

Idea

题目中说了,一种\(DP\),一种贪心;

我用的贪心

Solution

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

如题中,\(f(6)={0,2,0}\),\(G(6)={1,0,2}\)

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

有这么一个结论:

\(f(w)\)\(G(a[i-1]-1)\)\(1\)\(j-1\)位都相等,\(f[j]=G[j]+1\)

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#define ll long long
#define maxn 550
#define inf 2147483647
#define mod 10003
#define eps 1e-6
#define pi acos(-1.0)
#define de(x) ((x)*(x))
using namespace std; 
inline int read(){
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)) {x=x*10+ch-48;ch=getchar();}
    return x*f;
}
int a[maxn];
int ans=inf,n;
inline int G(int x){
    int ans=0;
    for(int i=1;i<=n;i++){
        int d=x/a[i];
        ans+=d;
        x-=a[i]*d;  
    }
    return ans;
}
signed main(){
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=2;i<=n;i++){
        int x=a[i-1]-1;
        int y=x,sum=0;
        for(int j=1;j<=n;j++){
            int d=x/a[j];
            sum+=d;
            x-=a[j]*d;
            if(sum+1<G(y-x+a[j])) ans=min(ans,y-x+a[j]);
        }
    }
    if(ans==inf) puts("-1");
    else printf("%d",ans); 
    return 0;
}

转载于:https://www.cnblogs.com/cbyyc/p/11457258.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值