Original question:
I often buy Coca-Cola from the vending. 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 have inserted sufficient amount of money), I receive a Coke followed by the ex Change (if any). The exchange is always given on as few coins as possible (this is uniquely determined by the coin set used). This procedure was 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's the least number of coins I must insert, given the number of Cokes I want to buy and the number of coins I ha ve of each value? Please help me solve the problem while I create some harder problems for you. 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 is the input contains the number of test cases (at most 50). Each of the 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 (the number of coins of Val UE 1, 5 and, respectively). The input limits is 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 integer:the minimum number of coins needed to insert into the Vendi Ng Machine.
Sample Input
3
2 2 1 1
2 1 4 1
20 200 3 0
Sample Output
5
3
148
English:
A person wants to buy a certain amount of coke, give you four number, respectively is want to buy Coke quantity, 1 yuan coin number, 5 yuan coin number, 10 yuan coin number. This man can only buy a bottle of Coke , ask you to spend at least how many coins. Put more than a bottle of Coke in the vending machine and you'll find the least amount of coins. Coke 8 Yuan.
#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;
}
Ideas:
Transfer equation Well, I think tot represents the number of Cokes you want to buy now, One,five,ten is the current number of coins, respectively.
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
}
Time complexity is O (tot*one*five*ten*4) =_=
Even with the use of a table, all States are fully timed out.
Poor think instead focusing a half-day also do not know how to optimize, on the Internet to see the solution after you know that should use memory search, each search for a situation, you do not need to save tot this state.