uva 10626Buying Coke

来源:互联网 发布:韩顺平js视频教程下载 编辑:程序博客网 时间:2024/05/21 10:04

原题:
I often buy Coca-Cola from the vending machine at work. Usually I buy several cokes at once, since my working mates also likes coke. A coke in the vending machine costs 8 Swedish crowns, and the machine accept crowns with the values 1, 5 and 10. As soon as I press the coke button (after having inserted sufficient amount of money), I receive a coke followed by the exchange (if any). The exchange is always given in as few coins as possible (this is uniquely determined by the coin set used). This procedure is repeated until I’ve bought all the cokes I want. Note that I can pick up the coin exchange and use
those coins when buying further cokes. Now, what is the least number of coins I must insert, given the number of cokes I want to buy and the number of coins I have of each value? Please help me solve this problem while I create some harder problems for you. You may assume that the machine won’t run out of coins and that I always have
enough coins to buy all the cokes I want.
Input
The first line in the input contains the number of test cases (at most 50). Each case is then given on a line by itself. A test case consists of four integers: C (the number of cokes I want to buy), n 1 , n 5 , n 10 (the number of coins of value 1, 5 and 10, respectively). The input limits are 1 ≤ C ≤ 150, 0 ≤ n 1 ≤ 500, 0 ≤ n 5 ≤ 100 and 0 ≤ n 10 ≤ 50.
Output
For each test case, output a line containing a single integer: the minimum number of coins needed to insert into the vending machine.
Sample Input
3
2 2 1 1
2 1 4 1
20 200 3 0
Sample Output
5
3
148

中文:
一个人想要买一定数量的可乐,给你四个数,分别是想要买可乐的数量,1元硬币的个数,5元硬币的个数,10元硬币的个数。这个人买可乐只能一瓶一瓶的买,问你最少花多少个硬币。在贩卖机里投入数量多于一瓶可乐的钱,会找给你数量最少的硬币。可乐8元钱。

#include <bits/stdc++.h>using namespace std;int dp[800][110][60];int dfs(int one,int five,int ten,int num){    if(dp[one][five][ten]!=-1)        return dp[one][five][ten];    if(num==0)        return 0;    int res[5];    for(int i=0;i<5;i++)        res[i]=INT_MAX;    if(one>=8)        res[0]=dfs(one-8,five,ten,num-1)+8;    if(ten>=1)        res[1]=dfs(one+2,five,ten-1,num-1)+1;    if(five>=2)        res[2]=dfs(one+2,five-2,ten,num-1)+2;    if(one>=3&&ten>=1)        res[3]=dfs(one-3,five+1,ten-1,num-1)+4;    if(one>=3&&five>=1)        res[4]=dfs(one-3,five-1,ten,num-1)+4;    sort(res,res+5);    dp[one][five][ten]=res[0];    return dp[one][five][ten];}int main(){    ios::sync_with_stdio(false);    int t;    cin>>t;    while(t--)    {        memset(dp,-1,sizeof(dp));        int tot,one,five,ten;        cin>>tot>>one>>five>>ten;        int ans=dfs(one,five,ten,tot);        cout<<ans<<endl;    }    return 0;}

思路:

转移方程很好想tot代表当前想要买的可乐数,one,five,ten分别是当前的钱币数。

dp[tot][one][five][ten]=min{        dp[tot-1][one-8][five][ten]+8,        dp[tot-1][one+2][five-2][ten]+2,        dp[tot-1][one+2][five][ten-1]+1,        dp[tot-1][one-3][five+1][ten-1]+4,        dp[tot-1][one-3][five-1][ten]+4        }

时间复杂度是O(tot*one*five*ten*4) =_=

即使是使用打表,枚举出所有状态也会完全超时!!

穷思苦想了半天也不知道该怎么优化,上网上看了题解以后知道了应该用记忆化搜索,每次搜索一个情况的解,就不需要保存tot这个状态了。

0 0
原创粉丝点击