您的位置:首页 > 其它

UVA - 10626 Buying Coke

2013-09-27 20:04 411 查看
题意:给你四个数,分别代表要买的可乐的数量,价格为1,5,10的硬币数量,每瓶可乐价格是8,问买这些可乐最小要的硬币数量,每买一瓶返还的硬币数最少的可能。dp[c][x][y][z]的表示是最容易的,但显然空间占用太大,也容易想到

我们可以把第一个参数去掉,至于返还的可能也就5种,2*5,8*1,10*1,5*1+3*1,10*1+1*3,显然为了能够最少我们是从10这种硬币开始的

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int n,n1,n2,n3,dp[710][160][160];

int DP(int c,int x,int y,int z){
    if (dp[x][y][z])
        return dp[x][y][z];

    if (c == 0)
        return 0;

    if (z == 0 && y == 0)
        return dp[x][y][z] = c * 8;
    int Min = 1 << 20;
    if (z >= 1){
        Min = DP(c-1,x+2,y,z-1)+1;
        if (x >= 3)
            Min = min(Min,DP(c-1,x-3,y+1,z-1)+4);
        return dp[x][y][z] = Min;
    }
    else {
        if (x >= 3)
            Min = min(Min,DP(c-1,x-3,y-1,z)+4);
        if (y >= 2)
            Min = min(Min,DP(c-1,x+2,y-2,z)+2);
        return dp[x][y][z] = Min;
    }
}

int main(){
    int t;
    scanf("%d",&t);
    while (t--){
        scanf("%d%d%d%d",&n,&n1,&n2,&n3);
        memset(dp,0,sizeof(dp));
        int ans = DP(n,n1,n2,n3);
        printf("%d\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: