10626 - Buying Coke

 1 /*
 2 题意:
 3 就是说有1,5,10三种硬币,要在自动售货机买可乐,单价8,
 4 一次只能买一瓶,然后售货机找零,然后拿着剩下的钱再买,
 5 给出要买的可乐的数量还有1,5,10三种货币的数量,问至少要给多少硬币。
 6 例如,2 2 1 1,要买2瓶可乐,先给一次10块,剩下1,5,10的硬币 4 1 0 枚,
 7 再给一个5,3个1, 所以总共要给 5个硬币
 8
 9 */
10 #include<cstdio>
11 #include<cstring>
12 #include<algorithm>
13 #define INF 0x3f3f3f3f
14 using namespace std;
15 int c,n1,n5,n10;
16 int dp[850][250][150];
17 int dfs(int o,int f,int t,int cnt)
18 {
19     if(o*1+f*5+t*10 < 8) return 0;
20     if(cnt == c) return 0;
21     if(dp[o][f][t] != INF) return dp[o][f][t];
22
23     if(t >= 1)
24          dp[o][f][t]= min( dp[o][f][t] , dfs(o+2,f,t-1,cnt+1)+1);
25     if(f >= 2)
26         dp[o][f][t] = min( dp[o][f][t] , dfs(o+2,f-2,t,cnt+1)+2);
27     if(f >= 1 && o >= 3)
28         dp[o][f][t] = min(dp[o][f][t], dfs(o-3,f-1,t,cnt+1)+4);
29     if(o>=3 && t>=1 )
30         dp[o][f][t] = min(dp[o][f][t], dfs(o-3,f+1,t-1,cnt+1)+4);
31     if(o >=8)
32         dp[o][f][t] = min(dp[o][f][t], dfs(o-8,f,t,cnt+1) + 8 )    ;
33     return    dp[o][f][t];
34 }
35 int main()
36 {
37     int t;
38     scanf("%d",&t);
39     while(t--)
40     {
41         scanf("%d%d%d%d",&c,&n1,&n5,&n10);
42         memset(dp,INF,sizeof(dp));
43         dp[0][0][0]=0;
44         dfs(n1,n5,n10,0);
45         printf("%d\n",dp[n1][n5][n10]);
46     }
47     return 0;
48 }
49
50 /*
51 #include<cstdio>
52 #include<cstring>
53 #include<algorithm>
54 #define INF 0x3f3f3f3f
55 using namespace std;
56 int c,n1,n5,n10;
57 int dp[800][200][100];
58 int dfs(int o,int f,int t,int cnt)
59 {
60     if(o*1+f*5+t*10 < 8) return 0;
61     if(cnt == 0) return 0;
62     if(dp[o][f][t] != -1) return dp[o][f][t];
63
64     dp[o][f][t] = INF;
65     if(t >= 1)
66          dp[o][f][t]= min( dp[o][f][t] , dfs(o+2,f,t-1,cnt-1)+1);
67     if(f >= 2)
68         dp[o][f][t] = min( dp[o][f][t] , dfs(o+2,f-2,t,cnt-1)+2);
69     if(f >= 1 && o >= 3)
70         dp[o][f][t] = min(dp[o][f][t], dfs(o-3,f-1,t,cnt-1)+4);
71     if(o>=3 && t>=1 )
72         dp[o][f][t] = min(dp[o][f][t], dfs(o-3,f+1,t-1,cnt-1)+4);
73     if(o >=8)
74         dp[o][f][t] = min(dp[o][f][t], dfs(o-8,f,t,cnt-1) + 8 )    ;
75     return    dp[o][f][t];
76 }
77 int main()
78 {
79     int t;
80     scanf("%d",&t);
81     while(t--)
82     {
83         scanf("%d%d%d%d",&c,&n1,&n5,&n10);
84         memset(dp,-1,sizeof(dp));
85         dp[0][0][0]=0;
86         dfs(n1,n5,n10,c);
87         printf("%d\n",dp[n1][n5][n10]);
88     }
89     return 0;
90 }
91
92 */
时间: 2024-07-28 14:06:04

10626 - Buying Coke的相关文章

uva 10626 Buying Coke (DP记忆化搜索)

uva 10626 Buying Coke 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 va

UVa 10626 Buying Coke(DP)

Problem D Buying Coke Input: Standard Input Output: Standard Output Time Limit: 2 Seconds 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 machi

uva 10626 Buying Coke (DP + 记忆化搜索)

Problem D Buying Coke Input: Standard Input Output: Standard Output Time Limit: 2 Seconds 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 machi

UVA 10626 Buying Coke (记忆化)

地址:点击打开链接 题意:就是买一个售价8分的饮料,然后你有的硬币有1,5,10分三种. 然后问买c瓶饮料,一次一次买,你最小的投币次数. 我们可以有几种方法:1:投8个一分  2:投一个5分的3个1分的 3:投一个10分的找3个一分的 4:投一个10分的3个一分的,找一个5分的 还有其他方案但是不是太划算. #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #in

uva10626 - Buying Coke(记忆话搜索)

题目:uva10626 - Buying Coke(记忆话搜索) 题目大意:给你3种价值的硬币, 1, 5, 10现在要求你取自动售卖机买可乐,一瓶可乐价值8,给你要求买的可乐的数目,和三种硬币的数目,问你最少需要投多少硬币.自动售卖机会根据你投入的钱来找零,可以的话找出的零钱硬币会最少. 解题思路: 这题之前没有想到可乐的已经购买瓶数是隐含在剩余的硬币情况中,换句话说就是你买了多少瓶可乐,不论用什么方式买,剩余的硬币的情况总能反映买了多少瓶可乐.所 以数组开3维就可以了.而且要注意还有 1 个

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

dp题目列表

10271 - Chopsticks 10739 - String to Palindrome 10453 - Make Palindrome 10401 - Injured Queen Problem 825 - Walking on the Safe Side 10617 - Again Palindrome 10201 - Adventures in Moving - Part IV 11258 - String Partition 10564 - Paths through the Ho

[BZOJ] 1618: [Usaco2008 Nov]Buying Hay 购买干草

1618: [Usaco2008 Nov]Buying Hay 购买干草 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1216  Solved: 633[Submit][Status][Discuss] Description 约翰的干草库存已经告罄,他打算为奶牛们采购H(1≤H≤50000)磅干草,他知道N(1≤N≤100)个干草公司,现在用1到 N给它们编号.第i个公司卖的干草包重量为Pi(1≤Pi≤5000)磅,需要的开销为Ci(l≤Ci≤5

2020: [Usaco2010 Jan]Buying Feed, II

2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 220  Solved: 162[Submit][Status] Description (buying.pas/buying.in/buying.out 128M 1S) Farmer John needs to travel to town to pick up K (1 <= K <= 100) pounds of feed