题解 CF10E 【Greedy Change】
完全不会,
看了论文A polynomial-time algorithm for the change-making problem后大概懂了,
来叙述一下论文的意思。
将原来的硬币系统记作向量
将对价值x的一个表示记作向量V,即满足
将V用到的硬币数目记作size,即
将对x的贪心表示记作G(x),满足字典序最大
将对x的最小表示记作M(x),满足size最小的前提下字典序最大
易证一个贪心表示的子集也是一个贪心表示,一个最小表示的子集也是一个最小表示。
令w为最小反例,即最小的w使得G(w)!=M(w)
易证G(w)和M(w)的非零位集合无交
令i和j为M(w)最小和最大的非零位
有结论:
证明:
所以
两边都给第i位加1,得
由(2)有
所以
所以
结合(3),(4)
可以发现本来
说明他们j位之前相等
而
说明
所以可以枚举
//by kcz [email protected]
//if you find any bug in my code
//please tell me
#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()
{
freopen("1.in","r",stdin);//freopen("1.out","w",stdout);
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;
}