codeforces 10E

题意:给定n种货币,每种货币数量无限。 现在要求以最少的货币数目表示一个数S。 很自然就会想到贪心:每次取最大的不超过当前待表示数的货币。 现在,你的任务是证明贪心法不一定最优:找到最小的S,使得正常人的表示法 比理论最优解差,或说明这样的S不存在。 n ≤ 300
题解:一道论文题,《A Polynomial-time Algorithm for the Change-making Problem》

#include <bits/stdc++.h>

using namespace std;

int n, dat[500], res = -1;

int main(){
    cin >> n;
    for(int i = 1; i <= n; i ++) 
		cin >> dat[i];
    
    for(int i = 1; i <= n; i ++)
        for(int j = i + 1; j <= n; j ++){
            int cnt1 = 1, cnt2 = 0, W = dat[i] - 1, t;
            
            for(int k = i + 1; k <= j; k ++) //非贪心做法
                cnt1 += (W / dat[k]), W %= dat[k];
                
            t = W = dat[i] - 1 - W + dat[j];
            for(int k = 1; k <= n; k ++) //贪心做法
                cnt2 += (W / dat[k]), W %= dat[k];
            
            if(cnt1 < cnt2 && (res == -1 || res > t)) 
				res = t;
        }
    cout << res;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38759433/article/details/86544145