入门经典 例题9-3 硬币问题

#include 
#include 
#include 
#include 
using namespace std;
const int MAXN = 20;
const int MAXS = 200;
int v[MAXN];
int d[MAXS]; // d[i] : the longest path start from i
int n, s;

int dp(int s)
{
    int& ans = d[s];
    if(ans != -1) return ans;
    ans = -1<<30;
    for(int i=0; i= 0) {
            int di = dp(s-v[i]);
            if(di+1 > ans) {
                ans = di + 1;
            }
        }
    }
    return ans;
}

int dp2(int s)
{
    int& ans = d[s];
    if(ans != -1) return ans;
    ans = 1<<30;
    for(int i=0; i= 0) {
            int di = dp2(s-v[i]);
            if(di == -1) continue;
            if(ans == -1) ans = di+1;
            else if(di+1 < ans) ans = di+1;
        }
    }
    return ans;
}

int mi[MAXS];
int si[MAXS];
int ma[MAXS];
int sa[MAXS];

const int INF = 1<<30;
void dp3()
{
    for(int i=0; i<=s; i++) {
        mi[i] = INF;
        ma[i] = -1*INF;
    }
    mi[0] = 0;
    ma[0] = 0;
    for(int i=1; i<=s; i++) {
        for(int j=0; j= v[j]) {
                if(mi[i] > mi[i-v[j]] + 1) {
                    mi[i] = mi[i-v[j]] + 1;
                    si[i] = v[j];
                }
                if(ma[i] < ma[i-v[j]] + 1) {
                    ma[i] = ma[i-v[j]] + 1;
                    sa[i] = v[j];
                }
            }
        }
    }
}

void print_min()
{
    while(s) {
        printf("%d ", si[s]);
        s -= si[s];
    }
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    while(scanf("%d%d", &n, &s) == 2) {
        for(int i=0; i

你可能感兴趣的:(入门经典 例题9-3 硬币问题)